ios特性访问器方法(setter和getter)

Employee.h

@interface Employee:NSObject

{

  int _employeeNumber;

  NSString *_name;

  Employee*_supervisitor;

  int _salary;

}

@property int employeeNumber;

@property(nonatomic,retain) NSString * name;

@property(nonatomic,retain)Employee *supervisitor;

@property int salary;

......

@end

Employee.m

@implementation Employee

@synthesize employeeNumber;

@synthesize name;

//@synthesize supervistor;//在这不要实现setter和getter下面手动实现

@synthesize salary;

-(void)setSupervistor:(Employee *)newSupervisitor

{

  [newSupervistor retain];

  [_supervistor release];

  _supervistor=newSupervisitor;

}

-(Employee*)supervisitor

{

  return _supervisitor;

}

//上面代码解释:上面代码的顺序很重要。向存储在supervisitor中的当前对象发送一条release消息,会对应当存储它的时候所接受的一条retain消息

 

 

 

 

原文地址:https://www.cnblogs.com/zhao123/p/3189715.html