Objective-C武器类练习

 

main.m


#import <Foundation/Foundation.h>

#import "Arm.h"

#import "Arm1.h"

#import "Gun.h"

#import "Knife.h"

#import "Boom.h"

#import "Sound.h"

#import "NPC.h"


int main(int argc, const char * argv[])

{


    @autoreleasepool {

        //分别定义三种不同的武器,输出各自的武器编号(静态变量),并实现各自重写的方法(方法重写)

        //每调用一次init方法,count静态变量值加一,并赋给各自的实例变量number

        //gun引用了Sound协议,可以使用协议里的play方法

        Gun<Sound> *gun = [[Gun alloc]init];

        //前面用@property@synthesize属性方法实现了各实例变量的getset方法,这里可以直接使用点语法,看起来更简单明了

        gun.name = @"ak47";

        //输出武器编号

        NSLog(@"%@的编号为 %d\n",gun.name,gun.number);

        //调用Gun类重写的movToX:ToY:fireToX:ToY:方法

        [gun movToX:10 ToY:20];

        [gun fireToX:20 ToY:20];

        [gun play];

        //ak47正在播放抢的声音!

        //Gun类和父类Arm类都实现了Sound协议中的play方法,gun调用的是自己实现的play方法,说明协议中的方法也可以重写

        

        Knife *k1 = [[Knife alloc]init];

        k1.name = @"xiaoli";

        NSLog(@"%@的编号为 %d\n",k1.name,k1.number);

        [k1 movToX:30 ToY:30];

        [k1 fireToX:40 ToY:40];

        [k1 play];

        //xiaoli正在唱歌

        //Knife的父类Arm引用了Sound协议,Knife的对象也可以使用协议的方法,说明协议也具有继承性?

        

        Boom *b1 = [[Boom alloc]init];

        b1.name = @"c4";

         NSLog(@"%@的编号为 %d\n",b1.name,b1.number);

        [b1 movToX:50 ToY:50];

        [b1 fireToX:60 ToY:60];

        //arm对象引用了Sound协议,可以调用Sound协议里面的方法

        Arm *arm = [[Arm alloc]init];//动态绑定的实现

        arm.name = @"wuqi";

         NSLog(@"%@的编号为 %d\n",arm.name,arm.number);

        //wuqi的编号为 4

        arm = gun;

         NSLog(@"%@的编号为 %d\n",arm.name,arm.number);

        //ak47的编号为 1

        //(动态绑定)实例对象被确定为gun,就拥有了gun所拥有的所有属性,

        [arm movToX:70 ToY:70];

        //ak47正在移动到70,70

        [arm fireToX:80 ToY:80];

        //ak47正在开火到80,80

        [arm play];

        //ak47正在播放抢的声音!

        

        

        arm = k1;

        NSLog(@"%@的编号为 %d\n",arm.name,arm.number);

        //xiaoli的编号为 2

        //

        [arm movToX:70 ToY:70];

        //xiaoli正在移动到70,70

        [arm fireToX:80 ToY:80];

        //xiaoli正在飞刀到80,80

        [arm play];

        //xiaoli正在唱歌

        

        

        arm = b1;

        NSLog(@"%@的编号为 %d\n",arm.name,arm.number);

        //c4的编号为 3

        //

        [arm movToX:70 ToY:70];

        //c4正在移动到70,70

        [arm fireToX:80 ToY:80];

        //c4正在投掷手雷到80,80

        [arm play];

        //c4正在唱歌

        

        

        id<Sound> a1;//

        a1 = gun;

        [a1 play];

        //ak47正在播放抢的声音!

        //协议也具有动态绑定特性

//        Arm *a12 = [[Arm1 alloc]init];

//        a12.name =@"abc";

//        a1 =a12;

//        [a1 play];

        //Arm1中没有引用Sound协议和实现协议中的方法,所以在这里不能使用协议里的play方法

        

        NPC * p1 = [[NPC alloc]init];

        p1.name = @"music";

        [p1 play];

        //music正在播放中国解放军进行曲!

        //同一种协议在不同的类中可以有不同的实现

        

    }

    return 0;

}




Arm.h


#import <Foundation/Foundation.h>

#import "Sound.h"


@interface Arm : NSObject<Sound>

{

    NSString * name;//名字

    int price;//价格

    int act;//攻击力

    int number;//武器编号

    int area;//攻击范围

}

@property NSString* name;

@property int price,act,number,area;


-(id)init;

//方法

-(void)movToX:(int)x ToY:(int)y;//移动 x,y

-(void)fireToX:(int)x ToY:(int)y;//攻击 x,y


@end



Arm.m


#import "Arm.h"

static int count;//定义为静态变量

@implementation Arm


@synthesize name;

@synthesize price,act,number,area;

//方法

-(id)init{

    if (self = [super init]) {

        number = ++count;

    }else{return nil;}

    return self;

}


-(void)movToX:(int)x ToY:(int)y{

    NSLog(@"%@正在移动到%d,%d",self.name,x,y);

}


-(void)fireToX:(int)x ToY:(int)y{

    NSLog(@"%@正在攻击到%d,%d",self.name,x,y);

}

//Arm类引用了Sound协议,在.m文件中必须实现协议中的方法

-(void)play{

    NSLog(@"%@正在唱歌",self.name);

}

@end



Gun.h

#import "Arm.h"


@interface Gun : Arm<Sound>

{

    int gunNUmber;//子弹数目

}

@property int gunNumber;


-(void)fireToX:(int)x ToY:(int)y;//开火

@end




Gun.m


#import "Gun.h"


@implementation Gun


@synthesize gunNumber;


-(void)fireToX:(int)x ToY:(int)y{

  NSLog(@"%@正在开火到%d,%d",self.name,x,y);  

}

-(void)play{

    NSLog(@"%@正在播放抢的声音!",self.name);

}


@end



Knife.h


#import "Arm.h"


@interface Knife : Arm

{

    NSString * color;//刀的颜色

}

@property NSString* color;

-(void)fireToX:(int)x ToY:(int)y;//飞刀


@end



Knife.m


#import "Knife.h"


@implementation Knife


@synthesize color;


-(void)fireToX:(int)x ToY:(int)y{

    NSLog(@"%@正在飞刀到%d,%d",self.name,x,y);

}


@end



Boom.h



#import "Arm.h"


@interface Boom : Arm

{

    int boomArea;// 爆炸范围

}

@property int boomArea;


-(void)fireToX:(int)x ToY:(int)y;//爆炸

@end



Boom.m


#import "Boom.h"


@implementation Boom


@synthesize boomArea;


-(void)fireToX:(int)x ToY:(int)y{

    NSLog(@"%@正在投掷手雷到%d,%d",self.name,x,y);

}

@end




Arm1.h


#import <Foundation/Foundation.h>


@interface Arm1 : NSObject

{

    NSString *name;

}

@property NSString *name;

-(id)init;

@end



Arm1.m


#import "Arm1.h"


@implementation Arm1

@synthesize name;

-(id)init{

    if (self = [super init]) {

      self.name = @"a11";

    }else{return nil;}

    return self;

}

@end




NPC.h

#import <Foundation/Foundation.h>

#import "Sound.h"


@interface NPC : NSObject<Sound>

{

    NSString * name;

}

@property NSString * name;

@end



NPC.m


#import "NPC.h"


@implementation NPC


@synthesize name;


-(void)play{

    NSLog(@"%@正在播放中国解放军进行曲!”",self.name);

}

@end



Sound.h


#import <Foundation/Foundation.h>


@protocol Sound <NSObject>

-(void)play;

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值