您的位置:首页 > 编程语言

第三讲.继承,完整初始化方法,遍历构造器,多态(代码) 另附植物大战僵尸练习

2015-08-29 13:33 381 查看
//初始化和遍历构造器使用
//person.h文件
1#import<Foundation/Foundation.h>

@interfacePerson:NSObject

{

NSString*_name;
int_age;
NSString*_sex;

}

+(id)PersonWithName:(NSString*)nameage:(int)age;
-(id)initWithName:(NSString*)nameage:(int)age;

-(void)setName:(NSString*)name;
-(NSString*)name;

-(void)setAge:(int)age;
-(int)age;

-(void)setSex:(NSString*)sex;
-(NSString*)sex;

+(id)PersonWithName:(NSString*)name;
-(id)initWithName:(NSString*)name;

@end


//person.m文件
1#import"Person.h"

@implementationPerson

+(id)PersonWithName:(NSString*)nameage:(int)age
{
Person*p=[[Personalloc]initWithName:(NSString*)nameage:age];
returnp;

}
//完整的初始化方法

-(id)initWithName:(NSString*)nameage:(int)age
{
self=[superinit];
if(self){
_name=name;
_age=age;
}

returnself;

}

+(id)PersonWithName:(NSString*)name
{

Person*p2=[[Personalloc]initWithName:name];
returnp2;

}

-(id)initWithName:(NSString*)name
{
self=[superinit];
if(self){
_name=name;
}

returnself;

}

-(void)setName:(NSString*)name
{

_name=name;

}
-(NSString*)name
{

return_name;
}

-(void)setAge:(int)age
{
_age=age;

}
-(int)age
{

return_age;
}

-(void)setSex:(NSString*)sex
{

_sex=sex;

}
-(NSString*)sex
{
return_sex;

}

@end


//main.m文件
1#import<Foundation/Foundation.h>

#import"Person.h"

intmain(intargc,constchar*argv[]){
@autoreleasepool{

//注意在使用遍历构造器时,要和初始化方法结合到一起才能使用
//类方法只有类才可以调用
Person*p1=[PersonPersonWithName:@"小明"age:12];
NSLog(@"%@%d",[p1name],[p1age]);

//注意在使用遍历构造器时,要和初始化方法结合到一起才能使用
Person*p2=[PersonPersonWithName:@"小辉"];
NSLog(@"%@",[p2name]);

}
return0;
}










oc中植物大战僵尸练习(用继承的方法写)

Zombie.h(普通僵尸)

#import<Foundation/Foundation.h>

@interfaceZombie:NSObject

{
NSString*_type;//僵尸种类
NSInteger_blood;//总血量
NSInteger_restBlood;//剩余血量
NSInteger_lostBlood;//失血量
BOOL_isDeath;//是否死亡

}

//初始化方法(总血量)
-(id)initWithType:(NSString*)type
blood:(NSInteger)blood
restBlood:(NSInteger)restBlood
lostBlood:(NSInteger)lostBlood
isDeath:(BOOL)isDeath;

//遍历构造器
+(id)zombleWithType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)loatBlood;

//赋值
-(void)setIsdeath:(BOOL)isDeath;

//取值
-(NSString*)type;
-(NSInteger)blood;
-(NSInteger)lostBlood;
-(BOOL)isDeath;

//被打失血
-(void)attack;

//死亡
-(void)dead;

@end



//Zombie.m(普通僵尸)
1#import"Zombie.h"

@implementationZombie
//初始化
-(id)initWithType:(NSString*)type
blood:(NSInteger)blood
restBlood:(NSInteger)restBlood
lostBlood:(NSInteger)lostBlood
isDeath:(BOOL)isDeath
{

if(self=[superinit])
{
_type=type;
_blood=blood;
_restBlood=restBlood;
_lostBlood=lostBlood;
_isDeath=isDeath;

}
returnself;

}

//遍历构造器
+(id)zombleWithType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
{

return[[Zombiealloc]initWithType:typeblood:bloodrestBlood:bloodlostBlood:lostBloodisDeath:NO];

}

-(void)setIsdeath:(BOOL)isDeath
{

_isDeath=isDeath;
}

//取值
-(NSString*)type
{
return_type;

}
-(NSInteger)blood
{
return_blood;

}
-(NSInteger)lostBlood
{

return_lostBlood;
}
-(BOOL)isDeath
{
return_isDeath;

}

//被打击失血
-(void)attack
{
if(_restBlood<=_lostBlood){
_isDeath=YES;
[selfdead];

}else{
NSLog(@"普通僵尸每次失去血量:%ld",_lostBlood);
_restBlood-=_lostBlood;
NSLog(@"普通僵尸剩余血量:%ld",_restBlood);

}
}

//死亡
-(void)dead
{
NSLog(@"....普通僵尸已死亡!!!");

}

@end


ArmorZombie.文件(路障僵尸)

#import<Foundation/Foundation.h>

#import"Zombie.h"

@interfaceArmorZombie:Zombie

{

NSString*_armorType;//防具类型
BOOL_isArmorDrop;//防具是否掉落

}

//初始化
-(id)initWithType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
isDeath:(BOOL)isDeath
armorType:(NSString*)armorType;

//遍历构造器
+(id)armorZombieType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
armorType:(NSString*)armorType;

//赋值
-(void)setlostBlood:(NSInteger)lostBlood;

//取值
-(NSString*)type;
-(NSInteger)blood;
-(NSInteger)lostBlood;
-(NSString*)armorType;
-(BOOL)isArmorDrop;

//被打失血
-(void)attack;

//防具被打烂
-(void)armorBreak;

//死亡
-(void)dead;

@end


ArmorZombie.m文件(路障僵尸)

#import"ArmorZombie.h"

@implementationArmorZombie
//初始化
-(id)initWithType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
isDeath:(BOOL)isDeath
armorType:(NSString*)armorType
{
self=[superinitWithType:typeblood:bloodrestBlood:bloodlostBlood:lostBloodisDeath:NO];

if(self){

_armorType=armorType;
_isArmorDrop=NO;

}
returnself;

}

//遍历构造器
+(id)armorZombieType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
armorType:(NSString*)armorType;
{
return[[ArmorZombiealloc]initWithType:typeblood:bloodrestBlood:bloodlostBlood:lostBloodisDeath:NO];

}

//赋值
-(void)setlostBlood:(NSInteger)lostBlood
{
_lostBlood=lostBlood;

}

//取值
-(NSString*)type
{
return_type;
}
-(NSInteger)blood
{
return_blood;

}
-(NSInteger)lostBlood
{
return_lostBlood;

}
-(NSString*)armorType
{
return_armorType;

}
-(BOOL)isArmorDrop
{
return_isArmorDrop;

}

//被打失血
-(void)attack
{
staticintflag=1;
if(_restBlood<=_blood/2&&1==flag){
_isArmorDrop=YES;
[selfarmorBreak];
[selfsetlostBlood:3];
flag=0;
}
if(_restBlood<=_lostBlood){
_isDeath=YES;
[selfdead];
}else{
NSLog(@"路障僵尸每次失血量:%ld",_lostBlood);
_restBlood-=_lostBlood;
NSLog(@"路障僵尸剩余血量:%ld",_restBlood);

}
}

//防具被打烂
-(void)armorBreak
{
NSLog(@"路障掉落.....");

}

//死亡
-(void)dead
{
NSLog(@".....路障僵尸已死亡!!!");

}

@end


IronZombie.h文件(路障僵尸)

#import<Foundation/Foundation.h>

#import"ArmorZombie.h"

@interfaceIronZombie:ArmorZombie

{

NSString*_weakness;//弱点

}

//初始化
-(id)initWithType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
isDeath:(BOOL)isDeath
armorType:(NSString*)armorType
weakness:(NSString*)weakness;

//遍历构造器
+(id)ironZombieType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
armorType:(NSString*)armorType
weakness:(NSString*)weakness;

//取值
-(NSString*)type;
-(NSInteger)blood;
-(NSInteger)lostBlood;
-(NSString*)armorType;
-(NSString*)weakness;

//被打失血
-(void)attack;

//防具被打烂
-(void)armorBreak;

//防具被磁铁吸走
-(void)absorb;

//死亡
-(void)dead;

@end


IronZombie.m文件(路障僵尸)

#import<Foundation/Foundation.h>

#import"ArmorZombie.h"

@interfaceIronZombie:ArmorZombie

{

NSString*_weakness;//弱点

}

//初始化
-(id)initWithType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
isDeath:(BOOL)isDeath
armorType:(NSString*)armorType
weakness:(NSString*)weakness;

//遍历构造器
+(id)ironZombieType:(NSString*)type
blood:(NSInteger)blood
lostBlood:(NSInteger)lostBlood
armorType:(NSString*)armorType
weakness:(NSString*)weakness;

//取值
-(NSString*)type;
-(NSInteger)blood;
-(NSInteger)lostBlood;
-(NSString*)armorType;
-(NSString*)weakness;

//被打失血
-(void)attack;

//防具被打烂
-(void)armorBreak;

//防具被磁铁吸走
-(void)absorb;

//死亡
-(void)dead;

@end


main.m文件(调用)

#import<Foundation/Foundation.h>

#import"Zombie.h"

#import"ArmorZombie.h"

#import"IronZombie.h"

intmain(intargc,constchar*argv[]){
@autoreleasepool{

Zombie*ptjs=[ZombiezombleWithType:@"普通僵尸"blood:50lostBlood:3];
inti=0;
while(![ptjsisDeath])
{
if(![ptjsisDeath])
{
[ptjsattack];//攻击普通僵尸
}
i++;

}

NSLog(@"--------------------------------");

ArmorZombie*lzjs=[ArmorZombiearmorZombieType:@"防具僵尸"blood:80lostBlood:2armorType:@"路障防具"];
i=0;
while(![lzjsisDeath])
{
if(![lzjsisDeath])
{
[lzjsattack];//攻击普通僵尸
}
i++;

}

NSLog(@"--------------------------------");

IronZombie*ttjs=[IronZombieironZombieType:@"铁桶僵尸"blood:120lostBlood:1armorType:@"铁桶防具"weakness:@"铁桶被吸走"];
i=0;
while(![ttjsisDeath])
{
if(![ttjsisDeath])
{
[ttjsattack];//攻击普通僵尸
}
i++;

}

}
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: