继承 初始化方法 遍历构造器
2015-09-07 19:55
429 查看
继承:
1.单向继承,一个类只能有一个父类,一个父类可以有多个子类.
2.单向继承,基类(根类)是OSObject
3.子类可以继承父类的属性和方法
当父类的方法不满足子类的需求时,子类可以重写父类的方法,重写父类的方法,在子类中不需要再次声明.
1.完全重写
2.部分重写 使用super
建立继承关系之后,子类可以继承父类的:
1.实例变量,@public@protected修饰情况之下
2.公开的方法
一个方法如果在.h中声明了,那么这个方法就是公开的方法,如果没有声明,则是私有的.
OC中没有绝对私有的方法.
子类调用方法的过程:
先在本类中寻找方法,如果找到就直接调用,如果没有找到就在其父类中寻找,如果在父类中也没有找到,就继续其次向上寻找(父类的父类),直到找到为止.
如果找不到就抛出异常,引起crash.
对象对对象可以赋值,对象就是指针.
自定义初始化方法不需要在.h文件中声明.因为是重写了该方法.
遍历构造器一定是一个类方法
返回值类型(id,instancetype)
都是声明变量 自定义减号需要声明变量 遍历构造器加号 不需要声明变量
自定义初始化方法:初始化+赋值.
便利构造器:开辟空间+初始化+赋值.
自定义初始化方法:
-(id)initWithColor:(NSString *)color
price:(NSString *)price{
if (self=[super init]) {
_price = price;
_color = color;
}
return self;
}
便利构造器:
+(id)appleWithColor:(NSString *)color
price:(NSString *)price{
Apple *apple=[[Apple alloc]initWithColor:color price:price];
return apple;
}
//初始化方法
//形式:
//系统的:- (instanceType)init;
//自定义的: - (instanceType)initWithXXX(参数类型)参数名......
// Zombie *zombie = [[Zombie alloc]
// initWithName:@"wuye" hp:20 ap:2 speed:10];
// NSLog(@"%@", zombie.name);
//[zombie setName:@"wuye" ap:20 hp:2 speed:1];
//遍历构造器
//+ (instanceType)类名;
//+ (instanceType)类名WithXXX:(参数类型)参数......
BlockZombie *z = [BlockZombie
zombie];
z.name =
@"zahngyang";
自定义初始化方法
#import <Foundation/Foundation.h>
@interface Zombie :
NSObject
{
NSInteger _hp;//血量
NSInteger _ap;//攻击力
CGFloat _speed;//速度
NSString *_name;//名字
}
- (void)move;//移动
- (void)attack;//攻击
- (void)die;//死亡
-(void)setName:(NSString *)name;
- (NSString *)name;
- (void)setSpeed:(CGFloat )speed;
- (CGFloat )speed;
- (void)setAp:(NSInteger )ap;
- (NSInteger )ap;
- (void)setHp:(NSInteger )hp;
- (NSInteger )hp;
- (instancetype)initWithName:(NSString *)name;
- (instancetype)initWithName:(NSString *)name hp:(NSString *)hp ap:(NSInteger )ap speed:(CGFloat
)speed;
@end
#import "Zombie.h"
@implementation Zombie
//自定义初始化方法
- (instancetype)initWithName:(NSString *)name;
{
//1.调用从父类继承的init方法,作用:初始化从父类继承的成员变量
self = [super
init];
//2.self是否为空,
if (self) {
//3.如果self不是空,就给成员变量赋值
_name = name;
}
//4.
return self;
}
- (instancetype)initWithName:(NSString *)name hp:(NSInteger )hp ap:(NSInteger )ap speed:(CGFloat
)speed
{
self = [super
init];
if (self) {
_name = name;
_hp = hp;
_ap = ap;
_speed = speed;
}
return
self;
}
- (void)move
{
NSLog(@"fast");
}
- (void)attack
{
NSLog(@"strong");
}
- (void)die
{
NSLog(@"fast");
}
-(void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (void)setSpeed:(CGFloat )speed
{
_speed = speed;
}
- (CGFloat )speed
{
return _speed;
}
- (void)setAp:(NSInteger )ap
{
_ap = ap;
}
- (NSInteger )ap
{
return _ap;
}
- (void)setHp:(NSInteger )hp
{
_hp = hp;
}
- (NSInteger )hp
{
return _hp;
}
@end
建一个student类
#import <Foundation/Foundation.h>
@interface Student :
NSObject
@property NSString *name;
@property NSInteger age;
@property NSInteger number;
@property NSString *gender;
@property CGFloat score;
- (instancetype)initWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score;
+ (instancetype)studentWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score;
@end
#import "Student.h"
@implementation Student
- (instancetype)initWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score
{
self = [super
init];//self继承父类super初始化
if (self) {//如果继承完成以下
self.name = name;
self.gender = gender;
self.number = number;
self.age = age;
self.score = score;
}
return
self;
}
+ (instancetype)studentWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score
{
Student *s = [[Student
alloc] initWithName:name
Gender:gender Number:number
Age:age Score:score];
return s;
}
@end
1.单向继承,一个类只能有一个父类,一个父类可以有多个子类.
2.单向继承,基类(根类)是OSObject
3.子类可以继承父类的属性和方法
当父类的方法不满足子类的需求时,子类可以重写父类的方法,重写父类的方法,在子类中不需要再次声明.
1.完全重写
2.部分重写 使用super
建立继承关系之后,子类可以继承父类的:
1.实例变量,@public@protected修饰情况之下
2.公开的方法
一个方法如果在.h中声明了,那么这个方法就是公开的方法,如果没有声明,则是私有的.
OC中没有绝对私有的方法.
子类调用方法的过程:
先在本类中寻找方法,如果找到就直接调用,如果没有找到就在其父类中寻找,如果在父类中也没有找到,就继续其次向上寻找(父类的父类),直到找到为止.
如果找不到就抛出异常,引起crash.
对象对对象可以赋值,对象就是指针.
自定义初始化方法不需要在.h文件中声明.因为是重写了该方法.
遍历构造器一定是一个类方法
返回值类型(id,instancetype)
都是声明变量 自定义减号需要声明变量 遍历构造器加号 不需要声明变量
自定义初始化方法:初始化+赋值.
便利构造器:开辟空间+初始化+赋值.
自定义初始化方法:
-(id)initWithColor:(NSString *)color
price:(NSString *)price{
if (self=[super init]) {
_price = price;
_color = color;
}
return self;
}
便利构造器:
+(id)appleWithColor:(NSString *)color
price:(NSString *)price{
Apple *apple=[[Apple alloc]initWithColor:color price:price];
return apple;
}
您好,写一个简单地便利构造器 +(Student *)studntWithName :(NSString *)aName andAge:(int)anAge { Student *stu=[Student alloc]initWithName :aName andAge:anAge return stu; } //1..h声明文件中 #import <Foundation/Foundation.h> @interface Person :NSObject { NSString * name;//名字 int age;//年龄 } @property (assign,nonatomic) NSString * name;//声明名字访问属性 @property (assign,nonatomic) int age;//声明年龄访问属性 -(id)initWith:(NSString *)newName withAge:(int)newAge;//声明构造函数 -(void)sayhi;//打招呼方式 @end //2..m实现文件中 @implementation Person @synthesize name;//实现name属性 @synthesize age;//实现age属性 -(id)initWith:(NSString *)newName withAge:(int)newAge;//实现构造函数 { self =[super init];//调用父类super init方法 if(self) { [self setName:newName];另一种调用方法 self.name:newName; [self setAge:newAge];//另一种调用方法 self.Age:newAge; } return self; /*构造函数一般书写方式: self =[super init]; if(self) { 函数体 } return self; */ } -(void)sayhi { NSLog(@"hi,My name is %@,I'm %d old!",name,age); } @end //3.主函数中 #import <Person.h> int main(int argc, const char * argv[]) { Person * xiaozhang=[[Person alloc]initWith:@"小张" withAge:25];//构造函数调用 [xiaozhang sayhi];//调用sayhi方法 return 0; }
写一个简单地便利构造器
+(Student *)studntWithName :(NSString *)aName andAge:(int)anAge { Student *stu=[Student alloc]initWithName :aName andAge:anAge return stu; } //1..h声明文件中 #import <Foundation/Foundation.h> @interface Person :NSObject { NSString * name;//名字 int age;//年龄 } @property (assign,nonatomic) NSString * name;//声明名字访问属性 @property (assign,nonatomic) int age;//声明年龄访问属性 -(id)initWith:(NSString *)newName withAge:(int)newAge;//声明构造函数 -(void)sayhi;//打招呼方式 @end //2..m实现文件中 @implementation Person @synthesize name;//实现name属性 @synthesize age;//实现age属性 -(id)initWith:(NSString *)newName withAge:(int)newAge;//实现构造函数 { self =[super init];//调用父类super init方法 if(self) { [self setName:newName];另一种调用方法 self.name:newName; [self setAge:newAge];//另一种调用方法 self.Age:newAge; } return self; /*构造函数一般书写方式: self =[super init]; if(self) { 函数体 } return self; */ } -(void)sayhi { NSLog(@"hi,My name is %@,I'm %d old!",name,age); } @end //3.主函数中 #import <Person.h> int main(int argc, const char * argv[]) { Person * xiaozhang=[[Person alloc]initWith:@"小张" withAge:25];//构造函数调用 [xiaozhang sayhi];//调用sayhi方法 return 0; }
//初始化方法
//形式:
//系统的:- (instanceType)init;
//自定义的: - (instanceType)initWithXXX(参数类型)参数名......
// Zombie *zombie = [[Zombie alloc]
// initWithName:@"wuye" hp:20 ap:2 speed:10];
// NSLog(@"%@", zombie.name);
//[zombie setName:@"wuye" ap:20 hp:2 speed:1];
//遍历构造器
//+ (instanceType)类名;
//+ (instanceType)类名WithXXX:(参数类型)参数......
BlockZombie *z = [BlockZombie
zombie];
z.name =
@"zahngyang";
自定义初始化方法
#import <Foundation/Foundation.h>
@interface Zombie :
NSObject
{
NSInteger _hp;//血量
NSInteger _ap;//攻击力
CGFloat _speed;//速度
NSString *_name;//名字
}
- (void)move;//移动
- (void)attack;//攻击
- (void)die;//死亡
-(void)setName:(NSString *)name;
- (NSString *)name;
- (void)setSpeed:(CGFloat )speed;
- (CGFloat )speed;
- (void)setAp:(NSInteger )ap;
- (NSInteger )ap;
- (void)setHp:(NSInteger )hp;
- (NSInteger )hp;
- (instancetype)initWithName:(NSString *)name;
- (instancetype)initWithName:(NSString *)name hp:(NSString *)hp ap:(NSInteger )ap speed:(CGFloat
)speed;
@end
#import "Zombie.h"
@implementation Zombie
//自定义初始化方法
- (instancetype)initWithName:(NSString *)name;
{
//1.调用从父类继承的init方法,作用:初始化从父类继承的成员变量
self = [super
init];
//2.self是否为空,
if (self) {
//3.如果self不是空,就给成员变量赋值
_name = name;
}
//4.
return self;
}
- (instancetype)initWithName:(NSString *)name hp:(NSInteger )hp ap:(NSInteger )ap speed:(CGFloat
)speed
{
self = [super
init];
if (self) {
_name = name;
_hp = hp;
_ap = ap;
_speed = speed;
}
return
self;
}
- (void)move
{
NSLog(@"fast");
}
- (void)attack
{
NSLog(@"strong");
}
- (void)die
{
NSLog(@"fast");
}
-(void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
- (void)setSpeed:(CGFloat )speed
{
_speed = speed;
}
- (CGFloat )speed
{
return _speed;
}
- (void)setAp:(NSInteger )ap
{
_ap = ap;
}
- (NSInteger )ap
{
return _ap;
}
- (void)setHp:(NSInteger )hp
{
_hp = hp;
}
- (NSInteger )hp
{
return _hp;
}
@end
建一个student类
#import <Foundation/Foundation.h>
@interface Student :
NSObject
@property NSString *name;
@property NSInteger age;
@property NSInteger number;
@property NSString *gender;
@property CGFloat score;
- (instancetype)initWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score;
+ (instancetype)studentWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score;
@end
#import "Student.h"
@implementation Student
- (instancetype)initWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score
{
self = [super
init];//self继承父类super初始化
if (self) {//如果继承完成以下
self.name = name;
self.gender = gender;
self.number = number;
self.age = age;
self.score = score;
}
return
self;
}
+ (instancetype)studentWithName:(NSString *)name Gender:(NSString *)gender Number:(NSInteger )number Age:(NSInteger
)age Score:(CGFloat )score
{
Student *s = [[Student
alloc] initWithName:name
Gender:gender Number:number
Age:age Score:score];
return s;
}
@end
相关文章推荐
- oracle 各种常用函数讲解
- 沟通的囧途
- 网络编程
- Spinner下拉列表组件的3种数据绑定方式
- mysql中文进行全文索引支持问题
- ioS开发之网络--网络基础概况
- 深入理解java异常处理机制
- poj 2251 简单搜索bfs
- AJAX基础
- Mini调试器捉虫记
- Linux内核中的Namespace
- 取得指定几个数中随机数
- 选取中文编码方案注意事项
- 树的最小支配集
- 欧拉路 / 回路 / 有向/ 无向 / 字典顺序
- DP 最长公共子序列并标记输出
- 多重背包问题的二进制分解思想
- Processing 教程(10)- 多个对象的行为
- Java中的await()/signal()用法
- objective-c数据类型