可以间接实现,方法有:
1、消息转发
2、协议
3、组合模式
4、代理
5、分类
直接上code,分别说明集中方法的实现
一、消息转发
消息转发可以参考我的另外一篇博客:http://www.cnblogs.com/bigshow1949/p/6109798.html
首页创建一个Doctor类
1 #import <Foundation/Foundation.h> 2 3 @interface Doctor : NSObject 4 5 - (void)operate; 6 7 - (void)operate2; 8 9 - (void)operate3; 10 @end
1 @implementation Doctor 2 3 - (void)operate { 4 NSLog(@"Doctor - operate"); 5 } 6 7 - (void)operate2 { 8 NSLog(@"Doctor - operate2"); 9 } 10 11 - (void)operate3 { 12 NSLog(@"Doctor - operate3"); 13 } 14 15 @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)];
二、协议
三、组合模式
四、代理
五、分类