Run virtual method in inherit class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smrah1@gmail.com

    Run virtual method in inherit class

    - Hi

    - we have like below code:

    namespace ConsoleApplicat ion1
    {
    class Program
    {
    static void Main(string[] args)
    {
    A a = new C();
    a.Write();
    }
    }

    class A
    {
    public virtual void Write()
    {
    Console.WriteLi ne("Class A");
    }
    }

    class B : A
    {
    public override void Write()
    {
    Console.WriteLi ne("Class B");
    }
    }

    class C : B
    {
    public override void Write()
    {
    Console.WriteLi ne("Class C");
    base.Write();
    }
    }
    }

    - When run ,outout is:

    Class C
    Class B

    - Now I want change below line of class C:

    base.Write();

    - that output will like :

    Class C
    Class A

    - in Other words,change overrided method of class C that no run method
    of base (class B) but run this method on - class A (base of base of
    class C).

    - thanks
    - SMRAH1

  • Peter Duniho

    #2
    Re: Run virtual method in inherit class

    On Tue, 21 Oct 2008 02:24:48 -0700, <smrah1@gmail.c omwrote:
    [...]
    - in Other words,change overrided method of class C that no run method
    of base (class B) but run this method on - class A (base of base of
    class C).
    As far as I know, without the cooperation of class B, you can't. But, you
    can obviously add a method in B that calls "base.Write ()", providing an
    explicit means to do so.

    Pete

    Comment

    Working...