問這個問題的更好的方法將是一個例子如下 這兩種方法的優缺點是什麼?一個人總是比另一個人好還是在特定情況下?如果使用Approach1,使用一個接口將會是正確的嗎?因爲任何人都可以訪問公共方法?如果使用接口,應該一個類總是嚴格執行一個接口
public interface IDoSomething
{
void Method1(string operation, User user, string category)
void Method2(string operation, User user)
void Method3(string operation)
}
//Approach1
Class A: IDoSomething
{
public void Method1(string operation, User user, string category)
{
//do some db logic here...
}
public void Method2(string operation, User user)
{
Method1(operation, user, "General");
}
public void Method3(string operation)
{
Method1(operation, User.GetDefaultUser(), "General");
}
}
OR
//Approach2
Class A: IDoSomething
{
void IDoSomething.Method1(string operation, User user, string category)
{
//do some logic here...
}
void IDoSomething.Method2(string operation, User user)
{
(this as IDoSomething).Method1(operation, user, "General");
}
void IDoSomething.Method3(string operation)
{
(this as IDoSomething).Method1(operation, User.GetDefaultUser(), "General");
}
}
哇,不知道顯式實現沒有繼承。有趣的知道。 – 2010-09-28 16:34:18
@達倫:這些方法是繼承的,但不是以可覆蓋的方式 - 它們永遠不是虛擬的。說實話,這有點奇怪。 – 2010-09-28 16:43:31
想要重寫本身就是一個強烈的表示,即成員應該是公開的,或者至少直接調用派生類(可能是前者)可見的東西。 – 2010-09-28 16:53:47