如何在objective C中继承多个类?

时间:2022-09-24 23:12:08

I have classA and ClassA inherit ClassX and access methods of classX. How can I also access the methiod of ClassY with inheritance because multiple inheritance is not possible. I dont want to create another other composition class for this.I want to use same classA for multiple inheritance.

我有classA和classA继承ClassX和ClassX的访问方法。由于多重继承是不可能的,所以如何使用继承访问ClassY的方法呢?我不想为这个创建另一个复合类。我想对多重继承使用相同的类a。

3 个解决方案

#1


4  

There is no multiple inheritance. The only way to achieve this is by merging the two class heirarchies. Either by having ClassX inherit ClassY or ClassY inherit ClassX (then ClassA inherits the child class X or Y).

没有多重继承。实现这一目标的唯一途径是合并两类继承关系。通过ClassX继承ClassY或ClassY继承ClassX(然后ClassA继承子类X或Y)。

If the two classes do not by design fit into the same hierarchy, you might want to reconsider your design and the reasons why you do not want to use composition.

如果这两个类不通过设计符合相同的层次结构,您可能需要重新考虑您的设计以及不希望使用组合的原因。

#2


1  

Like Objective-C, Swift does not have multiple inheritance. Swift uses protocols and categories to give you the same sort of ability. You can define a protocol that defines a set of methods, and then add support for that protocol to multiple methods. You can often build support for a protocol into a category and then that category to your classes as needed.

与Objective-C一样,Swift没有多重继承。Swift使用协议和类别来提供相同的能力。您可以定义一个协议来定义一组方法,然后向多个方法添加对该协议的支持。您通常可以将对协议的支持构建为一个类别,然后根据需要将该类别构建为您的类。

#3


0  

As said before, multiple inheritance is not supported by the language (neither ObjC nor Swift). If you need to inherit methods/properties from multiple classes, you will need to use composition. Alternatively, what the language does allow you to do is to have a class conform to multiple protocols, which may or may not be a solution to the problem you are trying to solve.

如前所述,该语言不支持多重继承(ObjC和Swift都不支持)。如果需要从多个类继承方法/属性,则需要使用组合。另外,语言允许您做的是让类符合多个协议,这可能是您正在尝试解决的问题的解决方案,也可能不是。

I have come across very few cases where I thought that I really needed to have multiple inheritance, and even for those cases, they were typically resolved by employing an appropriate code design pattern (thinking about something like the Gang of Four design patterns). In essence, you want to abstract your code in such a way so that multiple inheritance is not a requirement anymore.

我遇到过很少的情况,我认为我真的需要有多重继承,甚至在那些情况下,它们通常是通过采用适当的代码设计模式来解决的(思考类似于“*”的设计模式)。本质上,您希望以这样的方式抽象代码,以使多重继承不再是一种需求。

#1


4  

There is no multiple inheritance. The only way to achieve this is by merging the two class heirarchies. Either by having ClassX inherit ClassY or ClassY inherit ClassX (then ClassA inherits the child class X or Y).

没有多重继承。实现这一目标的唯一途径是合并两类继承关系。通过ClassX继承ClassY或ClassY继承ClassX(然后ClassA继承子类X或Y)。

If the two classes do not by design fit into the same hierarchy, you might want to reconsider your design and the reasons why you do not want to use composition.

如果这两个类不通过设计符合相同的层次结构,您可能需要重新考虑您的设计以及不希望使用组合的原因。

#2


1  

Like Objective-C, Swift does not have multiple inheritance. Swift uses protocols and categories to give you the same sort of ability. You can define a protocol that defines a set of methods, and then add support for that protocol to multiple methods. You can often build support for a protocol into a category and then that category to your classes as needed.

与Objective-C一样,Swift没有多重继承。Swift使用协议和类别来提供相同的能力。您可以定义一个协议来定义一组方法,然后向多个方法添加对该协议的支持。您通常可以将对协议的支持构建为一个类别,然后根据需要将该类别构建为您的类。

#3


0  

As said before, multiple inheritance is not supported by the language (neither ObjC nor Swift). If you need to inherit methods/properties from multiple classes, you will need to use composition. Alternatively, what the language does allow you to do is to have a class conform to multiple protocols, which may or may not be a solution to the problem you are trying to solve.

如前所述,该语言不支持多重继承(ObjC和Swift都不支持)。如果需要从多个类继承方法/属性,则需要使用组合。另外,语言允许您做的是让类符合多个协议,这可能是您正在尝试解决的问题的解决方案,也可能不是。

I have come across very few cases where I thought that I really needed to have multiple inheritance, and even for those cases, they were typically resolved by employing an appropriate code design pattern (thinking about something like the Gang of Four design patterns). In essence, you want to abstract your code in such a way so that multiple inheritance is not a requirement anymore.

我遇到过很少的情况,我认为我真的需要有多重继承,甚至在那些情况下,它们通常是通过采用适当的代码设计模式来解决的(思考类似于“*”的设计模式)。本质上,您希望以这样的方式抽象代码,以使多重继承不再是一种需求。