C # multiple inheritance

zhaozj2021-02-16  49

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 {////

///// public abstract class mybase {public mybase () {// // Todo: Add constructor logic //}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

Using system;

Namespace INTDV {///

/// /// public class myderive1: intedv.mybase {string myname; public myderive1 () {// // Todo: Add constructor logic /////

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 {///

/// /// public class myderive2: intDv.mybase {int myage; public myderive2 () {// // Todo: Add constructor logic ////

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 sealed class myMDerive: Intdv.myBase, Intdv.ImyDerive1, Intdv.ImyDerive2 {Intdv.myDerive1 d1; Intdv.myDerive2 d2; public myMDerive () {// // Todo: Add constructor logic // d1 = new myderive1 (); d2 = new myderive2 ();

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 {///

/// Class1. /// class class1 {/// /// The primary entry point of the application. /// [stathread] static void main (string [] args) {// // Todo: Add code here to start the application // INTDV.MYMDERIVE MD = new mymderive (); md.showmyage (); Md.showmyname ();

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

转载请注明原文地址:https://www.9cbs.com/read-27436.html

New Post(0)