初学者初始化方法 便利构造器
2015-09-09 15:28
375 查看
#pragma mark 初始化方法(在这里,我是初始化方法,通常情况下,把参数最全的初始化方法作为指定初始化方法)
-(id)initWithName:(NSString *)name withAge:(NSString *)age withSex:(NSString *)sex
{
self = [super init]; //父类初始化 子类self继承于父类super
if (self) { //因为父类可能初始化不成功。这时候子类没有必要进行初始化
_name = name;
_sex = sex;
_age = age;
}
return self;
}
#pragma mark 初始化方法2(初始值:name,sex)
-(id)initWithName:(NSString *)name withSex:(NSString *)sex
{
self = [self initWithName:name withAge:nil withSex:sex];
return self;
}
-(id)initWithAge:(NSString *)age withSex:(NSString *)sex
{
self = [self initWithName:nil withAge:age withSex:sex];
return self;
}
-(id)initWithName:(NSString *)name withAge:(NSString *)age
{
self = [self initWithName:name withAge:age withSex:nil];
return self;
}
#pragma mark 初始化方法3
-(id)initWithName:(NSString *)name
{
return [self initWithName:name withAge:nil withSex:nil];
}
#pragma mark setter 方法
-(void)setName:(NSString *)name // 有几个冒号有几个参数 setName:是方法名
{
_name = name;
}
-(void)setSex:(NSString *)sex
{
_sex = sex;
}
-(void)setAge:(NSString *)age
{
_age = age;
}
#pragma mark getter 方法
-(NSString *)getterName
{
return _name;
}
-(NSString *)getterSex
{
return _sex;
}
-(NSString *)getterAge
{
return _age;
}
#pragma mark 便利构造器1
+(id)personWithName:(NSString *)name withAge:(NSString *)age withSex:(NSString *)sex
{
// _name = name;
// _age = age;
// 类方法里面,绝对不可能出现实例变量
Person *p = [[Person alloc]initWithName:name withAge:age withSex:sex];
return p;
}
#pragma mark 便利构造器 2
+(id)personWithName:(NSString *)name
{
return [self personWithName:name withAge:nil withSex:nil];
}
-(id)initWithName:(NSString *)name withAge:(NSString *)age withSex:(NSString *)sex
{
self = [super init]; //父类初始化 子类self继承于父类super
if (self) { //因为父类可能初始化不成功。这时候子类没有必要进行初始化
_name = name;
_sex = sex;
_age = age;
}
return self;
}
#pragma mark 初始化方法2(初始值:name,sex)
-(id)initWithName:(NSString *)name withSex:(NSString *)sex
{
self = [self initWithName:name withAge:nil withSex:sex];
return self;
}
-(id)initWithAge:(NSString *)age withSex:(NSString *)sex
{
self = [self initWithName:nil withAge:age withSex:sex];
return self;
}
-(id)initWithName:(NSString *)name withAge:(NSString *)age
{
self = [self initWithName:name withAge:age withSex:nil];
return self;
}
#pragma mark 初始化方法3
-(id)initWithName:(NSString *)name
{
return [self initWithName:name withAge:nil withSex:nil];
}
#pragma mark setter 方法
-(void)setName:(NSString *)name // 有几个冒号有几个参数 setName:是方法名
{
_name = name;
}
-(void)setSex:(NSString *)sex
{
_sex = sex;
}
-(void)setAge:(NSString *)age
{
_age = age;
}
#pragma mark getter 方法
-(NSString *)getterName
{
return _name;
}
-(NSString *)getterSex
{
return _sex;
}
-(NSString *)getterAge
{
return _age;
}
#pragma mark 便利构造器1
+(id)personWithName:(NSString *)name withAge:(NSString *)age withSex:(NSString *)sex
{
// _name = name;
// _age = age;
// 类方法里面,绝对不可能出现实例变量
Person *p = [[Person alloc]initWithName:name withAge:age withSex:sex];
return p;
}
#pragma mark 便利构造器 2
+(id)personWithName:(NSString *)name
{
return [self personWithName:name withAge:nil withSex:nil];
}
相关文章推荐
- android studio 中去除应用标题栏
- 【JAVASCRIPT】React学习- 数据流(组件通信)
- Unity3D之C#学习
- Win7总是显示“软件应用无法兼容”解决方法
- iOS 打电话、发短信、写邮件、打开常用软件的几种方式
- java对redis的基本操作
- 组织机构树型数据库结构
- MyBatis学习总结(一)——MyBatis快速入门
- [leetcode] add two numbers(大数据加法链表版)
- 格雷码递归实现(c++)
- 程序员练级之路
- 解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
- 用链表实现一元多项式的加、减、乘、求导运算
- Dubbo框架介绍
- 用链表实现一元多项式的加、减、乘、求导运算
- C++标准库和标准模板库
- 提高iOS开发效率的第三方框架等
- js获取某个ID的class名称
- chef端口号
- 如何在QML应用中设计一个C++ Model并使用它