假设有2个类,一个类是主力球员,一个类是替补球员。
public class NormalPlayer{public int ID { get; set; }public string FirstName { get; set; }public string LastName { get; set; }public decimal WeekSalary { get; set; }public string GetFullName(){return this.FirstName + " " + this.LastName;}public decimal GetDaySalary(){return WeekSalary/7;}}public class SubPlayer{public int ID { get; set; }public string FirstName { get; set; }public string LastName { get; set; }public decimal MonthSalary { get; set; }public string GetFullName(){return this.FirstName + " " + this.LastName;}public decimal GetWeekSalary(){return MonthSalary/4;}}
我们发现,NormalPlayer和SubPlayer有共同的属性和方法,当然也有不同的属性和方法。把2个类的共同部分抽象出一个基类。
public class BasePlayer{public int ID { get; set; }public string FirstName { get; set; }public string LastName { get; set; }public string GetFullName(){PR
新闻热点
疑难解答