using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace override_test { class TestOverride { public class Employee { public string name; protected decimal basepay; public Employee(string name, decimal basepay) //构造函数 { this.name = name; this.basepay = basepay; } public virtual decimal CalculatePay() { return basepay; } } public class SalesEmployee : Employee //实现继承 { private decimal salesbonus; public SalesEmployee(string name, decimal basepay, decimal salesbonus) : base(name, basepay) { this.salesbonus = salesbonus; } public override decimal CalculatePay() { return basepay + salesbonus; } } static void Main() { SalesEmployee employee1 = new SalesEmployee("Alice",1000,500); Employee employee2 = new Employee("Bob", 1200); Console.WriteLine("Employee" + employee1.name + "earned:" + employee1.CalculatePay()); Console.WriteLine("Employee"+employee2.name+"earned:"+employee2.CalculatePay()); } } } //override的作用就是用来重写基类的方法,如果不重写且基类方法依然为virtual,则类似于普通的继承