Summary: Classes in C # do not support multi-inheritance, but you can use interfaces to implement multi-inheritance.
The four member types of an interface are method, attribute, event, and indexer, but they can only be defined. They cannot be set to specific values, and the interface does not contain the specific implementation of methods.
Note that fields are not included
Feature: similar to the abstract base class, that is, all the members of the interface must be implemented for any non-Abstract type of the inherited interface.
Classes and structures can be inherited from multiple interfaces
The interface itself can be inherited from multiple interfaces
Declaration: modifier interface Interface Name: List of inherited Interfaces
{Interface content}
Note: When an interface is declared, except the interface keyword and interface name, the interface name can be used by default.
You can use modifiers such as new. Public. Protected. Internal. Private to declare an interface, but the interface member must be public.
Interface implementation and inheritance:
Namespace _ Interface
{
Interface ipeople
{
String name {Get; set ;}
String sex {Get; set ;}
}
Interface iteacher: ipeople // inherits the public interface
{
Void teach ();
}
Interface istudent: ipeople
{
Void Study ();
}
Class program: ipeople, iteacher, istudent // multi-interface inheritance
{String name = "";
Public string name {get {return name;} set {name = value ;}}
String sex = "";
Public String sex {get {return sex;} set {sex = value ;}
}
Public void teach ()
{
Console. writeline (name + "" + sex + "instructor ");
}
Public void Study ()
{
Console. writeline (name + "" + sex + "student ");
}
Static void main (string [] ARGs)
{
Program program = new program (); // instantiate an object
Iteacher = Program; // use the derived class Object Instantiation interface iteacher
Iteacher. Name = "TM ";
Iteacher. Sex = "male ";
Iteacher. Teach ();
Istudent = Program; // use the Object Instantiation interface of the derived class istuent
Istudent. Name = "C #";
Istudent. Sex = "male ";
Istudent. Study ();
Console. Readline ();
}}}
Implementation of displaying interface members:
If the class implements two interfaces and the two interfaces contain members with the same signature, implementing this member in the class will result in both interfaces using this member as their implementation. If the two interface members implement different functions, an error may occur. You can create a member that is called only through this interface and is specific to this interface. The display interface member implementation uses the interface name and a period to name the class member.
The sample code is as follows: namespace _ display interface implementation
{
Interface myinterface1
{
Int add ();
}
Interface myinterface2
{
Int add ();
}
Class myclass: myinterface1, myinterface2
{
Int myinterface1.add () // display the implementation of interface members
{
Int x = 3;
Int y = 5;
Return X + Y;
}
Int myinterface2.add () // display the implementation of interface members
{
Int x = 3;
Int y = 5;
Int z = 7;
Return X + Y + z;
}
}
Class Program
{
Static void main (string [] ARGs)
{
Myclass = new myclass (); // The instantiated interface inherits the object of the class.
Myinterface1 myinterface1 = myclass; // use the interface to inherit the Object Instantiation interface of the class
Console. writeline (myinterface1.add (); // call the method in the interface using the interface object
Myinterface2 myinterface2 = myclass;
Console. writeline (myinterface2. Add ());
Console. Readline ();
}
}
}
Interface -- Interface