#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age; //@interface中变量默认是@public
@protected
int _height; //只能在当前类和子类的对象方法中访问
@pritate
int _weight; //只能在当前类的对象方法中才能直接访问
@package
NSString *_name;//只能在当前框架内使用
}
-(int) age;
-(void) setAge:(int)age;
-(int) height;
-(void) setHeight:(int)height;
-(int) weight;
-(void) setWeight:(int)weight;
-(NSString *)name;
-(void) setName:(NSString *)name;
@end
@implementation Person
{
NSString *birthday;//@implementation中变量默认是@private
}
-(int) age
{
return -age
}
-(void) setAge:(int)age
{
-age=age;
}
-(int) height
{
return _height;
}
-(void) setHeight:(int)height
{
-height=height;
}
-(int) weight
{
return _weight;
}
-(void) setWeight:(int)weight
{
_weight=weight;
}
-(NSString *)name
{
return _name;
}
-(void) setName:(NSString *)name
{
_name=name;
}
@end
1 @public (公开的)在有对象的前提下,任何地方都可以直接访问。
2 @protected
(受保护的)只能在当前类和子类的对象方法中访问
3 @private
(私有的)只能在当前类的对象方法中才能直接访问
4 @package (框架级别的)作用域介于私有和公开之间,只要处于同一个框架中就可以直接通过变量名问。
5 @interface中的声明的成员变量默认是public,@implatation中声明的成员变量默认是private