Interface (interface)
An interface refers to an abstraction that an entity provides itself to the outside world (another entity) that separates external communication methods from internal operations so that they can be modified internally without affecting the way other entities interact with the outside world.
An interface is actually a convention :
such as:iclonable, IComparable;
An interface is a collection of abstract members:
ICloneable contains Method clone ();
IComparable contains method compare ();
An interface is a reference type that is more abstract than abstract.
Help implement multiple Inheritance:
The use of interfaces:
1. implement the same behavior of unrelated classes without having to consider the hierarchical relationships of these classes;
2. interfaces allow you to understand the interface of an object without needing to know the class in which the object resides. For example: Publicsealed class string:icloneable, IComparable, IConvertible, IEnumerable.
Define an interface:
Public Interface istringlist{voidstring s); int Get ; } string This [intgetset;}} // Public Abstract Default, these two keywords do not write out
Implementing the interface:
class class Name: [Parent class,] interface, interface, interface, ..... interface { public method () {...}}
Display interface member implementations:
When implementing multiple interfaces, if different interfaces have a method of the same name, in order to disambiguate, you need to write the interface name before the method name: void Iwindow.close () {...};
When invoked, it can only be called with an interface: ((Iwindow) f). Close ();
interface Example:
usingSystem;namespacetestinterface{InterfaceRunner {voidrun (); } InterfaceSwimmer {voidswim (); } Abstract classAnimal {Abstract Public voideat (); } classPerson:animal, Runner, Swimmer { Public voidRun () {Console.WriteLine ("Run"); } Public voidswim () {Console.WriteLine ("Swim"); } Public Override voidEat () {Console.WriteLine ("Eat"); } Public voidspeak () {Console.WriteLine ("speak"); } } classProgram {Static voidM1 (Runner R) {R.run (); } Static voidm2 (Swimmer s) {s.swim (); } Static voidm3 (Animal a) {a.eat (); } Static voidM4 (Person p) {p.speak (); } Public Static voidMain (string[] args) {Person P=NewPerson (); M1 (P); M2 (p); M3 (p); M4 (P); Runner a=NewPerson (); A.run (); Console.readkey (true); } }}using the interface sample
interface in C #