2010-11-09 49 views
1

我得到編譯錯誤,這個過於複雜的類層次結構。我不知道是否有任何與嘗試做deepcopy的(),在混合泛型實現抽象方法本身就是一個通用接口方法的實現

public interface IInterface<T> 
{ 
    IInterface<T> DeepCopy(); 
} 

public abstract class AbstractClass<T> : IInterface<T> 
{ 
    public abstract IInterface<T> DeepCopy(); // Compiler requires me to declare this public 
} 

// Everything good at this point. There be monsters below 

public class ConcreteClass: AbstractClass<SomeOtherClass> 
{ 
    ConcreteClass IInterface<SomeOtherClass>.DeepCopy() 
    { 
     return new ConcreteClass; 
    } 
} 

我得到以下編譯器錯誤:

'IInterface<...>.DeepCopy()': containing type does not implement interface 'IInterface<SomeOtherClass>' 
+1

當您刪除MyMethod的顯式接口前綴時會發生什麼? – davisoa 2010-11-09 15:45:19

+0

編輯該問題以將MyMethod更改爲DeepCopy(因爲它實際上位於我的工作區中,我吮吸匿名)。卸下顯式接口前綴導致編譯器錯誤說我應該實現抽象類 .DeepCopy() – Jeremy 2010-11-09 15:56:26

+0

你錯過ACLASS關鍵字,應該是'公共抽象類抽象類' – 2010-11-09 15:57:36

回答

3

返回布爾

變化 ConcreteClass IInterface<SomeOtherClass>.MyMethod()
bool IInterface<SomeOtherClass>.MyMethod()

編輯:
然後你不能使用接口的顯式實現,因爲它不能滿足你需要像這樣實現它的抽象類的契約。

public override IInterface<SomeOtherClass> DeepCopy() 
{ 
    return new ConcreteClass(); 
} 
+0

該死的,犯了一個錯誤的消毒代碼。更新我的問題。 – Jeremy 2010-11-09 15:47:21

+0

@Jeremy,我更新了我的答案以及如何以及爲什麼需要更改您的ConcreteClass。 – 2010-11-09 15:58:43

+0

你明白了。通過自己的嘗試和錯誤釘牢它。 – Jeremy 2010-11-09 16:00:00

2

的錯誤是因爲DeepCopy()返回類型不匹配的接口的聲明。

除此之外,你有一個不同的問題。抽象類已經從接口實現了這個方法,但是在具體的類中你並沒有實現抽象方法。相反,你現在有實現的,你應該有下面的實現:

public override IInterface<SomeOtherClass> DeepCopy() 
{ 
} 

這將實現在抽象類,自動實現接口方法的抽象方法。您需要在抽象類中實現抽象方法的原因是因爲該類需要實現接口。這是一個班級的要求。

+0

該死的,對代碼進行了清理。更新我的問題 – Jeremy 2010-11-09 15:50:11

+0

更新的問題沒有任何意義。該接口沒有「MyMethod」方法,並且具有顯式實現(方法之前的'IInterface <...> .')實現了一個不存在的方法。你可以再檢查一次嗎? – 2010-11-09 15:51:30

+0

道歉先生,我的意思是使所有方法DeepCopy。再次更新它。 – Jeremy 2010-11-09 15:54:06