码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-29 14:00:12      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:


//初始化和遍历构造器使用

//person.h文件
1
#import <Foundation/Foundation.h> 2 3 @interface Person : NSObject 4 5 { 6 7 NSString *_name; 8 int _age; 9 NSString *_sex; 10 11 12 } 13 14 +(id)PersonWithName:(NSString*)name age:(int)age; 15 -(id)initWithName:(NSString*)name age:(int)age; 16 17 -(void)setName:(NSString*)name; 18 -(NSString*)name; 19 20 -(void)setAge:(int)age; 21 -(int)age; 22 23 -(void)setSex:(NSString*)sex; 24 -(NSString *)sex; 25 26 27 +(id)PersonWithName:(NSString *)name; 28 -(id)initWithName:(NSString*)name; 29 30 @end

 //person.m文件
1
#import "Person.h" 2 3 @implementation Person 4 5 +(id)PersonWithName:(NSString*)name age:(int)age 6 { 7 Person *p = [[Person alloc] initWithName:(NSString*)name age:age]; 8 return p; 9 10 } 11 //完整的初始化方法 12 13 -(id)initWithName:(NSString*)name age:(int)age 14 { 15 self = [super init]; 16 if (self) { 17 _name = name; 18 _age = age; 19 } 20 21 return self; 22 23 } 24 25 +(id)PersonWithName:(NSString *)name 26 { 27 28 Person *p2 = [[Person alloc] initWithName:name]; 29 return p2; 30 31 32 } 33 34 35 -(id)initWithName:(NSString*)name 36 { 37 self = [super init]; 38 if (self) { 39 _name = name; 40 } 41 42 43 return self; 44 45 46 } 47 48 49 50 51 52 -(void)setName:(NSString*)name 53 { 54 55 _name = name; 56 57 } 58 -(NSString*)name 59 { 60 61 return _name; 62 } 63 64 -(void)setAge:(int)age 65 { 66 _age = age; 67 68 69 } 70 -(int)age 71 { 72 73 return _age; 74 } 75 76 -(void)setSex:(NSString*)sex 77 { 78 79 _sex = sex; 80 81 } 82 -(NSString *)sex 83 { 84 return _sex; 85 86 87 } 88 89 90 91 @end

  //main.m文件
1
#import <Foundation/Foundation.h> 2 3 #import "Person.h" 4 5 int main(int argc, const char * argv[]) { 6 @autoreleasepool { 7 8 //注意在使用遍历构造器时,要和初始化方法结合到一起才能使用 9 //类方法只有类才可以调用 10 Person *p1 = [Person PersonWithName:@"小明" age:12]; 11 NSLog(@"%@ %d",[p1 name],[p1 age]); 12 13 //注意在使用遍历构造器时,要和初始化方法结合到一起才能使用 14 Person *p2 = [Person PersonWithName:@"小辉"]; 15 NSLog(@"%@ ",[p2 name]); 16 17 18 19 20 21 } 22 return 0; 23 }

 

 

 

 

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

Zombie.h(普通僵尸)

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Zombie : NSObject
 4 
 5 {
 6     NSString *_type;   //僵尸种类
 7     NSInteger _blood;  //总血量
 8     NSInteger _restBlood; //剩余血量
 9     NSInteger _lostBlood;  //失血量
10     BOOL _isDeath;  //是否死亡
11     
12     
13     
14 }
15 
16 
17 //初始化方法(总血量)
18 -(id)initWithType:(NSString*)type
19             blood:(NSInteger)blood
20         restBlood:(NSInteger)restBlood
21         lostBlood:(NSInteger)lostBlood
22           isDeath:(BOOL)isDeath;
23 
24 //遍历构造器
25 +(id)zombleWithType:(NSString*)type
26                     blood:(NSInteger)blood
27                 lostBlood:(NSInteger)loatBlood;
28 
29 
30 
31 //赋值
32 -(void)setIsdeath:(BOOL)isDeath;
33 
34 //取值
35 -(NSString*)type;
36 -(NSInteger)blood;
37 -(NSInteger)lostBlood;
38 -(BOOL)isDeath;
39 
40 //被打失血
41 -(void)attack;
42 
43 //死亡
44 -(void)dead;
45 
46 
47 
48 
49 @end

//Zombie.m(普通僵尸)
1
#import "Zombie.h" 2 3 @implementation Zombie 4 //初始化 5 -(id)initWithType:(NSString*)type 6 blood:(NSInteger)blood 7 restBlood:(NSInteger)restBlood 8 lostBlood:(NSInteger)lostBlood 9 isDeath:(BOOL)isDeath 10 { 11 12 if(self = [super init]) 13 { 14 _type = type; 15 _blood = blood; 16 _restBlood = restBlood; 17 _lostBlood = lostBlood; 18 _isDeath = isDeath; 19 20 } 21 return self; 22 23 } 24 25 //遍历构造器 26 +(id)zombleWithType:(NSString*)type 27 blood:(NSInteger)blood 28 lostBlood:(NSInteger)lostBlood 29 { 30 31 return [[Zombie alloc] initWithType:type blood:blood restBlood:blood lostBlood:lostBlood isDeath:NO]; 32 33 } 34 35 36 -(void)setIsdeath:(BOOL)isDeath 37 { 38 39 _isDeath = isDeath; 40 } 41 42 43 //取值 44 -(NSString*)type 45 { 46 return _type; 47 48 } 49 -(NSInteger)blood 50 { 51 return _blood; 52 53 } 54 -(NSInteger)lostBlood 55 { 56 57 return _lostBlood; 58 } 59 -(BOOL)isDeath 60 { 61 return _isDeath; 62 63 } 64 65 66 67 68 //被打击失血 69 -(void)attack 70 { 71 if(_restBlood <=_lostBlood){ 72 _isDeath = YES; 73 [self dead]; 74 75 } else { 76 NSLog(@"普通僵尸每次失去血量:%ld",_lostBlood); 77 _restBlood -= _lostBlood; 78 NSLog(@"普通僵尸剩余血量:%ld",_restBlood); 79 80 } 81 } 82 83 //死亡 84 -(void)dead 85 { 86 NSLog(@"....普通僵尸已死亡!!!"); 87 88 } 89 90 91 92 @end

ArmorZombie.文件(路障僵尸)

 1 #import <Foundation/Foundation.h>
 2 
 3 #import "Zombie.h"
 4 
 5 @interface ArmorZombie : Zombie
 6 
 7 {
 8     
 9     NSString* _armorType; //防具类型
10     BOOL _isArmorDrop;  //防具是否掉落
11     
12     
13 }
14 
15 //初始化
16 -(id)initWithType:(NSString*)type
17             blood:(NSInteger)blood
18         lostBlood:(NSInteger)lostBlood
19           isDeath:(BOOL)isDeath
20         armorType:(NSString*)armorType;
21 
22 
23 //遍历构造器
24 +(id)armorZombieType:(NSString*)type
25                     blood:(NSInteger)blood
26                 lostBlood:(NSInteger)lostBlood
27                 armorType:(NSString*)armorType;
28 
29 
30 //赋值
31 -(void)setlostBlood:(NSInteger)lostBlood;
32 
33 //取值
34 -(NSString*)type;
35 -(NSInteger)blood;
36 -(NSInteger)lostBlood;
37 -(NSString*)armorType;
38 -(BOOL)isArmorDrop;
39 
40 //被打失血
41 -(void)attack;
42 
43 //防具被打烂
44 -(void)armorBreak;
45 
46 //死亡
47 -(void)dead;
48 
49 
50 @end

ArmorZombie.m 文件(路障僵尸)

  1 #import "ArmorZombie.h"
  2 
  3 @implementation ArmorZombie
  4 //初始化
  5 -(id)initWithType:(NSString*)type
  6             blood:(NSInteger)blood
  7         lostBlood:(NSInteger)lostBlood
  8           isDeath:(BOOL)isDeath
  9         armorType:(NSString*)armorType
 10 {
 11     self = [super initWithType:type blood:blood restBlood:blood lostBlood:lostBlood isDeath:NO];
 12     
 13     if(self){
 14         
 15         _armorType = armorType;
 16         _isArmorDrop = NO;
 17         
 18     }
 19     return self;
 20     
 21 }
 22 
 23 
 24 //遍历构造器
 25 +(id)armorZombieType:(NSString*)type
 26                        blood:(NSInteger)blood
 27                    lostBlood:(NSInteger)lostBlood
 28                    armorType:(NSString*)armorType;
 29 {
 30     return [[ArmorZombie alloc]initWithType:type blood:blood restBlood:blood lostBlood:lostBlood isDeath:NO];
 31     
 32     
 33     
 34 }
 35 
 36 
 37 //赋值
 38 -(void)setlostBlood:(NSInteger)lostBlood
 39 {
 40     _lostBlood = lostBlood;
 41 
 42 }
 43 
 44 //取值
 45 -(NSString*)type
 46 {
 47     return _type;
 48 }
 49 -(NSInteger)blood
 50 {
 51     return  _blood;
 52 
 53 }
 54 -(NSInteger)lostBlood
 55 {
 56     return _lostBlood;
 57 
 58 }
 59 -(NSString*)armorType
 60 {
 61     return _armorType;
 62 
 63 }
 64 -(BOOL)isArmorDrop
 65 {
 66  return _isArmorDrop;
 67 
 68 }
 69 
 70 //被打失血
 71 -(void)attack
 72 {
 73     static int flag = 1;
 74     if (_restBlood <= _blood/2&&1==flag) {
 75         _isArmorDrop = YES;
 76         [self armorBreak];
 77         [self setlostBlood:3];
 78         flag = 0;
 79     }
 80     if (_restBlood <= _lostBlood) {
 81          _isDeath = YES;
 82           [self dead];
 83     }else {
 84         NSLog(@"路障僵尸每次失血量:%ld",_lostBlood);
 85         _restBlood -= _lostBlood;
 86         NSLog(@"路障僵尸剩余血量:%ld",_restBlood);
 87     
 88     }
 89 }
 90 
 91 //防具被打烂
 92 -(void)armorBreak
 93 {
 94     NSLog(@"路障掉落.....");
 95     
 96 }
 97 
 98 //死亡
 99 -(void)dead
100 {
101     NSLog(@".....路障僵尸已死亡!!!");
102 
103 
104 }
105 
106 
107 
108 
109 
110 
111 
112 @end

IronZombie.h文件(路障僵尸)

 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 #import "ArmorZombie.h"
 5 
 6 @interface IronZombie : ArmorZombie
 7 
 8 {
 9     
10     NSString*_weakness; //弱点
11     
12     
13     
14     
15 }
16 
17 //初始化
18 -(id)initWithType:(NSString*)type
19             blood:(NSInteger)blood
20         lostBlood:(NSInteger)lostBlood
21           isDeath:(BOOL)isDeath
22         armorType:(NSString*)armorType
23          weakness:(NSString*)weakness;
24 
25 
26 //遍历构造器
27 +(id)ironZombieType:(NSString*)type
28                blood:(NSInteger)blood
29            lostBlood:(NSInteger)lostBlood
30            armorType:(NSString*)armorType
31            weakness:(NSString*)weakness;
32 
33 
34 
35 
36 //取值
37 -(NSString*)type;
38 -(NSInteger)blood;
39 -(NSInteger)lostBlood;
40 -(NSString*)armorType;
41 -(NSString*)weakness;
42 
43 //被打失血
44 -(void)attack;
45 
46 //防具被打烂
47 -(void)armorBreak;
48 
49 //防具被磁铁吸走
50 -(void)absorb;
51 
52 //死亡
53 -(void)dead;
54 
55 @end

IronZombie.m文件(路障僵尸)

 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 #import "ArmorZombie.h"
 5 
 6 @interface IronZombie : ArmorZombie
 7 
 8 {
 9     
10     NSString*_weakness; //弱点
11     
12     
13     
14     
15 }
16 
17 //初始化
18 -(id)initWithType:(NSString*)type
19             blood:(NSInteger)blood
20         lostBlood:(NSInteger)lostBlood
21           isDeath:(BOOL)isDeath
22         armorType:(NSString*)armorType
23          weakness:(NSString*)weakness;
24 
25 
26 //遍历构造器
27 +(id)ironZombieType:(NSString*)type
28                blood:(NSInteger)blood
29            lostBlood:(NSInteger)lostBlood
30            armorType:(NSString*)armorType
31            weakness:(NSString*)weakness;
32 
33 
34 
35 
36 //取值
37 -(NSString*)type;
38 -(NSInteger)blood;
39 -(NSInteger)lostBlood;
40 -(NSString*)armorType;
41 -(NSString*)weakness;
42 
43 //被打失血
44 -(void)attack;
45 
46 //防具被打烂
47 -(void)armorBreak;
48 
49 //防具被磁铁吸走
50 -(void)absorb;
51 
52 //死亡
53 -(void)dead;
54 
55 
56 
57 @end

 

main.m文件(调用)

 1 #import <Foundation/Foundation.h>
 2 
 3 #import "Zombie.h"
 4 
 5 #import "ArmorZombie.h"
 6 
 7 #import "IronZombie.h"
 8 
 9 
10 int main(int argc, const char * argv[]) {
11     @autoreleasepool {
12         
13       Zombie *ptjs = [Zombie zombleWithType:@"普通僵尸" blood:50 lostBlood:3];
14         int i=0;
15         while (![ptjs isDeath])
16         {
17             if(![ptjs isDeath])
18             {
19                 [ptjs attack]; //攻击普通僵尸
20             }
21             i++;
22             
23         }
24         
25         NSLog(@"--------------------------------");
26         
27         
28         ArmorZombie *lzjs = [ArmorZombie armorZombieType:@"防具僵尸" blood:80 lostBlood:2 armorType:@"路障防具"];
29          i=0;
30         while (![lzjs isDeath])
31         {
32             if(![lzjs isDeath])
33             {
34                 [lzjs attack]; //攻击普通僵尸
35             }
36             i++;
37 
38         }
39         
40         NSLog(@"--------------------------------");
41 
42         
43 
44     IronZombie *ttjs = [IronZombie  ironZombieType:@"铁桶僵尸"  blood:120  lostBlood:1 armorType:@"铁桶防具" weakness:@"铁桶被吸走"];
45          i=0;
46         while (![ttjs isDeath])
47         {
48             if(![ttjs isDeath])
49             {
50                 [ttjs attack]; //攻击普通僵尸
51             }
52             i++;
53             
54         }
55      
56     }
57     return 0;
58 }

 

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

标签:

原文地址:http://www.cnblogs.com/erdeng/p/4769045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!