电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

简单的实例理解接口的伟大意义


发布日期:2021/3/14
 
首先我们必须明确接口是一个类

接口是一个特殊的类又是一个特别有意义的类不是因为它的特殊而是因为它的意义叫它接口更合适但不能忘了它仍是类

接口是一个只有声明没有实现的类

很多人纠结于接口只是一个标准是一个契约而忘记了它的意义

下面我们来看这样一个问题

话说有家影视公司选拔偶像派男主角导演说了男演员身高是王道于是有下面代码

[csharp] public class Actor

{

private string name;

private int height;

public Actor(string name int height)

{

thisname = name;

thisheight = height;

}

public string Name

{

get { return thisname; }

}

public int Height

{

get { return thisheight; }

}

public int CompareTo(object obj)

{

return thisheight ((Actor)obj)height;

}

public string GetName()

{

return thisname;

}

}

public class Actor

{

private string name;

private int height;

public Actor(string name int height)

{

thisname = name;

thisheight = height;

}

public string Name

{

get { return thisname; }

}

public int Height

{

get { return thisheight; }

}

public int CompareTo(object obj)

{

return thisheight ((Actor)obj)height;

}

public string GetName()

{

return thisname;

}

}

这个类除了可以存放男演员的基本信息还定义了一个函数publicint CompareTo(object obj)因为我们要比较男演员的身高用身高判断哪个演员更好

有了这个类后面你可以比较轻松地编写代码判断是刘德华更优秀还是潘长江更优秀了这个代码我这里就略过去了…

现在的问题是明天又要选拨女演员了导演说了女演员苗条是王道女演员的这个类你肯定是要做的只是…

只是我刚才略过去的让你编写的代码你是不是还要再重新编写呢????

这等于又重新编写了一个程序

这时我们就想到了接口我们来接着看代码吧

我先做一个接口这个接口

[csharp] namespace WestGardenIPlayer

{

public interface ISelectPlayer

{

string GetName()

int CompareTo(object obj)

}

}

namespace WestGardenIPlayer

{

public interface ISelectPlayer

{

string GetName()

int CompareTo(object obj)

}

}

这个接口定义了两个函数一个当然是要进行比较标准由你定你说是导演定的那更好不用你费脑子了

我们把刚才做的男演员的类按照这个接口的标准来实现也就是继承这个接口

[csharp] using System;

using WestGardenIPlayer;

namespace WestGardenDAL

{

public class Actor:ISelectPlayer

{

private string name;

private int height;

public Actor(string name int height)

{

thisname = name;

thisheight = height;

}

public string Name

{

get { return thisname; }

}

public int Height

{

get { return thisheight; }

}

public int CompareTo(object obj)

{

return thisheight ((Actor)obj)height;

}

public string GetName()

{

return thisname;

}

}

}

using System;

using WestGardenIPlayer;

namespace WestGardenDAL

{

public class Actor:ISelectPlayer

{

private string name;

private int height;

public Actor(string name int height)

{

thisname = name;

thisheight = height;

}

public string Name

{

get { return thisname; }

}

public int Height

{

get { return thisheight; }

}

public int CompareTo(object obj)

{

return thisheight ((Actor)obj)height;

}

public string GetName()

{

return thisname;

}

}

}

顺手把女演员的类也做了吧

[csharp] using System;

using WestGardenIPlayer;

namespace WestGardenDAL

{

public class Actress:ISelectPlayer

{

private string name;

private int weight;

public Actress(string name int weight){

thisname = name;

thisweight = weight;

}

public string Name

{

get { return thisname; }

}

public int Weight

{

get { return thisweight; }

}

public int CompareTo(object obj)

{

return ((Actress)obj)weight thisweight;

}

public string GetName()

{

return thisname;

}

}

}

using System;

using WestGardenIPlayer;

namespace WestGardenDAL

{

public class Actress:ISelectPlayer

{

private string name;

private int weight;

public Actress(string name int weight){

thisname = name;

thisweight = weight;

}

public string Name

{

get { return thisname; }

}

public int Weight

{

get { return thisweight; }

}

public int CompareTo(object obj)

{

return ((Actress)obj)weight thisweight;

}

public string GetName()

{

return thisname;

}

}

}

这时我们在应用层这样编写代码

[csharp] protected void Page_Load(object sender EventArgs e)

{

Actor actor = new Actor(潘长江

Actor actor = new Actor(刘德华

Actress actress = new Actress(巩俐

Actress actress = new Actress(周迅

WhoIsBetter(actor actor

WhoIsBetter(actress actress

}

public void WhoIsBetter(ISelectPlayer a ISelectPlayer b)

{

if (aCompareTo(b) >

ResponseWrite(aGetName())

else

ResponseWrite(bGetName())

}

protected void Page_Load(object sender EventArgs e)

{

Actor actor = new Actor(潘长江

Actor actor = new Actor(刘德华

Actress actress = new Actress(巩俐

Actress actress = new Actress(周迅

WhoIsBetter(actor actor

WhoIsBetter(actress actress

}

public void WhoIsBetter(ISelectPlayer a ISelectPlayer b)

{

if (aCompareTo(b) >

ResponseWrite(aGetName())

else

ResponseWrite(bGetName())

}

注意

我们做的这个函数publicvoid WhoIsBetter(ISelectPlayer aISelectPlayer b)

这个函数形参是ISelectPlayer是接口我认为接口的意义就在这里

你实现接口的类是男演员也好女演员也好男主角也好女主角也好男配角也好女本角也好男群众演员也好女群众演员也好只要你继承的是我这个ISelectPlayer或者你习惯于说遵守了我这个接口的标准或者契约我这段代码都不需要改变!!

这和那个比方是一样的不管你插在USB接口的是U盘还是移动硬盘还是什么mp还是mp还是你新发明的什么东西只要你能插在我的USB口上我主机都不需要做任何改变直接在上面读取或者写入数据

这个是硬件接口的意义所在也是我们这个ISelectPlayer类的意义所在因为它有了这个伟大的意义才把它改叫为接口的因为它象USB接口一样工作着……

上一篇:一个基于UDP的聊天应用程序

下一篇:颜色#RRGGBB与string 相互转换