[英]How can I have a class derived from a class known at runtime implementing an interface known at compilation
[英]Interface inheritance - hiding method from parent interface - why can I have both methods in the implementing class?
我試圖了解接口的繼承方式。 當一個接口繼承另一個接口但具有一個具有相同簽名的方法時,編譯器會警告該基本成員已隱藏(這似乎是合乎邏輯的)。
但是,當我稍后在類中實現子接口時,我必須實現具有相同簽名的父接口和子接口方法,這使我感到困惑……我認為當父成員隱藏時,我將需要實現只有子界面方法?
(我在查看IEnumerable
和IEnumerable<T>
接口時遇到了這個問題)
interface IMyBase
{
int DoSomething();
}
interface IMyDerived:IMyBase
{
int DoSomething();
}
class myClass : IMyDerived
{
int IMyDerived.DoSomething()
{
throw new NotImplementedException();
}
int IMyBase.DoSomething()
{
throw new NotImplementedException();
}
}
我必須實現具有相同簽名的父接口和子接口方法,這使我感到困惑
那么,如果您這樣做,您會期望發生什么?
IMyBase x = new myClass();
x.DoSomething();
有兩種方法IMyBase.Something
和IMyDerived.Something
都必須存在,因為兩者都可以被調用。 myClass
仍實現IMyBase
,該接口未以任何方式隱藏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.