C # multi-Inheritance

Source: Internet
Author: User

Address: http://blog.csdn.net/yarshray/article/details/14824

Well written and easy to understand.

 

Actually, I want to write this articleArticleIt was because when I was sorting out the posts on the Forum, I suddenly found that a person asked me how to implement multiple inheritance in C #. At that time, I answered the answer in a vague manner, therefore, I would like to add a description here.

First, I want to explain that C # does not have the concept of multi-inheritance of classes. to use multi-inheritance, you must use the interface. but we all know that interface is actually a virtual function list pointer. only functions and attributes are encapsulated internally. in addition, interfaces cannot be used only by deriving (because there is no constructor. this is similar to the abstract class, but the abstract class is a class, which has implementation methods. the object it describes is an object that cannot be found in reality, but it is a class object. the interface is actually a standard. after talking about this, let's take an example of how to implement multiple inheritance in C.

Mybase. CS

 
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace intdv {public abstract class mybase {public mybase () {// to do something here //}}}

Myderive1.cs

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace intdv {class myderive1: intdv. mybase {string myname; Public myderive1 () {// to do something here // myname = "yarshary";} public void showmyname () {console. writeline ("My name is:" + this. myname) ;}public void Rename (string N) {myname = n ;}} public interface imyderive1 {void showmyname (); void Rename (string N );}}

Myderive2.cs

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace intdv {public class myderive2: intdv. mybase {int myage; Public myderive2 () {// to do something here // myage = 21;} public void showmyage () {console. writeline ("My age is:" + myage);} public void reage (int A) {This. myage = A ;}} public interface imyderive2 {void showmyage (); void reage (int );}}

Mymderive. CS

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace intdv {public sealed class mymderive: intdv. mybase, intdv. imyderive1, intdv. imyderive2 {intdv. myderive1 D1; intdv. myderive2 D2; Public mymderive () {// to do something here // d1 = new myderive1 (); d2 = new myderive2 ();} public void showmyname () {d1.showmyname ();} public void Rename (string N) {d1.rename (n);} public void showmyage () {d2.showmyage ();} public void reage (int) {d2.reage ();}}}

In case analysis, I first defined a base class mybase at the beginning, and derived two Derived classes: myderive1 and myderive2.
These two Classes define a group of Operations respectively. myderive1 defines the showmyname and rename operations and uses them through imyderive1.
Description. The operation showmyage and reage are defined in myderive2 and described through the interface imyderive2, so that I can perform multiple inheritance
Only the inheritance interfaces imyderive1 and imyderive2 can ensure the inheritance of the operation list. The operation implementation is based on the combination model.
Interface Method.CodeThe derived object mymderive (M is the meaning of Multi-slave inheritance), although only the interface is inherited,
However, in the constructor, two objects, myderive1 and myderive2, are dynamically allocated (new. in addition, Operations inherited from interfaces such as showmyname actually call the operations of myderive1 and myderive2. because myderive1 and myderive2 are
As defined in mymderive, client code (that is, my main function) does not need to manage the allocation and release of objects (of course, managed by CLR ).
Objects in the environment are generally not released manually ).

From the above example, we can see that the implementation excuse for setting aside function interfaces is to implement the implementation of method bodies in multiple inheritance by means of combination. this not only guarantees the uniqueness of a single inherited parent class, but also guarantees the advantages of multiple inheritance. I personally think it is a very good design method, and in myProgramThis method is used in many parts of the design. Well, it's time to go here.

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.