First, we must make it clear that an interface is a class.
"An interface is a special class and a particularly meaningful class, not because it is special, but because of its meaning, it is called an interface that is more suitable, but cannot be forgotten, it is still a class."
"An interface is a class that only has declarations and is not implemented ."
Many people are entangled in the fact that interfaces are just a standard, a contract, and forget its meaning.
Let's look at this problem:
It is said that a film and television company selects an idol leading actor, and the director said, the height of the actor is king. The following code is available:
[Csharp] public class Actor
{
Private string name;
Private int height;
Public Actor (string name, int height)
{
This. name = name;
This. height = height;
}
Public string Name
{
Get {return this. name ;}
}
Public int Height
{
Get {return this. height ;}
}
Public int CompareTo (object obj)
{
Return this. height-(Actor) obj). height;
}
Public string GetName ()
{
Return this. name;
}
}
Public class Actor
{
Private string name;
Private int height;
Public Actor (string name, int height)
{
This. name = name;
This. height = height;
}
Public string Name
{
Get {return this. name ;}
}
Public int Height
{
Get {return this. height ;}
}
Public int CompareTo (object obj)
{
Return this. height-(Actor) obj). height;
}
Public string GetName ()
{
Return this. name;
}
}
This class not only stores the basic information of the actor, but also defines a function publicint CompareTo (object obj), because we need to compare the height of the actor and use height to determine which actor is better.
With this class, you can easily write code to determine whether Liu Dehua is better or pan Changjiang is better. This code is skipped here ....
The problem now is that we have to select an actress tomorrow. The director said, actress, SLIM is king. Actress, you must do it, just ....
I just skipped the previous step and asked you to write the code again. Do you have to write it again ????
This is equivalent to re-writing a program.
At this time, we thought of the interface. Let's look at the Code:
I will first create an interface, which is:
[Csharp] namespace WestGarden. IPlayer
{
Public interface ISelectPlayer
{
String GetName ();
Int CompareTo (object obj );
}
}
Namespace WestGarden. IPlayer
{
Public interface ISelectPlayer
{
String GetName ();
Int CompareTo (object obj );
}
}
This interface defines two functions, one of which, of course, is to be compared. The standard is determined by you. You say it is set by the Director. It is better, and you don't have to worry about it.
We implement the class of the actor just now according to the standard of this interface, that is, inherit this interface:
[Csharp] using System;
Using WestGarden. IPlayer;
Namespace WestGarden. DAL
{
Public class Actor: ISelectPlayer
{
Private string name;
Private int height;
Public Actor (string name, int height)
{
This. name = name;
This. height = height;
}
Public string Name
{
Get {return this. name ;}
}
Public int Height
{
Get {return this. height ;}
}
Public int CompareTo (object obj)
{
Return this. height-(Actor) obj). height;
}
Public string GetName ()
{
Return this. name;
}
}
}
Using System;
Using WestGarden. IPlayer;
Namespace WestGarden. DAL
{
Public class Actor: ISelectPlayer
{
Private string name;
Private int height;
Public Actor (string name, int height)
{
This. name = name;
This. height = height;
}
Public string Name
{
Get {return this. name ;}
}
Public int Height
{
Get {return this. height ;}
}
Public int CompareTo (object obj)
{
Return this. height-(Actor) obj). height;
}
Public string GetName ()
{
Return this. name;
}
}
}
Let's just take the actress's class as well:
[Csharp] using System;
Using WestGarden. IPlayer;
Namespace WestGarden. DAL
{
Public class Actress: ISelectPlayer
{
Private string name;
Private int weight;
Public Actress (string name, int weight ){
This. name = name;
This. weight = weight;
}
Public string Name
{
Get {return this. name ;}
}
Public int Weight
{
Get {return this. weight ;}
}
Public int CompareTo (object obj)
{
Return (Actress) obj). weight-this. weight;
}
Public string GetName ()
{
Return this. name;
}
}
}
Using System;
Using WestGarden. IPlayer;
Namespace WestGarden. DAL
{
Public class Actress: ISelectPlayer
{
Private string name;
Private int weight;
Public Actress (string name, int weight ){
This. name = name;
This. weight = weight;
}
Public string Name
{
Get {return this. name ;}
}
Public int Weight
{
Get {return this. weight ;}
}
Public int CompareTo (object obj)
{
Return (Actress) obj). weight-this. weight;
}
Public string GetName ()
{
Return this. name;
}
}
}
At this time, we write code at the application layer as follows:
[Csharp] protected void Page_Load (object sender, EventArgs e)
{
Actor actor1 = new Actor ("pan Changjiang", 150 );
Actor actor2 = new Actor ("Andy Lau", 180 );
Actress actress1 = new Actress ("Gong Li", 120 );
Actress actress2 = new Actress ("Zhou Xun", 80 );
WhoIsBetter (actor1, actor2 );
WhoIsBetter (actress1, actress2 );
}
Public void WhoIsBetter (ISelectPlayer a, ISelectPlayer B)
{
If (a. CompareTo (B)> 0)
Response. Write (a. GetName ());
Else
Response. Write (B. GetName ());
}
Protected void Page_Load (object sender, EventArgs e)
{
Actor actor1 = new Actor ("pan Changjiang", 150 );
Actor actor2 = new Actor ("Andy Lau", 180 );
Actress actress1 = new Actress ("Gong Li", 120 );
Actress actress2 = new Actress ("Zhou Xun", 80 );
WhoIsBetter (actor1, actor2 );
WhoIsBetter (actress1, actress2 );
}
Public void WhoIsBetter (ISelectPlayer a, ISelectPlayer B)
{
If (a. CompareTo (B)> 0)
Response. Write (a. GetName ());
Else
Response. Write (B. GetName ());
}
Note:
Publicvoid WhoIsBetter (ISelectPlayer a, ISelectPlayer B)
This function is named ISelectPlayer and an interface. In my opinion, the meaning of the interface is here.
The class you implement interfaces is either an actor or an actress, or a heroine or a supporting role, or an actress, or an actress, as long as you inherit my ISelectPlayer, Or, you are used to saying that my code does not need to be changed because it complies with the standards or contracts of my interface !!
This is the same as that. No matter whether you plug in a USB flash drive, a mobile hard disk, mp3, mp4, or something you have invented, as long as you can plug it into my USB port, the host does not need to make any changes and reads or writes data directly on it.
This is the meaning of the hardware interface and the meaning of our ISelectPlayer class, because it has this great significance, it is called an interface, because, it works like a USB interface ......
The ideas in this article come from blogs:
Http://www.cnblogs.com/WestGarden/
The example in this article comes from:
Http://www.bkjia.com/kf/201205/133389.html
Thank you!
Author: yousuosi