objective-C : 工厂方法
来源:互联网 发布:广州市软件创新人才 编辑:程序博客网 时间:2024/05/22 13:46
#import <Foundation/Foundation.h>
@interface people : NSObject{
NSString* _name;
int _age;
}
//下面创建工厂方法(为了更快捷的创建类的实例而存在),注意:工厂方法以 “+”开头
+(people*)peopleWithAge:(int)age andName:(NSString*)name;
//一般情况下创建一个类的实例要进行初始化例如: people* p=[people alloc]init];
//所以在这里也要声明一个初始化方法 :以(id)开头
-(id)initWithAge:(int)age andName:(NSString*)name;
-(int)getAge;
-(NSString*)getName;//这两个方法用于输出传入过来的_name _age参数
@end
#import "people.h"
@implementation people
+(people*)peopleWithAge:(int)age andName:(NSString *)name{
return [[peoplealloc]initWithAge:ageandName:name];}
- (instancetype)initWithAge:(int)age andName:(NSString *)name
{ self = [superinit];
if (self) {
_age=age;
_name=name;//self用来指明对象是当前方法的接收者(本例中people是这个初始化方法的接收者)
}
return self;
}
-(NSString*)getName{
return _name;}
-(int)getAge{
return _age;}
@end
#import <Foundation/Foundation.h>
#import "people.h"
int main(int argc,constchar * argv[]) {
@autoreleasepool {
people *p=[peoplepeopleWithAge:55 andName:@"习近平"];
//定义类的实例的时候就可以直接将值传给参数,然后通过get方法进行显示
NSLog(@"\n\n she's name is %@ , %d years old \n ",[p getName],[p getAge]);
}
return 0;
}
- objective-C : 工厂方法
- Objective C--工厂方法模式
- Objective C 工厂方法模式
- Objective C--工厂方法模式
- Objective C--简单工厂模式
- Objective C--抽象工厂模式
- Objective C 简单工厂模式
- Objective C 抽象工厂模式
- Objective C--简单工厂模式
- Objective C--抽象工厂模式
- Objective-C:初始化、实例方法、类方法、工厂方法、单例模式
- 工厂模式-C-工厂方法
- objective c实现配置文件+反射 工厂创建
- Objective-C中的反射和反射工厂
- Objective-C中的反射和反射工厂
- objective c实现配置文件+反射 工厂创建
- Objective-C 私有方法
- 扩展方法,Objective-C
- JSON与XML的区别比较
- SQL语句统计每天、每月、每年的 数据
- Tomcat项目部署方式
- BC9hdoj4993&&hdoj4994
- leetcode刷题,总结,记录,备忘 313
- objective-C : 工厂方法
- Struts2之OGNL类型转换
- BC10hdoj5019&&hdoj5020
- C++模板简介
- jQuery实践中发现的问题
- C#中获取日期之间天数的方法代码,及DataTime方法的日期格式整理
- [Lintcode]Minimum Depth of Binary Tree
- Recyclerview.getLayoutPosition()问题
- Struts2之自定义转换器