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屬性方法實現了各實例變量的get和set方法,這裏可以直接使用點語法,看起來更簡單明瞭
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