音乐家演奏乐器

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值