In C# how can a sub class object can directly call

2019-07-18 19:54发布

问题:

I was learning some OOP concept in C# today and while learning i found that a sub class Object is able to call a method directly from its super class which is overridden, i am familiar with Java in which if a method is overridden from its super class and if a sub class object is calling the same method then the one which is present in a sub class is executed and not the super class method to do that we use "super" keyword.

My Que is how does C# provide such feature that directly allows a sub class object to execute the super class method which is overridden

The below image is the code in which the sub class object "obj" allows me to call the super class method display by giving an option "void Super.display"

回答1:

From the image, you haven't override the method from base class, instead you are hiding the method.

With method hiding you can't call base class method from the child class like in the screen shot you have:

Code from Screen-shot.

Sub obj = new Sub();
obj.display();// this will call the child class method

The reference to Super.display in intellisence is probably buggy, you can't call a hidden method from the base class like that.

(Also you should get a warning to use new keyword because of method hiding)

To achieve a proper override, your method in base class has to be virtual or abstract like:

public class Super
{
    public virtual void display()
    {
        Console.WriteLine("super/base class");
    }
}

public class Sub : Super
{
    public override void display()
    {
        Console.WriteLine("Child class");
    }
}

and then you can call it like:

Super obj = new Sub();
obj.display(); //child class

Super objSuper = new Super();
objSuper.display(); //base class method

If you want to call base class method from inside the child class then use the base keyword like:

public override void display()
{
    base.display();
    Console.WriteLine("Child class");
}


回答2:

use base.MethodName()

but you need to define your parent method as virtual, and override the method in the sub class. You aren't doing that according to your image

See this example

public class Person
    {
        protected string ssn = "444-55-6666";
        protected string name = "John L. Malgraine";

        public virtual void GetInfo()
        {
            Console.WriteLine("Name: {0}", name);
            Console.WriteLine("SSN: {0}", ssn);
        }
    }
    class Employee : Person
    {
        public string id = "ABC567EFG";
        public override void GetInfo()
        {
            // Calling the base class GetInfo method:
            base.GetInfo();
            Console.WriteLine("Employee ID: {0}", id);
        }
    }

    class TestClass
    {
        static void Main()
        {
            Employee E = new Employee();
            E.GetInfo();
        }
    }
    /*
    Output
    Name: John L. Malgraine
    SSN: 444-55-6666
    Employee ID: ABC567EFG
    */


标签: c# oop