音乐家演奏乐器

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(@"小提琴演奏");
    
}



版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4687009.html