我有两节课。我在其中定义了一些方法。我需要在一个类中从这两个类中获取这些方法,而无需重新定义。Objective C 不支持多重继承,那么我该如何实现呢?
			
			4254 次
		
2 回答
            3        
        
		
不要使用继承,使用组合。
#import "classname1.h"
#import "classname2.h"
@implementation classname3
 -(id)method1
 {
     id val1 = [classname1 methodToUse];
     id val2 = [classname2 otherMethodToUse];
     return val1 + val2;
 }
@end
    于 2013-03-05T04:47:25.730   回答
    
    
            1        
        
		
Objective-C 不支持多重继承。您可以创建类的实例并在新类中访问它们的方法
@interface class3
{
   class1 *c1;
   class2 *c2;
}
和访问方法使用
[c1 yourMethod];
[c2 yourMethod];
您还可以使用协议通过创建委托方法并在其他类中实现它来访问多个类的方法,如http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols中所述.html
于 2013-03-05T05:00:26.507   回答