【发布时间】:2011-05-21 05:32:12
【问题描述】:
我有以下代码
public interface IFoo
{
void Bar();
}
public class Parent : IFoo
{
public virtual void Bar(){}
}
public class Child : Parent, IFoo
{
public override void Bar(){}
}
IFoo test = new Child();
test.Bar();
test.Bar() 总是调用父方法
任何帮助将不胜感激
【问题讨论】:
-
您发布的代码无法编译:
IFoo.Bar中不能有public修饰符。因此,我们无法判断您的代码有什么问题。请张贴真正的代码剥离到你的问题的核心。很可能您在Parent中显式实现IFoo.Bar,因此Child不会覆盖它。 -
您将不得不提供更多代码。该测试导致
Child.Bar在这里运行。 (而且您的接口定义在语法上不正确——接口成员没有可见性说明符。) -
您的示例无法编译。你的意思是让 Parent 成为一个抽象类吗?
-
你的代码工作正常,它调用了子方法。
标签: c# inheritance interface overriding virtual