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
You may want to take your argument a step further and talk about design patterns - and you can find out why he'd want to bother trying to inherit from multiple classes in c# if he even could
Multiple inheritance is not supported in C#.
But if you want to "inherit" behavior from two sources why not use the combination of:
There is a basic but important OOP principle that says: "Favor composition over inheritance".
You can create a class like this:
As you can see the dependecies (actual implementations of the interfaces) are injected via the constructor. You class does not know how each class is implemented but it knows how to use them. Hence, a loose coupling between the classes involved here but the same power of usage.
Today's trends show that inheritance is kind of "out of fashion".
Actually, it depends on your definition of inheritance:
This is not what is usually meant by the term "inheritance", but it is also not entirely unreasonable to define it this way.
C# 3.5 or below does not support the multiple inheritance, but C# 4.0 could do this by using, as I remembered, Dynamics.
C# does not support multiple inheritance of classes, but you are permitted to inherit/implement any number of interfaces.
This is illegal (B, C, D & E are all classes)
This is legal (IB, IC, ID & IE are all interfaces)
This is legal (B is a class, IC, ID & IE are interfaces)
Composition over inheritance is a design pattern that seems to be favorable even in languages that support multiple inheritance.