javapublic class Player { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public decimal WeekSalary { get; set; }}
假设有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;
}
}
public class BasePlayer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
}
然后让先前的2个类派生于这个基类。
public class NormalPlayer: BasePlayer
{
public decimal WeekSalary { get; set; }
public decimal GetDaySalary()
{
return WeekSalary/7;
}
}
public class SubPlayer : BasePlayer
{
public decimal MonthSalary { get; set; }
public decimal GetWeekSalary()
{
return MonthSalary/4;
}
}
public class BasePlayer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
public virtual decimal GetSalary()
{
throw new NotImplementedException();
}
}
在NormalPlayer和SubPlayer这2个派生类中,需要重写基类的虚方法。
public class NormalPlayer: BasePlayer
{
public decimal WeekSalary { get; set; }
//获取日薪
public override decimal GetSalary()
{
return WeekSalary / 7;
}
}
public class SubPlayer : BasePlayer
{
public decimal MonthSalary { get; set; }
//获取周薪
public override decimal GetSalary()
{
return MonthSalary / 4;
}
}
public abstract class BasePlayer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
public abstract decimal GetSalary();
}
javapublic class Player { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public decimal WeekSalary { get; set; }}
假设有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;
}
}
public class BasePlayer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
}
然后让先前的2个类派生于这个基类。
public class NormalPlayer: BasePlayer
{
public decimal WeekSalary { get; set; }
public decimal GetDaySalary()
{
return WeekSalary/7;
}
}
public class SubPlayer : BasePlayer
{
public decimal MonthSalary { get; set; }
public decimal GetWeekSalary()
{
return MonthSalary/4;
}
}
public class BasePlayer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
public virtual decimal GetSalary()
{
throw new NotImplementedException();
}
}
在NormalPlayer和SubPlayer这2个派生类中,需要重写基类的虚方法。
public class NormalPlayer: BasePlayer
{
public decimal WeekSalary { get; set; }
//获取日薪
public override decimal GetSalary()
{
return WeekSalary / 7;
}
}
public class SubPlayer : BasePlayer
{
public decimal MonthSalary { get; set; }
//获取周薪
public override decimal GetSalary()
{
return MonthSalary / 4;
}
}
public abstract class BasePlayer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
public abstract decimal GetSalary();
}