【问题标题】:method resolution with base types基类型的方法解析
【发布时间】:2011-04-14 17:35:05
【问题描述】:

我的情况是这样的:

public class InheritedClass : BaseClass
{
    public override void SomeMethod()
    {
        AnotherMethod();
    }
    public override void AnotherMethod()
    {
    }
}

public class BaseClass
{
    public virtual void SomeMethod()
    { }
    public virtual void AnotherMethod()
    { }
}

那么当我调用InheritedClassInstance.SomeMethod 时会调用哪个方法呢?它是调用InheritedClassInstance.AnotherMethod,还是BaseClass 的AnotherMethod

【问题讨论】:

    标签: c# inheritance method-resolution-order


    【解决方案1】:

    它调用InheritedClassInstance.AnotherMethod()

    如果你想让它调用基类AnotherMethod(),你可以写base.AnotherMethod()

    【讨论】:

      【解决方案2】:

      它将调用继承类的派生方法,除非您显式调用基方法 (base.AnotherMethod())

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-11
        • 1970-01-01
        • 2011-02-09
        • 2015-10-22
        • 1970-01-01
        • 2010-12-23
        • 1970-01-01
        相关资源
        最近更新 更多