本文介绍了是c#中的多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我们大多数人都听说过,而且我们大多数人都认为.net不支持多重继承(如果我们想要执行那么我们有如果确实如此,那么任何人都可以向我解释并帮助我理解以下场景中发生的事情: 我们都知道在.net中创建的每个类都默认继承System.Data,所以我创建了以下类: class MyCls1 { public void 显示() { Console.WriteLine( Hello); } } 其中我只定义了单一的显示。当我创建MyCls1类的对象并尝试调用display()方法时,我已经得到了Equals,GetHashCode,GetType和ToString,也就是(可能是来自system.object的)。 现在我已经创建了另一个名为Mycls2的类,同样的东西也是如此。 class Mycls2 { public void 显示() { Console.WriteLine( Hello2); } } 所以我们可以说这两个类继承了System.Object类。 现在如果我将类MyCls1继承到类Mycls2中,那么因为Mycls2类已经继承了System.object并且现在继承了类MyCls1,所以我们可以说类MyCls2继承了类MyCls1和System.object同时(即多重继承中发生的事情)并且它在多重继承中?如果不是那么为什么??? class MyCls1 { public void disp() { Console.WriteLine( Hello); } } class Mycls2:MyCls1 { public void Show() { Console.WriteLine( Hello2); } } 解决方案 这不是多重继承。 想想关于它,看看家人或人。 你来自你的父母:他们来自他们的父母。这意味着你从父母和祖父母那里继承了。从他们的父母,他们的等等...... 这不是多重继承,而是继承自构成父母的整个分支 。 类继承是相同的:对象 - >控制 - >表格 - > MyForm 意味着在MyForm中可以使用Control和Object的所有属性,因为它继承了整个分支 - 否则你无法设计任何复杂的东西,因为整个层次结构必须是平坦的: pre lang =text>对象 - >一切然后真正的继承根本不起作用。 多重继承是从无关分支继承的: object - >控制 - >表格对象 - >收藏 - >列表类MyClass:Form,List 而C#不允许这样做。 C#中实际允许多重继承,但仅限于接口。此外,文件的继承列表只能包含一个文件,但只能包含任意数量的接口。这有时被称为多重继承的弱形式。实际上,它比免费的多重继承要弱得多。但是,在某种意义上,创造的问题较少。 -SA C#实现单继承但允许同时多个接口。 引用:最大的问题多继承是指当编译器需要找到虚拟方法的正确实现时,它允许模糊。 阅读: C#中的多重继承 [ ^ ] 为什么不允许:( MSDN [ ^ ]) 1.不同的语言实际上对MI的工作方式有不同的期望。例如,如何解决冲突以及重复的基数是合并还是冗余。在我们在CLR中实现MI之前,我们必须对所有语言进行调查,找出常见概念,并决定如何以语言中立的方式表达它们。我们还必须决定MI是否属于CLS,这对于不想要这个概念的语言(例如VB.NET)意味着什么。当然,这是我们作为公共语言运行时所处的业务,但我们还没有为MI做这件事。 2.数量MI确实合适的地方实际上非常小。在许多情况下,多个接口继承可以完成工作。在其他情况下,您可以使用封装和委派。如果我们要添加一个稍微不同的构造,比如mixins,那实际上会更强大吗? 3.多个实现继承为实现注入了很多复杂性。这种复杂性会影响投射,布局,调度,现场访问,序列化,身份比较,可验证性,反射,泛型,以及可能还有很多其他地方。 -KR Hi,as mostly all of us heard and most of us believe that .net does not supporting multiple inheritance (if we want to perform that then we have to use interface for that) if it is really true then anyone can explain me and help me to understand what is happening in the following scenario:as we all know that every class created in .net is inheriting System.Data by default so i have created following class:class MyCls1 { public void display() { Console.WriteLine("Hello"); } }where i have defined only single methood "display". when i create the object of MyCls1 class and try to call the display() methood, i have got "Equals", "GetHashCode", "GetType" and "ToString" methoods also (which is derrieved from system.object may be).now i have created another class named "Mycls2" and same thing is haapen with it also.class Mycls2 { public void Show() { Console.WriteLine("Hello2"); } }so we can say that these two classes inheriting System.Object class.now if i inherit class MyCls1 into class Mycls2, so bacause of class Mycls2 is already inheriting System.object and now its inheriting class MyCls1 also so that can we say that class MyCls2 is inheriting both class MyCls1 and System.object at same time (that is what happen in the multiple inheritance) and it is in the multiple inheritance ??? and if its not then why ???class MyCls1 { public void disp() { Console.WriteLine("Hello"); } } class Mycls2 :MyCls1 { public void Show() { Console.WriteLine("Hello2"); } } 解决方案 That isn't multiple inheritance.Think about it for a moment, and look at families or people.You are derived from your parents: and they are derived from their parents. Which means that you "inherit" from both your parents, and your grandparents. And from their parents, and theirs, and so on...That isn't multiple inheritance, it's inheriting from the "whole branch" that makes up your parent.Class inheritance is the same: Object -> Control -> Form -> MyForm means that all the properties of Control and Object are available in MyForm because it inherits the whole branch - otherwise you couldn't design anything complex, because the whole hierarchy would have to be "flat": Object -> EverythingThen true inheritance wouldn't work at all.Multiple inheritance is when you inherit from unrelated branches:object -> Control - > Formobject -> Collection -> ListClass MyClass: Form, ListAnd C# does not allow that.Multiple inheritance is actually allowed in C#, but only for interfaces. Also, inheritance list for a file can include only one file but any number of interfaces. This is sometimes called "weak form of multiple inheritance". Indeed, it's much weaker then free multiple inheritance. But, in certain sense, creates less problems.—SAC# implements single inheritance but allows for multiple interfaces at the same time.Quote:The biggest problem with multiple-inheritance is that it allows for ambiguity when the compiler needs to find the correct implementation of a virtual method.Read this : Multiple Inheritance in C#[^]Why it is not allowed : (MSDN[^])1. Different languages actually have different expectations for how MI works. For example, how conflicts are resolved and whether duplicate bases are merged or redundant. Before we can implement MI in the CLR, we have to do a survey of all the languages, figure out the common concepts, and decide how to express them in a language-neutral manner. We would also have to decide whether MI belongs in the CLS and what this would mean for languages that don't want this concept (presumably VB.NET, for example). Of course, that's the business we are in as a common language runtime, but we haven't got around to doing it for MI yet.2. The number of places where MI is truly appropriate is actually quite small. In many cases, multiple interface inheritance can get the job done instead. In other cases, you may be able to use encapsulation and delegation. If we were to add a slightly different construct, like mixins, would that actually be more powerful?3. Multiple implementation inheritance injects a lot of complexity into the implementation. This complexity impacts casting, layout, dispatch, field access, serialization, identity comparisons, verifiability, reflection, generics, and probably lots of other places.-KR 这篇关于是c#中的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 03:24