2015-06-17 52 views
1

目前我有以下內容:接口子類,從父類繼承未經重新實現父類

public class ChildClass : ParentClass 
{... 

父類如下實現一個接口(I需要父類被實例化,因此不能是抽象的) :

public class ParentClass : IParentClass 
{... 

我也想子類實現一個接口,這樣我可以嘲笑這個類,但我想父類的繼承成員可見到ChildClass接口。因此,如果我在父類中有方法MethodA(),我希望此方法能夠在使用IChildClass而不僅僅是ChildClass時調用。

我能想到的唯一的辦法就是要覆蓋的方法在ChildClass,定義IChildClass該方法與只調用base.MethodA(),但是這並沒有真正似乎正確

回答

5

如果我理解正確的話,你說你想在你的接口和你的類中使用繼承層次結構。

這是你將怎樣實現這樣的事情:

public interface IBase 
{ 
    // Defines members for the base implementations 
} 

public interface IDerived : IBase 
{ 
    // Implementors will be expected to fulfill the contract of 
    // IBase *and* whatever we define here 
} 

public class Base : IBase 
{ 
    // Implements IBase members 
} 

public class Derived : Base, IDerived 
{ 
    // Only has to implement the methods of IDerived, 
    // Base has already implement IBase 
} 
+0

非常感謝,完美的作品! –

2

我覺得你可以做兩件事情。

1)您可以繼承多個接口。 C#支持這個。您只能從一個基類繼承,但您可以從多個接口繼承。

2)你可以讓你的接口彼此繼承。 IChildClass接口可以繼承IParentClass接口。

這有幫助嗎?