开发者

method resolution with base types

开发者 https://www.devze.com 2023-02-24 14:19 出处:网络
My situation is this: public class InheritedClass : BaseClass { 开发者_开发百科public override void SomeMethod()

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?


It calls InheritedClassInstance.AnotherMethod()

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消