标签:修改 比较 add require 种类 复制 atom interface 细节
先借鉴百科对原型模式的介绍:
1 #import <Foundation/Foundation.h> 2 3 @protocol PrototypeCopyProtocol <NSObject> 4 5 @required 6 7 /** 8 复制自己 9 10 @return 返回一个拷贝的对象 11 */ 12 - (id)clone; 13 14 @end
创建对象模型
Student.h
1 #import <Foundation/Foundation.h> 2 #import "PrototypeCopyProtocol.h" 3 4 @interface Student : NSObject <PrototypeCopyProtocol> 5 6 @property (nonatomic, strong) NSString *name; 7 @property (nonatomic, strong) NSString *age; 8 @property (nonatomic, strong) NSString *score; 9 @property (nonatomic, strong) NSString *address; 10 11 #pragma mark - PrototypeCopyProtocol method 12 - (id)clone; 13 14 @end
Student.m
1 #import "Student.h" 2 3 @implementation Student 4 5 - (id)clone { 6 7 Student *stu = [[[self class] alloc] init]; 8 9 stu.name = self.name; 10 stu.age = self.age; 11 stu.score = self.score; 12 stu.address = self.address; 13 14 return stu; 15 } 16 17 @end
下面是Controller中使用:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //创建第一个学生实例 5 Student *stu1 = [[Student alloc] init]; 6 7 stu1.name = @"Jackey"; 8 stu1.age = @"18"; 9 stu1.score = @"90"; 10 stu1.score = @"重庆"; 11 12 //创建第二个学生实例 13 Student *stu2 = [stu1 clone]; 14 15 stu2.name = @"Franky"; 16 17 }
标签:修改 比较 add require 种类 复制 atom interface 细节
原文地址:http://www.cnblogs.com/zhouxihi/p/6034154.html