objective-c 如何从多个类继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1365732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to inherit from multiple class
提问by CiNN
Let's say i have a griffon object that needs to be part of the felidae and bird class.
How do i do it ?
I can only make it inherit from 1 class at a time...
假设我有一个需要成为猫科和鸟类类的一部分的狮鹫对象。
我该怎么做 ?
我一次只能让它继承 1 个类......
回答by Kelly Gendron
This may help...
这可能有助于...
Multiple inheritance
多重继承
There is no innate multiple inheritance (of course some see this as a benefit). To get around it you can create a compound class, i.e. a class with instance variables that are ids of other objects. Instances can specifically redirect messages to any combination of the objects they are compounded of. (It isn't that much of a hassle and you have direct control over the inheritance logistics.) [Of course, this is not `getting around the problem of not having multiple inheritance', but just modeling your world slightly different in such a way that you don't need multiple inheritance.]
Protocols address the absence of multiple inheritance (MI) to some extent: Technically, protocols are equivalent to MI for purely "abstract" classes (see the answer on `Protocols' below).
[How does Delegation fit in here? Delegation is extending a class' functionality in a way anticipated by the designer of that class, without the need for subclassing. One can, of course, be the delegate of several objects of different classes. ]
没有先天的多重继承(当然有些人认为这是一个好处)。为了解决这个问题,你可以创建一个复合类,即一个类的实例变量是其他对象的 id。实例可以专门将消息重定向到它们复合的对象的任意组合。(这并没有那么麻烦,您可以直接控制继承逻辑。)[当然,这不是“解决没有多重继承的问题”,而只是对您的世界进行建模,在这种情况下略有不同不需要多重继承的方式。]
协议在某种程度上解决了多重继承 (MI) 的缺失:从技术上讲,协议等同于纯粹“抽象”类的 MI(请参阅下面关于“协议”的答案)。
[委派如何适应这里?委托是以类的设计者预期的方式扩展类的功能,而不需要子类化。当然,一个人可以是不同类的几个对象的委托。]
-Taken from http://burks.brighton.ac.uk/burks/language/objc/dekorte/0_old/intro.htm
-摘自http://burks.brighton.ac.uk/burks/language/objc/dekorte/0_old/intro.htm
回答by Anindya Sengupta
Multiple Inheritance in Objective C is not supported. The reason for not supporting this mechanism might be the fact that it would have been too difficult to include in the language or the authors thought it is a bad programming and design decision. However, in various cases multiple inheritance proves to be helpful. Fortunately objective C does provide some workarounds for achieving multiple inheritance. Following are the options:
不支持 Objective C 中的多重继承。不支持这种机制的原因可能是将它包含在语言中太难了,或者作者认为这是一个糟糕的编程和设计决策。然而,在各种情况下,多重继承被证明是有帮助的。幸运的是,目标 C 确实提供了一些实现多重继承的变通方法。以下是选项:
Option 1: Message Forwarding
选项 1:消息转发
Message Forwarding, as the name suggests, is a mechanism offered by Objective C runtime. When a message is passed to an object and the object does not respond to it, the application crashes. But before crashing the objective c runtime provides a second chance for the program to pass the message to the proper object/class which actually responds to it. After tracing for the message till the top most superclass, the forwardInvocationmessage is called. By overriding this method, one can actually redirect the message to another class.
顾名思义,消息转发是 Objective C 运行时提供的一种机制。当消息传递给对象而对象没有响应时,应用程序就会崩溃。但是在崩溃之前,目标 c 运行时为程序提供了第二次机会将消息传递给实际响应它的正确对象/类。在跟踪消息直到最顶层的超类之后,该forwardInvocation消息被调用。通过覆盖此方法,实际上可以将消息重定向到另一个类。
Example: If there is a class named Car which has a property named carInfo which provides the car's make, model and year of manufacture, and the carInfo contains the data in NSString format, it would be very helpful if NSString class methods could be called upon the objects of Car class which actually inherits from NSObject.
示例:如果有一个名为 Car 的类,它有一个名为 carInfo 的属性,该属性提供汽车的品牌、型号和制造年份,并且 carInfo 包含 NSString 格式的数据,如果可以调用 NSString 类方法将非常有帮助实际上继承自 NSObject 的 Car 类的对象。
- (id)forwardingTargetForSelector:(SEL)sel
{
if ([self.carInfo respondsToSelector:sel]) return self.carInfo;
return nil;
}
Source: iOS 4 Developer's cookbook - Erica Sadun
来源:iOS 4 开发人员的食谱 - Erica Sadun
Option 2: Composition
选项 2:组合
Composition is a cocoa design pattern which involves referencing another object and calling its functionalities whenever required. Composition actually is a technique for a view to build itself based on several other views. So, in Cocoa terminology this is very similar to Subclassing.
组合是一种可可设计模式,它涉及引用另一个对象并在需要时调用其功能。组合实际上是一种视图基于其他几个视图构建自身的技术。因此,在 Cocoa 术语中,这与子类化非常相似。
@interface ClassA : NSObject {
}
-(void)methodA;
@end
@interface ClassB : NSObject {
}
-(void)methodB;
@end
@interface MyClass : NSObject {
ClassA *a;
ClassB *b;
}
-(id)initWithA:(ClassA *)anA b:(ClassB *)aB;
-(void)methodA;
-(void)methodB;
@end
Source: Objective-C multiple inheritance
Option 3: Protocols
选项 3:协议
Protocols are classes which contains method to be implemented by other classes who implement the protocol. One class can implement as many as protocols and can implement the methods. However, with protocols only methods can be inherited and not the instance variables.
协议是包含要由实现协议的其他类实现的方法的类。一个类可以实现与协议一样多的数量,并且可以实现方法。但是,对于协议,只能继承方法,而不能继承实例变量。
回答by dreamlax
You can dynamically create a class at runtime and choose the methods of each parent class to inherit. Have a look at the NeXT runtime's documentation hereabout dynamically creating classes. I did this once just for fun, but I didn't get very far as it gets incredibly messy very quickly.
您可以在运行时动态创建一个类,并选择要继承的每个父类的方法。在此处查看有关动态创建类的 NeXT 运行时文档。我做了一次只是为了好玩,但我没有走得太远,因为它很快变得非常混乱。
Edit
编辑
It gets more difficult though, because there can only be one superclass, otherwise the keyword superbecomes ambiguous.
但它变得更加困难,因为只能有一个超类,否则关键字super会变得不明确。
回答by Devin Ceartas
You can't, per se. But you can have references to as many other objects as you need and you can use multiple protocols.
你不能,本身。但是您可以根据需要引用尽可能多的其他对象,并且可以使用多种协议。
回答by Alec
First, make felidae a subclass of bird. Piece of cake. :-)
首先,使猫科成为鸟类的子类。小菜一碟。:-)

