如何证明C#中没有多重继承?

时间:2013-02-11 08:25:21

标签: c# .net interface multiple-inheritance

是的,我被问到这个问题和我能提供的所有问题,是我所知道的以下信息的摘要。当然,除了在编写所谓的预期代码时出错,希望我们可以编写 1 ,是否有更多有效理由证明C#不支持Multiple继承?

在C#,comparing C# to Java (where Java implements MI with interfaces)的MI背景下,在SO和外部网站上提出了许多问题,如何在C#中模拟/模拟MI,如何处理C#中缺少MI,what is the problem with MI等等。所以我的问题可能反映了那些问题的双重性 ...但是我还没有找到答案,因此想到了拍摄。

  

In a blog I saw the author is talking about some valuable points:

     
      
  1. 跨语言的不同MI实现使得实现与语言无关的实现成为一项挑战。
  2.   
  3. 可以使用接口代替,这使MI有点多余。
  4.   
  5. MI增加了铸造,反射等方面的复杂性。
  6.         

    作者表示实现的多重继承是C#中不允许的。 所以要停止前往double diamond inheritance problem/deadly diamond of death .. Like author had pointed out saying "I have inherited a lot from both my mother and father"..

因此,从多个超级/基类/父类继承类更有意义。例如。 Class House继承自Class Building(名词,代表建筑物的详细信息{house,school,office,hotel etc})和Class Construction(代表建筑物的细节)。因此,如果可以的话,那就太好了,

1. code wish we could write
public class House: Building, Construction {
// methods, properties...
} 

相反,我们必须写出interFACE ..

interface Buildstruction
{
    methodConstruction();
}
//class Construction then has to implement the interface

public class  House: Building, Buildstruction
{
    Construction ConstionObject;
    methodBuilding();
    methodConstruction() { ConstionObject.methodConstruction(); }
}

是不是更容易不允许多个超类具有相同的名称方法(例如calculate方法)?然后我意识到那些父/超类需要有自由创建自己的属性和方法独立于另一个类..

因此 MI 在C#中似乎任务不可能 ......以及原因决定了它为何如此......仅仅是因为,

  • 钻石问题
  • 没有人真正指出MI在编程中的真正用途?

还怎么做,

  • MI增加了castingreflection
  • 的复杂性
  • 接口使MI有点多余?
  • MI会导致代码问题(支持完全支持OOP)与语言无关吗?

感谢评论/答案不只是 “这就是创建C#语言的方式......” :)

0 个答案:

没有答案