一個組件向外界展示了幾個接口(IFirst
,ISecond
,IThird
),即那些接口是public
。來自內部的公共接口?
現在,作爲一個實現細節,所有這些對象都有一個共同的特徵,由接口IBase
描述。我不想公開IBase
,因爲這可能會在將來的實現中發生變化,並且與我的程序集的用戶完全無關。
很明顯,公共接口不能從內部派生(給我一個編譯器錯誤)。
有沒有什麼辦法可以表達IFirst
,ISecond
和IThird
只有從內部的角度纔有共同之處?
一個組件向外界展示了幾個接口(IFirst
,ISecond
,IThird
),即那些接口是public
。來自內部的公共接口?
現在,作爲一個實現細節,所有這些對象都有一個共同的特徵,由接口IBase
描述。我不想公開IBase
,因爲這可能會在將來的實現中發生變化,並且與我的程序集的用戶完全無關。
很明顯,公共接口不能從內部派生(給我一個編譯器錯誤)。
有沒有什麼辦法可以表達IFirst
,ISecond
和IThird
只有從內部的角度纔有共同之處?
不是在C#
你能做的最好的是有你的類實現內部和公共接口。
正如Andrew上面所說。只是爲了擴大,這裏是一個代碼示例:
public interface IFirst
{
string FirstMethod();
}
public interface ISecond
{
string SecondMethod();
}
internal interface IBase
{
string BaseMethod();
}
public class First: IFirst, IBase
{
public static IFirst Create() // Don't really need a factory method;
{ // this is just as an example.
return new First();
}
private First() // Don't really need to make this private,
{ // I'm just doing this as an example.
}
public string FirstMethod()
{
return "FirstMethod";
}
public string BaseMethod()
{
return "BaseMethod";
}
}
public class Second: ISecond, IBase
{
public static ISecond Create() // Don't really need a factory method;
{ // this is just as an example.
return new Second();
}
private Second() // Don't really need to make this private,
{ // I'm just doing this as an example.
}
public string SecondMethod()
{
return "SecondMethod";
}
public string BaseMethod()
{
return "BaseMethod";
}
}