@interface Person : NSObject { int _age; //@interface中變量默認是@public @protected int _height; //只能在當前類和子類的對象方法中訪問 @prita">
#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;
}
{
_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