编写程序以实现多重继承 [英] Write a program to Implement Multiple Inheritance

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

问题描述

请帮助我理解这个问题.

Please help me understand this question.

推荐答案

C#的模拟多重继承模式 [ ^ ]

一个小的Google会走很长一段路.
Simulated Multiple Inheritance Pattern for C#[^]

A little Google goes a long way.


您用C#关键字标记了您的问题,所以我假设您使用的是C#.

对你的问题.这是不可能的,至少不会以您从其他语言(例如,其他语言)中了解到的方式C ++
例子:

You tagged your question with the C# keyword so I''m assuming you''re writing in C#.

To your question. It is not possible, at least not in the way you may know it from other languages, e.g. C++
An example:

class Machine
{...}

class Car
{...}

class Ferrari : Car, Machine
{...}



上面的代码将无法编译.但是您可以执行以下操作



The above code will not compile. But you can do the following

class Machine
{...}

class Car : Machine
{...}

class Ferrari : Car
{...}



另外,您可能希望查看以下两个链接:
C#的模拟多重继承模式 [ C#中的多重继承 [



In addition you might want to have a look at these two links:
Simulated Multiple Inheritance Pattern for C#[^]
Multiple Inheritance in C#[^]

Best regards


除了Dave和Shoputer的答案:.NET中,类不支持多重继承,但是所谓的弱形式多重继承.它意味着两件事的结合:1)对于接口,允许多重继承,2)对于一个类,在以下意义上支持多重继承:一个类可以有一个完全相同的基(不包括唯一的无基"根类) System.Object),但在该类的基本类型列表中没有数量限制的基本接口.

此功能不允许像类中的C ++这样的类之间存在真正的多重继承关系,但是可以基于弱形式开发类似的逻辑结构.

如果知道多重继承的用户面临严重的设计问题也很重要,那么在某些情况下使用时应格外小心.请参阅
> http://en.wikipedia.org/wiki/Multiple_inheritance#Criticisms [
In addition to the answers by Dave and Shoputer: in .NET, multiple inheritance is not supported for classes, but what is supported is so called weak form of multiple inheritance. It means the combination of two things: 1) for interfaces, multiple inheritance is allowed, 2) for a class, multiple inheritance is supported in the following sense: a class can have exactly one base (excluding the only one "baseless" root class System.Object) but unlimited number of base interfaces in the list of the base types of the class.

This feature will not allow for true multiple inheritance like in C++ as relationship between classes, but similar logical structures can be developed based in the weak form.

If also important to know that the user of multiple inheritance face with serious design problems, so it is useful in certain situations by should be used with great care. Please see http://en.wikipedia.org/wiki/Multiple_inheritance#Criticisms[^].

—SA


这篇关于编写程序以实现多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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