考慮這個例子:重載一個抽象方法
public interface IAccount
{
string GetAccountName(string id);
}
public class BasicAccount : IAccount
{
public string GetAccountName(string id)
{
throw new NotImplementedException();
}
}
public class PremiumAccount : IAccount
{
public string GetAccountName(string id)
{
throw new NotImplementedException();
}
public string GetAccountName(string id, string name)
{
throw new NotImplementedException();
}
}
protected void Page_Load(object sender, EventArgs e)
{
IAccount a = new PremiumAccount();
a.GetAccountName("X1234", "John"); //Error
}
我怎樣才能調用從客戶端重寫的方法,而不必抽象/接口上定義一個新的方法簽名(因爲它僅僅是一個特殊的高級賬戶的情況)?我在這個設計中使用抽象工廠模式...謝謝...
是的,正如我所說,這只是特殊情況,因爲抽象圖案有特殊的情況下,像這樣沒有具體的解決方案... – CSharpNoob 2010-10-30 16:45:32