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

假设有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()
        {
            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;
        }
    }
 
 

 

接着,我们发现NormalPlayer和SubPlayer计算日薪和周薪的方法也可以抽象出来,作为虚方法放到基类中。

 
 
    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;
        }
 
 
    }
 
 

 

但在实际情况中,BasePlayer只是一个抽象出来的类,我们并不希望实例化这个类。这时候,就可以把BasePlayer设计为abstract抽象类。同时,在抽象类中,提供一个计算薪水的抽象方法。一旦在基类中声明了没有方法体的抽象方法,所有派生于这个抽象类的类必须实现或重写基类中的抽象方法。

 
 
    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();
    }
 
 

 

由此可见,当2个或多个类中有重复部分的时候,我们可以抽象出来一个基类,如果希望这个基类不能被实例化,就可以把这个基类设计成抽象类。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值