method resolution with base types

懵懂的女人 提交于 2019-12-12 01:58:32

问题


My situation is this:

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

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

So which method is called when I call InheritedClassInstance.SomeMethod? Does it call InheritedClassInstance.AnotherMethod, or the BaseClass's AnotherMethod?


回答1:


It calls InheritedClassInstance.AnotherMethod()

If you wanted it to call the base class AnotherMethod() you would write base.AnotherMethod()




回答2:


It will call the derived method on the inherited class unless you explicitly call the base method (base.AnotherMethod())



来源:https://stackoverflow.com/questions/5667232/method-resolution-with-base-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!