In fact, I want to write this article because I suddenly found a person asked me how to achieve multiple inheritance in C #, so I would like to add it.
First of all, I want to explain that there is no class inheritance in C #, which must be done by interface interface. But everyone knows that INTERFACE is actually a virtual function list pointer. Only functions of internal packaging and Attributes. And interfaces can only be used by derived (because there is no constructor). This is similar to the abstraction class, but the abstraction class is a class, he has a method. The object it describes is An object that cannot be in realities, but it itself is a class object. The interface is actually a standard. So much, let me give an example how to achieve multiple inheritance in C #.
example:
Using system;
Namespace INTDV {////
Using system;
Namespace INTDV {///
MyName = "yarshray";
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);}}
Using system;
Namespace INTDV {///
Myage = 21;
Public void showmyage () {console.writeline ("My Age IS:" myage);
Public void reage (int a) {myage = a;}}
Public interface iMyderive2 {void showmyage (); void reage (int A);}}
Using system;
namespace Intdv {///
Public void showmyName () {d1.showmyname (); public void rename () {d1.rename (N);} public void showmyage () {d2.showmyage ();} public void reage (int a) { D2.Reage (a);}}}
A summary description of Namespace INTDV {///
Md.reage (22); md.showmyage ();
Md.Rename ("SAGA"); md.showmyname ();}}}
Case analysis, first, I defined a base class mybase at the beginning and sent two derived classes to myderive1, and myderive2, respectively, a set of operations, and myderive1 defined SHOWMYNAME and RENAME and passed Adjust IMYDERIVE1 describes. Operation SHOWMYAGE and REAGE are defined in MyDerive2, so that IMYDERIVE2 is described in multiple inheritance, which is Imyderive1 and Imyderive2 to ensure the inheritance of the operation list, the implementation of the operation through the combined model. In the interface method. Here is a code from the code, my derived object Mymderive (M is more inherited from inheritance), although only the interface is inherited, but in the constructor, dynamic allocation (new) Myderive1 and Myderive2 Two objects. And actually call the operation of Myderive1 and MyDerive2 in inheritance in inheriting interfaces such as Interior. Because Myderive1 and Myderive2 are defined within MyMderive, the client code (that is, my main function) does not need The distribution and release of the management object (of course, the release of objects in the CLR hosted environment is generally not human).
From the above example, it can be seen that the implementation of the excuse to leave the function interface specifically achieve the implementation of multiple inheritance method. This ensures uniqueness of the single inheritance parent class, but also guarantees the advantages of multiple inheritance. I personally think is a very good design method, and this method is used in many places in my programming. So good, it will be here. Next time