C # The interface is something that makes it easy for beginners to confuse. It seems very simple to use. Define the interface and then define it in it. Method To complete the specific implementation through inheritance and its subclass. However, if you do not really understand the functions of an interface, you may think that using an interface is an extra step. Of course, this is absolutely wrong. An important principle in software design is interface-oriented programming, dependency and interface or abstraction layer. It can be seen how important an interface is in real development.
In the previous article C # programming tool: Class, I introduced the knowledge of classes. This article mainly introduces another important knowledge point in OO programming-interfaces. To some extent, an interface is also a class, a special class or abstract class. More accurately, an interface only contains the signature of a method, delegate, or event. The method implementation is completed in the class implementing the interface [MSDN].
I. Interface Definition
As defined in the interface above on MSDN, the interface only contains the method, delegate, or event signature. The more common explanation of this sentence is that the interface is only responsible for defining operations, rather than implementing specific details. The following IPlayer interface is an interface for playing games. It only defines the corresponding method without the specific implementation of the method. The Code is as follows:
1/** // <summary>
2 // game playing Interface
3 /// </summary>
4 public interface IPlayer
5 {
6/** // <summary>
7 // obtain the player name
8 /// </summary>
9 /// <returns> player name </returns>
10 string GetName ();
11
12/*** // <summary>
13 // The Player determines the gesture.
14 /// </summary>
15 /// <returns> one of the three constants defined by this interface </returns>
16 string Show ();
17}
The above is the definition of a typical interface. An interface named IPlayer is defined, and two internal methods are defined: GetName and Show. In addition to defining methods in interfaces, we can also define attributes, indexes, and events. For details, refer to the definitions on MSDN or related books. Here we will take attributes as an example to briefly introduce them, the interface can only be defined but not implemented. The specific implementation is completed by its subclass. How should the attribute be defined? We usually define the following attributes:
1/** // <summary>
2 // define the _ Name attribute and provide the get; set attribute accessors
3 /// </summary>
4 private string _ Name;
5 public string Name
6 {
7 get {return _ Name ;}
8 set {_ Name = value ;}
9}
So how does one define attributes in the interface and implement them in its subclass? The following code snippet:
1/** // <summary>
2 // define the interface and define an attribute named Name in the interface
3 /// </summary>
4 public interface IAttribute
5 {
6 string Name {get; set ;}
7}
8/** // <summary>
9 // define a class to inherit the IAttribute interface and implement its attributes
10 /// </summary>
11 public class Component: IAttribute
12 {
13 public string Name
14 {
15 get
16 {
17 return "Zhang San ";
18}
19 set
20 {
21 this. Name = value;
22}
23}
24}
Ii. Interface implementation
At the beginning of this article, I have said that an interface is only responsible for definition and not implementation. The specific implementation is done by its subclass. OK. Now we will take the IPlayer interface for playing games defined above as an example to briefly introduce the interface implementation.
Let's take the case of playing with scissors and stone headers in my interesting programming. Grandpa and Grandma taught me everything from childhood, and they often used to play ghost games, but little grandson does not know how to change. Every time he outputs scissors, can he win his grandfather? What can we do with this analysis? The interface is defined above. Is it OK to directly implement this interface. When Grandpa and Sun Tzu play games, they define two classes to inherit the IPlayer interface. The Code is as follows:
1/** // <summary>
2 // action status
3 /// </summary>
4 public class Options
5 {
6 public static readonly string JIANDAO = "Scissors ";
7 public static readonly string SHITOU = "Stone ";
8 public static readonly string BU = "cloth ";
9}
Only these three action states appear in the game, so we can encapsulate them. Here we use class encapsulation. Of course, we can also encapsulate them using other related technologies, for example, the Structure and enumeration introduced in the second article C # programming tool 2: Structure and enumeration in this series, in this example, the three unchanged states can be completely encapsulated by structure or enumeration. For details, please read the previous article. The following defines the Grandpa class and Grandson class to implement the interface (IPlayer. The Code is as follows:
1/** // <summary>
2 // Grandpa-one of the players
3 /// </summary>
4 public class Grandpa: IPlayer
5 {
6 public string GetName ()
7 {
8 return "grandpa ";
9}
10
11 public string Show ()
12 {
13 Random random = new Random ();
14 int I = (int) (random. Next () * 1000) % 3;
15 switch (I)
16 {
17 case 0: return Options. JIANDAO;
18 case 1: return Options. SHITOU;
19 default: return Options. BU;
20}
21}
22}
1/** // <summary>
2 // Sun Tzu-one of the players
3 /// </summary>
4 public class Grandson: IPlayer
5 {
6 public string GetName ()
7 {
8 return "grandson ";
9}
10
11 public string Show ()
12 {
13 return Options. JIANDAO;
14}
15}
As shown above, our GrandPa and GrandSon implement the IPlayer interface, as shown in:
Iii. interface inheritance
We will not introduce this in detail here. We just need to remember that there is a long-lived sentence: "An interface can be inherited from one or more basic interfaces ". Schematic code:
1interface IA { }
2interface IB:IA { }
3interface IC : IA, IB { }
4interface ID : IA, IB, IC { }
Iv. interface features
In addition Method It can also contain attributes, indexers, events, and so on, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).
In terms of type, the interface is of reference type. Similar to a class, the interface has three similarities with the abstract class:
1. It cannot be instantiated;
2. contains an unimplemented method statement;
3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members );
V. interfaces and callbacks
Normally, we create an object and directly go Use Its method. However, in some cases, you want to call the method of this object only when a scenario appears or the condition is met. The callback can solve the problem of "delay calling object method. The called method object is called a callback object.
First, create a callback object, and then create a controller object to notify the Controller object of the method that the callback object needs to be called. the Controller object is responsible for checking whether a scenario exists or whether certain conditions are met. when this scenario occurs or this condition is met, the method of the callback object is automatically called. the schematic code is as follows:
1 using System;
2 using System. Collections. Generic;
3 using System. Text;
4
5 namespace CallBack
6 {
7 class Program
8 {
9 static void Main (string [] args)
10 {
11 // create a controller object and pass in the callback object provided to it
12 Resolve resolve = new Resolve (new PlayBasketball ());
13 resolve. Play ();
14
15 resolve = new Resolve (new PlayFootball ());
16 resolve. Play ();
17}
18}
19
20/*** // <summary>
21 // define an interface-callback object
22 /// </summary>
23 public interface IPlayer
24 {
25 void Play ();
26}
27
28/** // <summary>
29 // basketball
30 /// </summary>
31 public class PlayBasketball: IPlayer
32 {
33 public void Play ()
34 {
35 Console. WriteLine ("playing basketball ");
36}
37}
38
39/** // <summary>
40 // football
41 /// </summary>
42 public class PlayFootball: IPlayer
43 {
44 public void Play ()
45 {
46 Console. WriteLine ("playing football ");
47}
48}
49
50/** // <summary>
51 // control role -- controller object
52 /// </summary>
53 public class Resolve
54 {
55 // hold a reference to an interface, by constructing Method Initialization
56 private IPlayer player;
57 public Resolve (IPlayer player)
58 {
59 this. player = player;
60}
61
62 public void Play ()
63 {
64 player. Play ();
65}
66}
67}
This article describes the interface-related knowledge points. For more details about the interface, please refer to the relevant materials.
Source: http://beniao.cnblogs.com/