版权声明:盗版必究 https://blog.csdn.net/jinxiul5/article/details/81942003
方法替换
特点:方法替换主用运用new将想要继承的方法合法化重写
例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Person
{
public void PerRun()
{
Console.WriteLine ("正长运动");
}
}
public class Student:Person
{
//如果不适用new 关键字,这里会报警告。
public new void PerRun()
{
Console.WriteLine ( "百米冲刺");
}
}
class Program
{
static void Main(string[] args)
{
//使用父类调用子类方法时会执行父类方法
Person p = new Student ( );
//打印“正长运动”
p.PerRun ( );
//要想执行子类的方法需要实例化子类
Student s = new Student ( );
//打印“百米冲刺”
s.PerRun ( );
Console.ReadKey ( );
}
}
}