C # interface Example (simple and classic) 2008/12/04 10: 04 using system; using system. collections. generic; using system. text; using system. threading; namespace apptest {class demo_interface {static void main (string [] ARGs) {// use (Note: here the interface iPrint is used. Below are different instances for different functions) iPrint I; I = new A (); // instantiate a and print out a I. print (); I = new B (); // instantiate B and print B I. print (); thread. sleep (3*1000); // pause for 3 seconds and view the printed result} // define the interface. It does not need to be changed after definition. It requires different functions and is extended during inheritance. Interface iPrint {// The interface only contains methods, attributes, indexers, and event signatures, but does not support defining fields and implementation methods. // supports multi-inheritance; it can be used to support callback. It can act on the Value Type and reference type. C # cannot contain any static members. // It cannot be directly instantiated. It can be implemented through inheritance. Void print (); // only defined, not implemented // iperson (); // error // string name; // error // public void getidcard (); // error // void getname (); // correct // void getage (string S); // correct // int getheight (); // correct} // A inherits the Class A: iPrint {// implementation interface method, which must be, // Of course, you can also add other fields, attributes, and Methods public void print () {system. console. writeline ("A") ;}}// B inherits the Class B interface: iPrint {public void print () {system. console. writeline ("B") ;}}// in this example, the iPrint interface defines Print method, but no method body is implemented. // both Class A and Class B inherit the iPrint interface and implement different print methods. // call a \ B in main, obtain different print results ================================================ = well written, however, there is still a lack of examples. People can say that I can define print () and printpreview () two virtual functions on the base class, so that the classes of different printers can inherit this base class, the override virtual function can be implemented through the same basic class pointer. If you want to add a new feature such as scan () to the HP printer and IBM printer, you have to re-Modify the base class if you use the base class virtual function, modifying the base class will affect all the classes that inherit from the base class. It is very dangerous to modify the base class after the system is built. However, you only need to use the interface, the IBM print class inherits an iscan interface and does not affect other classes. This is where the interface is superior to the virtual function. Five object-oriented design principles single Responsibility Principle 2. principle 3. rys replacement principle definition: if every object O1 of type S has an object O2 of type T, replace O2 with O1 for any program P defined by type T, P is a subtype of T. 4. Dependency inversion principle 5. Interface isolation principle