如何从多个类继承 [英] how to inherit from multiple class

查看:78
本文介绍了如何从多个类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个狮鹫对象,它需要成为猫科和鸟类类的一部分.
我该怎么做?
我一次只能让它继承 1 个类...

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...

推荐答案

这可能有帮助...

多重继承

  • 没有天生的多重继承(当然有些人认为这是一个好处).为了解决这个问题,你可以创建一个复合类,即一个类的实例变量是其他对象的 id.实例可以专门将消息重定向到它们复合的对象的任意组合.(这并没有那么麻烦,您可以直接控制继承逻辑.)[当然,这不是解决没有多重继承的问题",而只是建模您的世界在这种情况下略有不同不需要多重继承的方式.]

  • 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.]

协议在某种程度上解决了多重继承 (MI) 缺失的问题:从技术上讲,协议等同于纯粹抽象"类的 MI(请参阅下面关于协议"的答案).

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. ]

-取自 http://burks.brighton.ac.uk/burks/language/objc/dekorte/0_old/intro.htm

这篇关于如何从多个类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆