标签:成员变量 protected 默认 objective 作用域
/**
*
* @public : 在任何地方都能直接访问对象的成员变量
* @private : 只能在当前类的对象方法中直接访问(@implementation中默认是@private)
* @protected : 可以在当前类及其子类的对象方法中直接访问 (@interface中默认就是@protected)
* @package : 只要处在同一个框架中,就能直接访问对象的成员变量
* @interface和@implementation中不能声明同名的成员变量
*/
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _no;
@public // 在任何地方都能直接访问对象的成员变量
int _age;
@private // 只能在当前类的对象方法中直接访问
int _height;
@protected // 能在当前类和子类的对象方法中直接访问
int _weight;
int _money;
}
- (void)setHeight:(int)height;
- (int)height;
- (void)test;
@end
#import "Person.h"
@implementation Person
{
int _aaa;// 默认就是私有
@public
int _bbb;
// @implementation中不能定义和@interface中同名的成员变量
// int _no;
}
- (void)test
{
_age = 19;
_height = 20;
_weight = 50;
_aaa = 10;
}
- (void)setHeight:(int)height
{
_height = height;
}
- (int)height
{
return _height;
}
@end
#import "Person.h"
@interface Student : Person
- (void)study;
@end
#import "Student.h"
@implementation Student
- (void)study
{
// _height = 10;
[self setHeight:10];
int h = [self height];
_weight = 100;
}
@end
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"
@implementation Car : NSObject
{
@public
int _speed;
@protected
int _wheels;
}
- (void)setSpeed:(int)speed
{
_speed = speed;
}
- (int)speed
{
return _speed;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Student *stu = [Student new];
[stu setHeight:100];
NSLog(@"%d", [stu height]);
Car *c = [Car new];
c->_speed = 250;
//c.speed = 10;
// NSLog(@"%d", c.speed);
//[c setSpeed:<#(int)#>];
Person *p = [Person new];
p->_age = 100;
//p->_height = 20;
//p->_weight = 10;
}
return 0;
}
标签:成员变量 protected 默认 objective 作用域
原文地址:http://blog.csdn.net/wangzi11322/article/details/45126225