1. Interface definition interface is for unified standard, keyword interface. When defining an interface, write the letter I, such as interface, before the interface name.Ijiekou{string Fangfa ();//method without method body} 2. (1) The method inside the interface has no method body, no modifier, no implementation. (2) The interface also has the transitivity, cannot be instantiated, cannot write the method with the method body. (3) The ordinary class inherits the interface must implement its non-implementation method, the interface inheritance interface may not implement its method, the abstract class cannot inherit the interface. (4) A common class can inherit multiple interfaces. 3. Similarities and differences between interfaces and abstract classes
|
Abstract class |
Interface |
Vary |
Define with abstract |
Define with interface |
Only one class can be inherited |
can inherit multiple interfaces |
Non-abstract derived classes must implement abstract methods |
Classes that inherit an interface must implement all methods |
The Override keyword is required |
Direct implementation |
A method that can write a method body |
Can't write methods that have a method body |
With |
1. Neither can be instantiated |
2. Include methods that are not implemented |
3. Derived classes must implement methods that are not implemented |
4. The interface is passed as a parameter, the object that implements the interface is passed, the interface is returned as the return type, and the object that implements the interface is returned.
1 Public classA2 {3 InterfaceIjiekou ()//Define Interface Ijiekou4 {5 intNum ();6 }7 }8 9 Public classB:ijiekou//Class B Inheritance Interface IjiekouTen { One Public inta A { - Public intNum () - { the return 1; - } -}
1 Static voidMain (string[] args)2 {3Ijiekou J =NewB ();//interface Instantiation Class B4a.x =NewA ();//Instantiating Classes5X.chuancan (J);//to pass J into the Chuancan class.6 }7 Public intChuancan (Ijiekou i)//here is the use of the interface as a parameter8 {9Console.WriteLine (I.num)//The return value of the A method in the output Class BTen}
1 Static voidMain (string[] args)2 {3Ijiekou J =NewB ();4 Console.WriteLine (j.fanhui.num);5 }6 PublicIjiekou Fanhui ()//here is the interface as the return type7 {8Ijiekou JJ =NewB ();9 returnJJ;Ten } One}
. NET Beginner Interface First Contact