Objective-C 工厂方法
2015-11-26 22:23
441 查看
调用过程更加清晰
+为静态方法
+为静态方法
// 类定义 @interface People : NSObject{ int _age; NSString* _name; } // 工厂方法 静态 +(People*)peopleWithAge:(int)age andName:(NSString*)name; -(id)initWithAge:(int)age andName:(NSString*)name; -(int)getAge; -(NSString*)getName; @end // 类实现 @implementation People // 工厂方法 +(People*)peopleWithAge:(int)age andName:(NSString*)name{ return [[People alloc] initWithAge:age andName:name]; } // 初始化方法 -(instancetype)initWithAge:(int)age andName:(NSString *)name{ self = [super init]; // 执行父类init方法 if (self) { _age = age; _name = name; } return self; } -(int)getAge{ return _age; } -(NSString*)getName{ return _name; } @end // 调用 int main(int argc, char * argv[]) { People *p = [People peopleWithAge:10 andName:@"jinpangpang"]; NSLog(@"p.age %d, p.name %@",[p getAge],[p getName]); @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
相关文章推荐
- Objective-C set/get方法
- 102 For which two database objects can the VERSIONS clause of the Flashback Versions Query be used?
- Swift如何调用Object-C类
- 如何在Objective-C中实现链式语法?
- Objective-C 类,函数调用
- 【读书笔记一】Zen and the Art of Objective_C Craftsmanship
- OC基础
- 92 Examine the output of the query that you executed to list the objects in the recycle bin: SQL> SE
- PHP反射类ReflectionClass和ReflectionObject的使用方法
- SAP锁对象(lock object)
- objective-c 中代码块(blocks)
- 开源库ZXing加入xcode工程报错几个原因及解决方法
- Objective-C官方文档翻译 Block
- Objective-C中的Block
- Objective-C语法之代码块(block)的使用
- IOS,objective_C中用@interface和 @property 方式声明变量的区别
- ObjectiveC中的block用法解析
- error: cannot pass objects of non-trivially-copyable type
- php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组
- iOS 打包上线_报错All object files and libraries for bitcode...