On C # multi-Inheritance

Source: Internet
Author: User

C # The discussion of Multi-inheritance seems to be an old question, but today this article will show you that C # multi-inheritance may be something you have never heard, people who even invented the C # language never thought that I would write code like this and enjoy myself. 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. Please refer to the case if 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? The answer is actually very simple, that is, to write an extension method for the IFly interface. First, please refer to the definition of this empty interface and its extension method (pay attention to generic restrictions ): copy code 1 public interface I Fei 2 {3 4} 5 6 public static class Fei interface extension 7 {8 public static void Fei <T> (this T Fei instance) where T: I Fei 9 {10 Console. writeLine ("prepare"); 11 Console. writeLine ("open wings"); 12 Console. writeLine (""); 13 Console. writeLine ("I fly, I fly, I fly Fly"); 14} 15} copy the code to see the implementation of tigers and flies: copy code 1 public class Tiger 2 {3 public virtual void self-introduction () 4 {5 Console. writeLine ("Hello everyone, I'm a tiger. "); 6} 7} 8 9 public class fly: I fly 10 {11 public void fly a look () 12 {13 this. fei (); 14} 15} copy the code and then read the implementation of the Super Tiger: Copy code 1 public class super Tiger: Tiger, I fly 2 {3 public override void self-introduction () 4 {5 Console. writeLine ("Hello everyone, I'm a super tiger! "); 6} 7 8 public void I will fly yo () 9 {10 this. Fei (); 11} 12} copy the code. 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. Is there any practical experience in the development of multi-inheritance? I am here to share with you a scenario that is widely used in our project. Our project is based on ASP. NET MVC4. There are multiple areas below, which can be understood as independent modules. Each module has a similar login Logout function, so we use ILogoutController and implement the Logout function in its extension method. Log out will do these tasks: Clear the session, save the user status, and write logs. In this way, as long as a controller inherits from ILogoutController, the controller has the login logout function. In principle, as long as it is similar to functionality, we can use the null interface Extension Method to write more inheritance, so as to implement function injection, and the injection code is easier to maintain. Finally, let's review 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 analysis above, we can draw a conclusion that when writing multi-inheritance is required, 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. EDIT: I did not expect to reply to your comments in just one day. This post has 30 comments. It seems that you are very interested in the discussion of language. Thanks for your enthusiastic participation, but I was surprised that this post also received nine objections. From the comments, about 60% of the people opposed this article. However, I would like to say that there is really no objection. Accepting this idea will only help your project, but will not be harmful. Whether the idea conforms to the definition of Multi-inheritance or whether it can achieve the same effect of inheritance (polymorphism and private members), whether you call it a design pattern or a syntactic sugar, or nondescribedomainsinheritance, this idea is completely in line with the object-oriented thinking, much stronger than using static methods.

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.