C # How to Implement Multi-Inheritance

Source: Internet
Author: User

C # How to Implement Multi-Inheritance

I recently saw a post about how to implement multi-inheritance in C. I believe that there are not many people involved in c # (cainiao like me). At first glance, I think it is ridiculous. c # certainly cannot implement multi-inheritance. We all know that in c ++, there are many ambiguity issues due to multi-inheritance implementation. Therefore, in c #, the multi-inheritance is canceled and implemented using interfaces! However, if you are a beginner, you will certainly not ask such questions. I must be a master, and then I will start surfing the internet to check information! Then we can find that it can be implemented!

When talking about multi-inheritance, You can first think about this question: Do you know how to implement multi-inheritance in C?

There are only two mainstream answers.

Answer 1: using interfaces, a class can inherit from multiple interfaces.

Answer 2: C # does not support multi-inheritance. C ++ supports multi-inheritance. Multi-inheritance will make the code messy. Therefore, Microsoft gave up multi-inheritance when designing C.

I believe that C # does not support inheritance for a long time until a project in May 2013, by accident, I found that my code fully realized the true meaning of Multi-inheritance.

First, let's talk about what is the true meaning of Multi-inheritance. The true multi-inheritance should be like C ++, rather than a class in C # That inherits multiple interfaces is called multi-inheritance. In C #, if a class implements multiple interfaces, write the implementation for each interface. If the interface is inherited by multiple classes, there will be repeated code, this is obviously unacceptable.

However, the multi-inheritance of c ++ actually brings a lot of trouble to coding, I also believe that Microsoft has abandoned this feature in C # Only because it realized the irrationality of Multi-inheritance. In C #, I implement multi-inheritance. The first is true multi-inheritance, and the second code is reasonably written.


See the case

Suppose you have a class named tiger and a class named fly. Now you want to create a new super tiger, a Flying Tiger. In C ++, you can define a super tiger class to inherit from tigers and flies so that the Tigers can fly. However, the problem arises, because this super tiger also inherits from the fly, and there is a way below the fly to eat, the parameter type is shit. This method is obviously not suitable for our super tigers.

Although this example is exaggerated, many C ++ programmers are designing code in this way. The subclass inherits multiple parent classes, and some members of multiple parent classes may not be associated with this subclass, so the caller of the subclass is very uncomfortable. For example, in the above example, when the caller obtains an instance of the Super tiger, how can he find a way to eat shit under the super tiger !!! It's really about to laugh.

C ++ will inevitably cause this problem if it permits multiple inheritance. C # programmers will never write such funny code. For C # programmers, it is certainly necessary to commission this method into an interface, and then let both the fly class and the super tiger class inherit from this interface. In this way, the fly will fly, and the super Tiger will fly. Is it perfect to solve this problem?

The problem seems to have been solved, but if I tell you that the fly method needs to be the same as that of the Super tiger flying method: First, open your wings, lean forward, pat your wings, take off, and continue to Pat. We certainly cannot copy the same piece of code. It is a job of entry-level programmers. We are not qualified to do it now. What should we do? A simple and quick way is to use static methods, such as FlyHelper. Fly (...).

Static methods solve the issue of code reuse, but they always feel that something is wrong. My super tigers and flies have clearly inherited the flight. Why do I need to call a static method like this. If one day I want my pig to fly, isn't it necessary to call this static method.

How can we implement an elegant Inheritance like C ++ in C?


Answer announcement

The answer is actually very simple, that is, writing an extension method to the IFly interface.

First, read thisEmpty Interface(Pay attention to generic restrictions ):

Namespace Interface {// fly interface public Interface IFly {} // Extension Method public static class ExtendFly {public static void StartFly
 
  
(This T example) where T: IFly {Console. writeLine ("prepare"); Console. writeLine ("open wings"); Console. writeLine (""); Console. writeLine ("I fly, I fly, I fly ");}}}
 

Let's look at the implementation of tigers and flies:

Namespace Interface {// fly class implementation fly Interface public class flies: IFly {public void fly () {// call this. StartFly () ;}} in the Interface ();}}}
Namespace Interface {// Tiger class public class tiger {public void introduce () {Console. WriteLine ("I am a Tiger ");}}}

Let's look at the implementation of the Super Tiger:

Namespace Interface {// The Super Tiger class inherits the Tiger class and implements the flying public class SuperTiger: Tiger, IFly {public override void introduce () {Console. writeLine ("Hello everyone, I'm a super tiger! ");} Public void TigerFly () {// call this. StartFly () in the interface ();}}}

How do you understand? Is this implementation very simple? Is there a big benefit?

When the boss asks you to implement a flying super pig someday, you just need to let your super pig inherit the "I fly" interface. When the boss does not want this super pig, you only need to delete this interface. If you are developing an animal kingdom program, you can inject the flying function into any animal. Think about whether it is nice.

Summary

Finally, let's look back at the abnormal examples of super tigers that have been written in C ++. In fact, this is not a C ++ error, but a programmer uses the wrong inheritance. In terms of syntax, C ++ does not limit how programmers write multi-inheritance. However, from the above example, we can draw a conclusion:

When writing multiple inheritance statements, the inherited parent class can only be a function, rather than a complete class.

If we follow this idea, we can write this example in C ++. First, we will introduce a Flyable class, and then let the super tigers and flies inherit this Flyable.

In C #, although the Code implementing multi-inheritance is slightly bent, the benefits of Multi-inheritance are obvious: Implementing the injection function for different classes, make your code more in line with object-oriented thinking.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.