【发布时间】:2015-11-17 18:03:23
【问题描述】:
当子类同时继承父类和接口时,为什么子类不能访问父类方法?
考虑以下情况,我无法访问 DoSomething() 方法。
class Program
{
static void Main(string[] args)
{
IMyInterface myClass = null;
myClass = new ChildClass();
// this returns error
myClass.DoSomething();
}
}
internal class ParentClass
{
public ParentClass() { }
public void DoSomething() { }
}
internal class ChildClass : ParentClass, IMyInterface
{
public string MyProperty { get; set; }
public ChildClass() : base() { }
}
internal interface IMyInterface
{
string MyProperty { get; set; }
}
我查看了 SO 讨论 here 和 here,但他们似乎关注如何使用 new、override 和 virtual 关键字隐藏成员......抱歉,但我不知道如何适用于这种情况。此外,我浏览了接口 here 和 here 上的 MSDN API 参考,但没有运气。
【问题讨论】:
-
因为您将
myClass声明为IMyInterface,它没有DoSomething的定义。
标签: c# inheritance interface