首页 > 学院 > 开发设计 > 正文

为什么使用抽象类,什么时候使用抽象类

2019-11-17 02:58:46
字体:
来源:转载
供稿:网友

为什么使用抽象类,什么时候使用抽象类

假设有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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表