A colleague and I are having a bit of an argument over multiple inheritance. I'm saying it's not supported and he's saying it is. So, I thought that I'd ask the brainy bunch on the net.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
It does not allow it, use interface to achieve it.
Why it is so?
Here is the answer: It's allowed the compiler to make a very reasoned and rational decision that was always going to consistent about what was inherited and where it was inherited from so that when you did casting and you always know exactly which implementation you were dealing with.
You can't inherit multiple classes at a time. But there is an options to do that by the help of interface. See below code
you can call them as below
Multiple inheritance allows programmers to create classes that combine aspects of multiple classes and their corresponding hierarchies. For ex. the C++ allows you to inherit from more than one class
In C#, the classes are only allowed to inherit from a single parent class, which is called single inheritance. But you can use interfaces or a combination of one class and interface(s), where interface(s) should be followed by class name in the signature.
Ex:
You can inherit like the following:
class NewClass : X, Y { } In the above code, the class "NewClass" is created from multiple interfaces.
class NewClass : FirstClass, X { } In the above code, the class "NewClass" is created from interface X and class "FirstClass".
I recently somehow got to the same mindset, inheriting two classes into a class and ended up on this page (even though i know better) and would like to keep reference to the solution i found perfect in this scenario, without enforcing implementation of interfaces
My solution to this problem would be splitting up your data into classes that make sense:
Later on in your code, you could use it like this:
Include Both Parts, Base & Address
Base Only:
Address Only:
In generally, you can’t do it.
Consider these interfaces and classes:
You can inherit multiple interfaces:
But you can’t inherit multiple classes:
C# does not support multiple inheritance built in.
For adding multiple inheritance to languages that does not support it, You can use twin design pattern