@public:在任何地方都能直接访问对象的成员变量;

@private:只能在当前类的对象方法中直接访问;

@protected:可以在当前类以及其子类的对象方法中直接访问;(默认情况下就是@protected

@package:只要处在同一框架中,就能直接访问对象的成员变量;

*** 成员变量还可以在类的.m文件中声明 但是 所声明的变量是私有变量。



@property 应用


用在@interface;

用来自动生成settergetter的声明;

@property int age;就可以替代下面的两行;

- (int)age; // getter方法

- (void)setAge; // setter方法


示例:

#import <Foundation/Foundation.h>


@interface People : NSObject

{

int age;

float weight;

}


@property int age; // 等价下面的注释

/*

- (int)age; // getter方法

- (void)setAge; // setter方法

*/

@end




 

@synthesize 应用


用在@implementation中;

用来自动生成settergetter方法的实现;

@synthesize age =_age;就可以替代

- (void)setAge:(int)age

{

_age = age ;

}


- (int)age

{

return _age ;

}



示例:


#import "People.h"


@implementation People

@synthesize age =_age; // 等价下面注释

/*

- (void)setAge:(int)age

{

_age = age ;

}


- (int)age

{

return _age ;

}

*/


@end



posted on 2017-01-22 14:51  夜之独行者  阅读(191)  评论(0编辑  收藏  举报