0
如果我有一個Bag類型的結構實現接口IGetProps,並且我有一個具有Bag類型成員變量的類商店,我可以在Store的實現中指定我希望Store類通過它的成員提供IGetProps型袋。C#:我可以指定一個類的成員結構是一個繼承接口的實現者嗎?
包不能更改爲一個類,以便我可以繼承它。 IGetProps有很多方法,所以我不想用Store中的方法顯式包裝每個方法。
如:
interface IGetProps
{
int GetA();
int GetB();
}
struct Bag : IGetProps
{
public int GetA() { return 0;}
public int GetB() { return 1;}
... // Many more methods
}
class Store : IGetProps
{
private Bag bag; // <--- Can I designate bag to be the provide of IGetProps for Store?
}
不,你不能。 但是,您可以從包中繼承商店。 – dgorti
@dgorti類不能從結構繼承。 http://stackoverflow.com/questions/15408667/inherit-from-struct – Nico
爲什麼'Bag'是一個結構?既然你指出它有更多的方法_,這似乎表明應該使用'class',因爲結構通常意味着用值類型語義來建模不可變對象。 –