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

Objective-C内存管理
本文介绍了一个Objective-C编写的Employee类,该类包含了属性如员工编号、姓名、上级及薪资等,并详细展示了如何手动实现对上级属性的内存管理,包括设置和获取的方法。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值