用于多重继承吗? [英] A use for multiple inheritance?

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

问题描述

任何人都可以想到使用多重继承的任何情况吗?我能想到的每种情况都可以通过方法运算符

Can anyone think of any situation to use multiple inheritance? Every case I can think of can be solved by the method operator

AnotherClass() { return this->something.anotherClass; }

推荐答案

全面使用的大多数用途多重继承用于mixin.例如:

Most uses of full scale Multiple inheritance are for mixins. As an example:

class DraggableWindow : Window, Draggable { }
class SkinnableWindow : Window, Skinnable { }
class DraggableSkinnableWindow : Window, Draggable, Skinnable { }

等...

在大多数情况下,最好使用多重继承来严格执行接口继承.

In most cases, it's best to use multiple inheritance to do strictly interface inheritance.

class DraggableWindow : Window, IDraggable { }

然后,在DraggableWindow类中实现IDraggable接口.编写好的mixin类太困难了.

Then you implement the IDraggable interface in your DraggableWindow class. It's WAY too hard to write good mixin classes.

MI方法的好处(即使您仅使用Interface MI)也可以将各种不同的Windows视为Window对象,但是可以灵活地创建不可能(或更难)的事物. )具有单一继承.

The benefit of the MI approach (even if you are only using Interface MI) is that you can then treat all kinds of different Windows as Window objects, but have the flexibility to create things that would not be possible (or more difficult) with single inheritance.

例如,在许多类框架中,您将看到以下内容:

For example, in many class frameworks you see something like this:

class Control { }
class Window : Control { }
class Textbox : Control { }

现在,假设您想要一个具有Window特性的文本框?就像是可拖动的,具有标题栏等...您可以执行以下操作:

Now, suppose you wanted a Textbox with Window characteristics? Like being dragable, having a titlebar, etc... You could do something like this:

class WindowedTextbox : Control, IWindow, ITexbox { }

在单一继承模型中,如果没有重复的Control对象和其他类型的问题,就无法轻松地从Window和Textbox继承.您也可以将WindowedTextbox视为窗口,文本框或控件.

In the single inheritance model, you can't easily inherit from both Window and Textbox without having some problems with duplicate Control objects and other kinds of problems. You can also treat a WindowedTextbox as a Window, a Textbox, or a Control.

另外,为了解决您的.anotherClass()习惯用法,.anotherClass()返回一个不同的对象,而多重继承允许将同一对象用于不同的目的.

Also, to address your .anotherClass() idiom, .anotherClass() returns a different object, while multiple inheritance allows the same object to be used for different purposes.

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

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