很多人糾結於接口只是一個標准
下面我們來看這樣一個問題
話說有家影視公司選拔偶像派男主角
[csharp] public class Actor
{
private string name;
private int height;
public Actor(string name
{
this
this
}
public string Name
{
get { return this
}
public int Height
{
get { return this
}
public int CompareTo(object obj)
{
return this
}
public string GetName()
{
return this
}
}
public class Actor
{
private string name;
private int height;
public Actor(string name
{
this
this
}
public string Name
{
get { return this
}
public int Height
{
get { return this
}
public int CompareTo(object obj)
{
return this
}
public string GetName()
{
return this
}
}
這個類
有了這個類
現在的問題是
只是
這等於又重新編寫了一個程序
這時
我先做一個接口
[csharp] namespace WestGarden
{
public interface ISelectPlayer
{
string GetName()
int CompareTo(object obj)
}
}
namespace WestGarden
{
public interface ISelectPlayer
{
string GetName()
int CompareTo(object obj)
}
}
這個接口
我們把剛才做的男演員的類
[csharp] using System;
using WestGarden
namespace WestGarden
{
public class Actor:ISelectPlayer
{
private string name;
private int height;
public Actor(string name
{
this
this
}
public string Name
{
get { return this
}
public int Height
{
get { return this
}
public int CompareTo(object obj)
{
return this
}
public string GetName()
{
return this
}
}
}
using System;
using WestGarden
namespace WestGarden
{
public class Actor:ISelectPlayer
{
private string name;
private int height;
public Actor(string name
{
this
this
}
public string Name
{
get { return this
}
public int Height
{
get { return this
}
public int CompareTo(object obj)
{
return this
}
public string GetName()
{
return this
}
}
}
順手
[csharp] using System;
using WestGarden
namespace WestGarden
{
public class Actress:ISelectPlayer
{
private string name;
private int weight;
public Actress(string name
this
this
}
public string Name
{
get { return this
}
public int Weight
{
get { return this
}
public int CompareTo(object obj)
{
return ((Actress)obj)
}
public string GetName()
{
return this
}
}
}
using System;
using WestGarden
namespace WestGarden
{
public class Actress:ISelectPlayer
{
private string name;
private int weight;
public Actress(string name
this
this
}
public string Name
{
get { return this
}
public int Weight
{
get { return this
}
public int CompareTo(object obj)
{
return ((Actress)obj)
}
public string GetName()
{
return this
}
}
}
這時
[csharp] protected void Page_Load(object sender
{
Actor actor
Actor actor
Actress actress
Actress actress
WhoIsBetter(actor
WhoIsBetter(actress
}
public void WhoIsBetter(ISelectPlayer a
{
if (a
Response
else
Response
}
protected void Page_Load(object sender
{
Actor actor
Actor actor
Actress actress
Actress actress
WhoIsBetter(actor
WhoIsBetter(actress
}
public void WhoIsBetter(ISelectPlayer a
{
if (a
Response
else
Response
}
注意
我們做的這個函數
這個函數
你實現接口的類是男演員也好
這和那個比方是一樣的
這個
From:http://tw.wingwit.com/Article/program/net/201311/13799.html