2011-02-24 106 views
2

我寫過這段代碼。實現通用接口

public interface IMusicInstrument 
{ 
    string InstrumentType(int InstrumentId);     
} 

public interface IGuitar<T, K> where T : GuitarBase where K : IMusicInstrument 
{ 
    string Name { get; set; } 
    string GetType(T t); 
} 

public class ElectricGuitar : GuitarBaseExtended, IGuitar<ElectricGuitar, IMusicInstrument> 
{ 
    public string Name { get; set; } 
    public string GetType(ElectricGuitar t) 
    { 
     return "The electric guitar is: " + t.Name; 
    } 
} 

我在想,因爲IMusicInstrument需要我實現了一個名爲InstrumentType方法,當我把它作爲我的IGuitar接口的參數,其在興田電吉他類,我會得到一個編譯時錯誤執行。但是,我沒有。

我執行錯了嗎?

我的目標是將IMusicInstrument實現爲通用類型的IGuitar接口,以滿足IMusiInstrument和IGuitar的要求。

任何想法?

回答

1

你還沒有說ElectricGuitar實現IMusicInstrument ...你剛剛說,它實現IGuitar<ElectricGuitar, IMusicInstrument>,並沒有InstrumentType方法。目前還不清楚爲什麼根據IGuitar<T, K>中的K類型參數,因爲它沒有在接口中使用。