c# virtual 关键字 虚方法
1、简单的说,虚方法就是可以被子类重写的方法,如果子类重写了虚方法,那么运行时将使用重写后的逻辑,如果没有重写,则使用父类中虚方法的逻辑
class Program { static void Main(string[] args) { Person per = new Person(); Police pol = new Police(); Doctor doc = new Doctor(); Teacher tea = new Teacher(); per.Working(); pol.Working(); doc.Working(); tea.Working(); Console.ReadKey(); } } class Person { public virtual void Working() { Console.WriteLine("我是人类,有多种职业!"); } } class Police : Person { public override void Working() { Console.WriteLine("我是警察,治安保民!"); } } class Doctor : Person { public override void Working() { Console.WriteLine("我是医生,治病救人!"); } } class Teacher : Person { public override void Working() { Console.WriteLine("我是教师,教书育人!"); } }