开发者

Overriding, Overridden Function

开发者 https://www.devze.com 2023-03-20 16:49 出处:网络
here\'s what I am trying to do class A { virtual void foo(); } class B : A { virtual override void foo(); } class C : B

here's what I am trying to do

class A
{
    virtual void foo();
}

class B : A
{
    virtual override void foo();
}

class C : B
{
    override void foo();
}

so what I w开发者_C百科ant to see when calling C.foo() is A.foo(), B.foo(), C.foo()

but I dont think virtual override can be used in the same function definition. How would I go around this?

Thanks -Mike


Overridden functions are automatically virtual, unless explicitly declared sealed.

Note that calling C.Foo() will not call B.Foo() or A.Foo() unless C.Foo manually calls base.Foo().


code should work like this:

public class A
{
    public virtual void foo() {}
}

public class B : A
{
    public override void foo() {}
}

class C : B
{
    public override void foo() {}
}

so long as foo is accessible. no need for 'virtual' in B


Is this what you're looking for?

class A
    {
        public virtual void foo()
        {
            MessageBox.Show("A");
        }
    }

    class B : A
    {
        public override void foo()
        {
            MessageBox.Show("B");
            base.foo();
        }
    }

    class C : B
    {
        public override void foo()
        {
            MessageBox.Show("C");
            base.foo();
        }
    }


C c = new C();
c.foo();
0

精彩评论

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