// 实现 copyWithZone 方法
// 参数 zone 基本不用,它的意思是指定该方法从始至终都在某一个区域分配内存
- ( id ) copyWithZone: ( NSZone * ) zone{
// 方法内部进行以下操作
// ( 1 )实例化对象
A* a = [ A alloc ] init ];
// 一般正规写法是[[ self.class alloc ] init ] 因为这样子类也可以复用该方法
// ( 2 )给属性赋值
a.xx = xx;
// ( 3 )返回新对象
return a;
}
//1. 遵守 NSCopying 协议
@interface Teacher : NSObject<NSCopying>
@property ( nonatomic , copy ) NSString* name ;
//2. 实现 copyWithZone 方法;
- ( id ) copyWithZone: ( NSZone * ) zone ;
@end
@implementation Teacher
// 实现 copyWithZone 方法
- ( id ) copyWithZone: ( NSZone * ) zone
{
Teacher* teacher = [[ self.class alloc ] init ];
teacher.name = self.name ;
return teacher ;
}