I recently saw a post about how to achieve multiple inheritance in C# language. I believe that people who are not involved in C# (rookies like me) find it ridiculous at first glance. C# definitely cannot achieve more inheritance. We all know that in C++, there will be many ambiguity problems in implementing multiple inheritance, so in C#, multiple inheritance is cancelled and implemented using interfaces! But think about it, if you are a beginner, you will definitely not be able to ask such questions. He must be a master, and then he started searching for information online! Then I found that it can be realized!
Speaking of more inheritance, first of all, you can think about this question: Do you know how to achieve more inheritance in C#?
There are only two mainstream answers.
Answer 1: Use interfaces, a class can inherit from multiple interfaces.
Answer 2: C# does not support multiple inheritance, but C++ supports multiple inheritance. Multiple inheritance will make the code very messy. Therefore, Microsoft gave up multiple inheritance when designing C#.
People who can know Answer 2 obviously know more. I also believed that C# does not support multiple inheritance for a long time. Until a project in May 2013, I accidentally discovered that my code completely realized the true meaning of multiple inheritance.
Let’s talk about what is the true meaning of multi-inheritance. True multi-inheritance should be like C++, rather than saying that if a class inherits multiple interfaces in C#, it is called multi-inheritance. In C#, if a class implements multiple interfaces, then the implementation needs to be written for each interface. If the interface is inherited by multiple classes, then there will be duplicate code, which is obviously unacceptable.
However, multiple inheritance like C++ does indeed cause great trouble to coding. I also believe that Microsoft has abandoned this feature in C# because it realizes the unreasonableness of multiple inheritance. The first thing I implement is the multiple inheritance in C# is the real multiple inheritance, and the second code is written very reasonably.
Please see the case
If you have a class called tiger and another class called fly. Now you want to create a new super tiger, a kind of tiger that can fly. In C++, you can define a super tiger class that inherits from tigers and flies so that this kind of tiger can fly. However, the problem arises. This super tiger also inherits from flies, and there is a method called eating, and the parameter type is shit. This method of eating shit is obviously too incompatible with our super tiger.
Although this example is a bit exaggerated, many C++ programmers are really designing code like this. Since the subclass inherits multiple parent classes, and some members of multiple parent classes must not match the subclass, the caller of the subclass is very uncomfortable. For example, in the above example, when the caller gets an instance of the Super Tiger, he finds that there is a way to eat shit under the Super Tiger! ! ! It's really going to make people laugh.
If C++ allows multiple inheritance in this way, it will inevitably cause this problem. C# programmers will never write such funny code. For C# programmers, they must make this fly method an interface, and then let both fly and super tiger inherit from this interface. In this way, flies can fly and super tigers can fly. Is this perfect solution to this problem?
The problem seems to be solved, but if I tell you that the method of flying fly needs to be exactly the same as that of a super tiger flying: first spread your wings, lean forward, flap your wings, take off, and continue flapping. We definitely can't copy the same code, it's something that entry-level programmers do, and we are no longer qualified to do that. So what should I do? A simple and quick way is to use static methods, such as (...).
Static methods solve the problem of code reuse, but always feel something is wrong when written. My super tigers and flies have clearly inherited the flying method, so why do I still call a static method like this? If I want my pig to fly one day in the future, wouldn’t I still have to call this static method?
How can we achieve elegant inheritance in C# like C++?
The answer is revealed
The answer is actually very simple, that is, write an extension method to the IFly interface.
First, please look at the definition of this empty interface and its extension methods (note generic limitations):
namespace Interface
{
//Flying interface
public interface IFly
{
}
//Extend method
public static class ExtendFly
{
public static void StartFly<T>(this T example) where T : IFly
{
("Prepare");
("Open wings");
("take off");
("I fly, I fly, I fly");
}
}
}
Let’s look at the implementation of tigers and flies:
namespace Interface
{
//Fly flying interface
public class flies : IFly
{
public void fly()
{
//Calling the method of flying in the interface
();
}
}
}
namespace Interface
{
//Tiger
public class Tiger
{
public void introduce()
{
("I am a tiger");
}
}
}
Let’s look at the implementation of Super Tiger:
namespace Interface
{
//Super tiger, inherited the tiger and implemented the method of flying
public class SuperTiger : Tiger, IFly
{
public override void introduce()
{
("Hello everyone, I'm a super tiger!");
}
public void TigerFly()
{
//Calling the method of flying in the interface
();
}
}
}
How about it, do you understand? Is this implementation very simple? Are there any benefits?
If the boss asks you to realize a flying super pig one day in the future, you only need to let your super pig inherit the "I flying" interface. If the boss doesn't want this super pig flying one day, you just need to inherit and delete this interface. If you are developing an animal kingdom program, you can inject the function of flying into any animal. Think about it, do you all feel very good?
Summarize
Finally, let’s review the perverted examples of super tiger eating shit written in C++ before. This is actually not C++'s fault, but programmers use multiple inheritance wrongly. Although C++ does not restrict how programmers write multiple inheritance in syntax, we can easily draw a conclusion from the above example analysis:
When multiple inheritance is needed, the inherited parent class can only be a function, not a complete class.
If we follow this idea, today's example can be written in C++. First, we will mention a Flyable class, and then let the super tiger and flies inherit this Flyable.
In C#, although the code that implements multiple inheritance has taken a little turn, the benefits of multiple inheritance are very obvious: implementing injection-based functions for different classes makes your code more in line with the idea of object-oriented.