136,自定义Copy类
来源:互联网 发布:centos minimal 桌面 编辑:程序博客网 时间:2024/06/05 08:57
#import <Foundation/Foundation.h>
@interface Person :NSObject<NSCopying,NSMutableCopying>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)int age;
@end
@implementation Person
- (id)copyWithZone:(nullableNSZone *)zone{
//1,创建对象
Person *person = [[selfclass] allocWithZone:zone];
//2,赋值
person.name = _name;
person.age = _age;
return person;
}
- (id)mutableCopyWithZone:(nullableNSZone *)zone{
//1,创建对象
Person *person = [[selfclass] allocWithZone:zone];
//2,赋值
person.name = _name;
person.age = _age;
return person;
}
-(NSString *)description{
return [NSStringstringWithFormat:@"name = %@,age = %i",_name,_age];
}
@end
@interface Student : Person
@property(nonatomic,assign)double weight;
@end
@implementation Student
-(id)copyWithZone:(NSZone *)zone{
id obj = [supercopyWithZone:zone];
[obj setWeight:_weight];
return obj;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
id obj = [supercopyWithZone:zone];
[obj setWeight:_weight];
return obj;
}
- (NSString *)description
{
return [NSStringstringWithFormat:@"name = %@,age = %i,weight = %lf",[selfname],[selfage],_weight];
}
@end
int main(int argc,const char * argv[]) {
/*1,Copy:需要遵守NSCopy协议,实现copyWithZone方法
mutableCopy:需要遵守NSMutableCopy协议,实现mutableCopyWithZone方法,mutableCopyWithZone方法
*/
Person *person1 = [Personnew];
person1.name = @"ljs1";
person1.age = 10;
Person *newPerson1 = [person1 copy];
newPerson1.age = 19;
NSLog(@"%@",[person1description]);
NSLog(@"%@",[newPerson1description]);
Person *person2 = [Personnew];
person2.name = @"ljs2";
person2.age = 10;
Person *newPerson2 = [person2 mutableCopy];
newPerson2.age = 19;
NSLog(@"%@",[person2description]);
NSLog(@"%@",[newPerson2description]);
//在Copy的时候,得到子类的属性的话,需要重写copyWithZone,
Student *student1 = [Studentnew];
student1.name = @"abc";
student1.age = 123;
student1.weight = 1.64;
Student *student2 = [student1 copy];
NSLog(@"%@",[student1description]);
NSLog(@"%@",[student2description]);
return 0;
}
//2015-12-24 09:09:19.777 18,自定义Copy类[1260:46241] name = ljs1,age = 10
//2015-12-24 09:09:19.778 18,自定义Copy类[1260:46241] name = ljs1,age = 19
//2015-12-24 09:09:19.778 18,自定义Copy类[1260:46241] name = ljs2,age = 10
//2015-12-24 09:09:19.778 18,自定义Copy类[1260:46241] name = ljs2,age = 19
//2015-12-24 09:09:19.779 18,自定义Copy类[1260:46241] name = abc,age = 123,weight = 1.640000
//2015-12-24 09:09:19.779 18,自定义Copy类[1260:46241] name = abc,age = 123,weight = 1.640000
//Program ended with exit code: 0
- 136,自定义Copy类
- copy使用...自定义类实现copy操作...
- 自定义类实现 copy 协议
- 自定义copy
- VB自定义文件COPY的类.
- 自定义Copy函数
- 自定义类的copy实现(实现NSCopying协议)
- OC自定义对象的copy
- (转载)copy,对象自定义copy 什么是property
- AJ整理问题之:copy,对象自定义copy 什么是property
- 自定义方法copy数组 和 Java类库对数组复制的支持
- 自定义方法copy数组 和 Java类库对数组复制的支持
- Copy文件工具类
- copy
- copy
- copy
- copy.
- copy
- MapReduce-定制Partitioner-求文件奇偶数行之和
- MFC 利用小型数据库Access 少步惆 教你用VC开发
- DirectShow 播放mp4编码 视频
- oracle更改服务端字符集
- 【代码笔记】iOS-带索引的tableView
- 136,自定义Copy类
- 【Eclipse】--Eclipse.ini,JDK,JRE,JVM
- Redis 源码分析(zmalloc部分)
- 日经春秋 20151224
- Mybatis自动代码生成插件MybatisGenerator使用
- VC++ fatal error RC1004: unexpected end of file found
- 关于音频管理的一点使用
- jquery easyui的treegrid的控制
- Linux kill 杀死指定进程