<pre name="code" class="objc">#import<Fountation/Fountation.h>
@interface Person : NSObject
{
int _no;//默认的是@protected
@public //在任何地方都能直接访问对象的成员变量
int _age;
@private //只能在当前类的对象方法中直接访问
int _height;
@protected //能在当前类和子类的对象方法中直接访问
int _weight;
//@package 只要处在同一个框架中,就能直接访问对象的成员变量
}
- (void)test;
@end
@implementation Person
/*
{
//写在这里是私有的 @private
//@implementation中不能定义和@interface中同名的成员变量
NSString _name;
}
*/
- (void)test
{
//当前类的对象方法中能直接访问
_age =100;
_weight = 10;
_height = 29;
}
@end
@interface Student : Person
- (void)test2;
@end
@implementation Student
- (void)test2
{
_age =100;
_weight = 10;
//不能直接访问
_height = 29;
}
@end
int main()
{
@autoreleasepool{
Person *p = [[Person alloc] init];
p->_age =100;
//不能访问
//p->_weight = 10;
//p->_height = 29;
}
return 0;
}