您的位置:首页 > 其它

音乐家演奏乐器

2015-07-29 19:50 351 查看
Main.m

#import <Foundation/Foundation.h>
#import "Piano.h"
#import "Violin.h"
#import "Actor.h"

/*
乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin)。

各种乐器的弹奏( play )方法各不同样。演奏家能够使用各种乐器。

*/

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

//创建演奏者和乐器
Actor *actor = [[Actor alloc] init];
Piano *piano = [[Piano alloc] init];
Violin *violin = [[Violin alloc] init];

int cmd;

NSLog(@"请输入须要使用的乐器:");

scanf("%d",&cmd);

if (cmd == 1) {
[actor playWithInstrument:piano];
}else if (cmd == 2) {
[actor playWithInstrument:violin];
}else {
NSLog(@"输入有误");
}

return 0;


Actor.h

//使用乐器演奏的方法
- (void)playWithInstrument:(Instrument *)instrument;


Actor.m


//使用乐器演奏的方法
- (void)playWithInstrument:(Instrument *)instrument {

[instrument play];

}


Instrument.h

@interface Instrument : NSObject

//演奏
- (void)play;


Instrument.m


//演奏
- (void)play {

NSLog(@"乐器演奏");

}


Piano.m

//演奏
- (void)play
{

NSLog(@"钢琴演奏");

}
Violin.m

//演奏
- (void)play {

NSLog(@"小提琴演奏");

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