#import "ViewController.h"
#import "Programmer.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Programmer * grammer = [Programmer new];
[grammer program];
[grammer draw];
[grammer sing];
Student * student = [Student new];
[student draw];
[student sing];
}
#import <Foundation/Foundation.h>
// 编程技能
@protocol Program <NSObject>
- (void)program;
@end
// 绘画技能
@protocol Draw <NSObject>
- (void)draw;
@end
// 歌唱技能
@protocol Sing <NSObject>
- (void)sing;
@end
// 原本一个什么也不会的程序员
// 学会了多个技能
@interface Programmer : NSObject <Program, Draw, Sing>
// 继承的协议方法自动公有,无须声明接口
@end
#import "Programmer.h"
// 需要自行实现协议方法
@implementation Programmer
- (void)program {
NSLog(@"I'm writing bugs!");
}
- (void)draw {
NSLog(@"I can draw");
}
- (void)sing {
NSLog(@"I can sing");
}
@end
#import "Programmer.h"
@interface Student : Programmer
@end
#import "Student.h"
@implementation Student
- (void)draw {
NSLog(@"Student can draw");
}
- (void)sing {
NSLog(@"Student can sing");
}
@end
网友评论