考虑这段代码:
internal class Program
{
private static void Main(string[] args)
{
var student = new Student();
student.ShowInfo(); //output --> "I am Student"
}
}
public class Person
{
public void ShowInfo()
{
Console.WriteLine("I am person");
}
}
public class Student : Person
{
public void ShowInfo()
{
Console.WriteLine("I am Student");
}
}
在上面的代码中,我们不使用方法隐藏。
当创建学生实例并调用showinfo
方法时,我的输出是I am Student
我不使用new
关键字。
为什么不使用方法隐藏时不调用父方法?