我今天在C#中学习了一些OOP概念,在学习的过程中我发现子类Object能够直接从超类中调用一个被重写的方法,我熟悉Java,如果从超类中重写一个方法如果一个子类对象正在调用相同的方法,那么执行子类中存在的那个,而不是超类方法,我们使用"super"关键字.
我的Que是C#如何提供这样的功能,它直接允许子类对象执行被覆盖的超类方法
下图是子类对象"obj"允许我通过给出"void Super.display"选项调用超类方法显示的代码.

使用 base.MethodName()
但你需要定义你的父母的方法virtual,并override在子类中的方法.根据你的形象,你没有这样做
看这个例子
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
*/
Run Code Online (Sandbox Code Playgroud)