Cri*_*scu 6 .net c# inheritance interface explicit-implementation
那个头衔是满口的,不是吗?......
这是我正在尝试做的事情:
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 */ }
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,调用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 */
Run Code Online (Sandbox Code Playgroud)
那么... IFoo.Bar(){...}除了基本上将两个接口合并为一个之外,有没有办法在我的课堂中声明?
如果没有,为什么?
可以在接口中使用new关键字来显式隐藏它扩展的接口中声明的成员:
public interface IBar
{
void Bar();
}
public interface IFoo:IBar
{
void Foo();
new void Bar();
}
public class Class1 : IFoo
{
void Bar(){}
void IFoo.Foo(){}
void IFoo.Bar(){}
void IBar.Bar(){}
}
Run Code Online (Sandbox Code Playgroud)
你没有IFoo;的两个实现 你只有一个。
CLR 不区分来自接口树中不同点的接口副本。
特别是,没有办法调用IFoo.Bar(); 你只能打电话IBar.Bar。
如果您添加一个单独的Bar()方法IFoo,您的代码将起作用。
| 归档时间: |
|
| 查看次数: |
684 次 |
| 最近记录: |