【发布时间】:2011-05-28 22:25:17
【问题描述】:
这个标题很拗口,不是吗?...
这就是我想要做的:
public interface IBar {
void Bar();
}
public interface IFoo: IBar {
void Foo();
}
public class FooImpl: IFoo {
void IFoo.Foo() { /* works as expected */ }
//void IFoo.Bar() { /* i'd like to do this, but it doesn't compile */ }
//so I'm forced to use this instead:
void IBar.Bar() { /* this would compile */ }
}
我的问题是……调用 Bar() 很不方便:
IFoo myFoo = new FooImpl();
//myFoo.Bar(); /* doesn't compile */
((IBar)myFoo).Bar(); /* works, but it's not necessarily obvious
that FooImpl is also an IBar */
所以...除了基本上将两个接口合并为一个之外,有没有办法在我的班级中声明IFoo.Bar(){...}?
如果不是,为什么?
【问题讨论】:
-
我认为你需要让 IFoo 实现来自 IBar 的方法 Bar
-
@Stephan H - 接口不能实现任何东西;他们只能继承。你的意思可能是别的吗?
-
一个聪明的程序员知道什么是他必须关心的,什么是无关紧要的。 ...我说对了吗?
标签: c# .net inheritance interface explicit-implementation