After N years of use, do you know what the interface is? -- A simple instance describes the great significance of the interface

Source: Internet
Author: User

ASP. NETBasic knowledge: simple instances understand the great significance of interfaces

Westgarden

Http://www.cnblogs.com/WestGarden/

 

 

Source code: 13033480 group sharing

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. So there are the followingCode:

 

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 actors, but also defines a functionPublicIntCompareto (ObjectOBJ ),Because we need to compare the height of an actor and use height to determine which one 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.....

 

(Not suitable for children. 1000 rows are omitted 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 writing anotherProgram.

 

At this time, we thought of the interface. Let's look at the Code:

Let me first make an interface:

 

 
Using system; namespace westgarden. idal {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:

 

Using system; using westgarden. idal; 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:

 

Using system; using westgarden. idal; 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:

 

Using system; using westgarden. idal; using westgarden. dal; namespace westgarden. web {public partial class select: system. web. UI. page {protected void page_load (Object sender, eventargs e) {actor actor1 = new actor ("pan Changjiang", 150); actor actor2 = new actor ("Liu Dehua", 180 ); actress actress1 = new actress ("Gong Li", 120); actress actress2 = new actress ("Zhou Xun", 80); response. write ("Best Actor:" + whoisbetter (actor1, actor2) + "</BR>"); response. write ("Best Actress:" + whoisbetter (actress1, actress2) + "</BR>") ;}// it works like a USB port, whether you are inserting an actor or actress ..., as long as it inherits the iselectplayer interface. Public String whoisbetter (iselectplayer A, iselectplayer B) {If (A. compareto (B)> 0) return a. getname (); else return B. getname ();}}}

Note:

The function we are doing,PublicVoidWhoisbetter (IselectplayerA,IselectplayerB)

 

This function is namedIselectplayer, Is the interface, I think, the meaning of the interface is here.

 

Your interface class is either an actor, an actress, or an actress, or a heroine, or a supporting role, or a supporting role, or a male actor or a female actor, as long as you inherit thisIselectplayerOr, you are used to saying that the Code does not need to be changed because it complies with the standards or contracts of my interface !!

 

This is the same as that.USBThe interface isUDisk, mobile disk, or somethingMP3, OrMP4Or something you have invented, as long as you can insert it into myUSBOn the port, I do not need to make any changes on the host, and read or write data directly on it.

 

This is the meaning of the hardware interface.IselectplayerClass, because it has this great significance, it is called an interface, because it is likeUSBInterfaces work the same way.......

 

The ideas in this article come from blogs:

Http://www.cnblogs.com/WestGarden/

The example in this article comes from:

Http://www.cnblogs.com/wu-jian/archive/2012/05/24/2516284.html

Thank you!

Copyright: 2012. You are welcome to reprint it. Please indicate the source for reprinting. More Article See blog http://www.cnblogs.com/WestGarden/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.