OC中的多继承

时间:2021-09-04 06:20:01

可以间接实现,方法有:

1、消息转发

2、协议

3、组合模式

4、代理

5、分类

直接上code,分别说明集中方法的实现

一、消息转发

消息转发可以参考我的另外一篇博客:http://www.cnblogs.com/bigshow1949/p/6109798.html

首页创建一个Doctor类

 #import <Foundation/Foundation.h>

 @interface Doctor : NSObject

 - (void)operate;

 - (void)operate2;

 - (void)operate3;
@end
 @implementation Doctor

 - (void)operate {
NSLog(@"Doctor - operate");
} - (void)operate2 {
NSLog(@"Doctor - operate2");
} - (void)operate3 {
NSLog(@"Doctor - operate3");
} @end

再创建一个Teacher类:

#import "Teacher.h"
#import "Doctor.h" @implementation Teacher - (id)forwardingTargetForSelector:(SEL)aSelector
{
Doctor *doctor = [[Doctor alloc]init];
if ([doctor respondsToSelector:aSelector]) {
return doctor;
}
return nil;
}
@end

在控制器里实现

    // 多继承 消息转发
Teacher *teacher = [[Teacher alloc] init];
[teacher performSelector:@selector(operate)];

二、协议

三、组合模式

四、代理

五、分类

参考:http://blog.csdn.net/yiyaaixuexi/article/details/8970734

参考:http://www.jianshu.com/p/2e9382be43bf