首页 > 学院 > 开发设计 > 正文

OC基础--Property

2019-11-14 18:23:53
字体:
来源:转载
供稿:网友

编译器指令:

用来告诉编译器要做什么

  @PRoperty:

    @property是编译器的指令 告诉编译器在@interface中自动生成setter和getter的声明

  @synthesize:

    @synthesize是编译器的指令 告诉编译器在@implementation中自动生成setter和getter的实现

  手动写setter-getter:

#import <Foundation/Foundation.h>
@interface Member : NSObject{ @public NSString * _name; int _age; NSString * _account; NSString * _passWord;}- (void) setName: (NSString *) name;- (NSString *) name;- (void) setAge: (int) age;- (int) age;- (void) setAccount: (NSString *)account;- (NSString *) account;- (void) setPassword: (NSString *) password;- (NSString *) password; @end #import "Member.h" @implementation Member- (void) setName: (NSString *) name{ _name = name;}- (NSString *) name{ return _name;}- (void) setAge: (int) age{ _age = age;}- (int) age{ return _age;}- (void) setAccount: (NSString *)account{ _account = account;}- (NSString *) account{ return _account;}- (void) setPassword: (NSString *) password{ _password = password;}- (NSString *) password{ return _password;}@end

  使用@property和@synthesize:

#import <Foundation/Foundation.h>@interface Member : NSObject{@public    NSString * _name;    int _age;    NSString * _account;    NSString * _password;}@property NSString * name;@property int age;@property NSString * account;@property NSString * password; @end#import "Member.h" @implementation Member
@synthesize name = _name;@synthesize age = _age;@synthesize account = _account;@synthesize password = _password;
@end

  @property和@synthesize说明:

    @property:

      编译器只要看到@property, 就知道我们要生成某一个属性的getter/setter方法的声明

      /*       - (void)setAge:(int)age;       - (int)age;       */      // 使用@property等效以上两句      @property int age; 

      使用@property作声明的时候 , 不需要加下划线  _

      // 加上下划线后等效于以下两句      @property int _age;      /*       - (void)set_age:(int)_age;       - (int)_age;       */

    @synthesize:

       在@synthesize后面告诉编译器, 需要实现哪个@property生成的声明

      /*       - (void)setAge:(int)age       {          _age = age;       }       - (int)age       {          return _age;       }      */      // 使用@synthesize等效以上部分      // 如果成员变量_age不存在,就会自动生成一个私有的成员变量_age(在.m实现文件中)      @synthesize age = _age; 

       告诉@synthesize, 需要将传入的值赋值给谁和返回谁的值给调用者

      /*       - (void)setAge:(int)age       {          _number = age;       }         - (int)age       {          return _number;       }       */      // 如果这样写读写的是 _number 而不是 _age      @synthesize age = _number;  

       如果在@synthesize后面没有告诉系统将传入的值赋值给谁, 系统默认会赋值给和@synthesize后面写得名称相同的成员变量

      #import <Foundation/Foundation.h>       @interface Person : NSObject      {          @public          int _age;          int age;      }      @property int age;       @end       #import "Person.h"      @implementation Person       @synthesize age;      @end      #import <Foundation/Foundation.h>      #import "Person.h"      int main(int argc, const char * argv[]) {          Person *p = [Person new];          [p setAge:88];              NSLog(@"_age = %i, age = %i", p->_age, p->age);              return 0;      }      /*       输出结果:2015-09-01 19:13:45.846 synthesize基本使用[813:21032] _age = 0, age = 88       所以 @synthesize age;  读写的是 age 属性而不是 _age       如果成员变量 age 不存在,就会自动生成一个私有的成员变量 age(在.m实现文件中)       */ 

      多个属性可以通过一行@synthesize搞定,多个属性之间用逗号连接

      @synthesize name = _name, age = _age, account = _account, password = _password;

私有成员:

  访问修饰符:

     @public

     >可以在其它类中访问被public修饰的成员变量

     >也可以在本类中访问被public修饰的成员变量

     >可以在子类中访问父类中被public修饰的成员变量

     @private

     >不可以在其它类中访问被private修饰的成员变量

     >可以在本类中访问被private修饰的成员变量

     >不可以在子类中访问父类中被private修饰的成员变量

     @protected

     >不可以在其它类中访问被protected修饰的成员变量

     >可以在本类中访问被protected修饰的成员变量

     >可以在子类中访问父类中被protected修饰的成员变量 

     注意: 默认情况下所有的实例变量都是protected

     @package

     >介于public和private之间的

     如果是在其它包中访问那么就是private的

     如果是在当前代码所在的包种访问就是public的

  私有变量:

    写在@implementation中的成员变量, 默认就是私有的成员变量, 并且和利用@private修饰的不太一样, 在@implementation中定义的成员变量在其它类中无法查看, 也无法访问, 这种私有变量只能在本类中访问

    在@interface中定义的变量,  无论使用什么成员变量修饰符修饰(包括@private), 我们都可以在其它类中看到这个变量 只不过有得修饰符修饰的变量我们不能操作而已

  私有方法:

    如果只有方法的实现, 没有方法的声明, 那么该方法就是私有方法  不过在OC中没有真正的私有方法, 因为OC是消息机制  

        // 访问只有实现没有声明的私有方法        id pp = [Person new];        [pp test];             Person *p = [Person new];        [p performSelector:@selector(test)];

@property增强

   1  从Xcode4.4以后apple对@property进行了一个增强, 以后只要利用一个@property就可以同时生成setter/getter方法的声明和实现

   2  默认@property会将传入的属性赋值给_开头的成员变量

   3  如果利用@property来生成getter/setter方法, 那么我们可以不写成员变量, 系统会自动给我们生成一个_开头的成员变量

      注意: @property自动帮我们生成的成员变量是一个私有的成员变量, 也就是说是在.m文件中生成的, 而不是在.h文件中生成的

   4  @property有一个弊端: 它只会生成最简单的getter/setter方法的声明和实现, 并不会对传入的数据进行过滤, 如果想对传入的数据进行过滤, 那么我们就必须重写getter/setter方法

          如果重写了setter方法, 那么property就只会生成getter方法

          如果重写了getter方法, 那么property就只会生成setter方法

          如果同时重写了getter/setter方法, 那么property就不会自动帮我们生成私有的成员变量

#import <Foundation/Foundation.h>@interface Member : NSObject @property NSString * name;@property int age;@property NSString * account;@property NSString * password;@end#import "Member.h"@implementation Member@end

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表