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


    假设有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个或多个类中有重复部分的时候,我们可以抽象出来一个基类,如果希望这个基类不能被实例化,就可以把这个基类设计成抽象类。

     

     

  • 相关阅读:
    python基础-------模块与包(一)
    python基础-------函数(三)
    python基础-------函数(二)
    python基础-------函数(一)
    python基础(三)----字符编码以及文件处理
    python基础(二)-------数据类型
    python基础(一)------Python基础语法与介绍
    linux进程、软件包的安装和删除,及安装python3.6(源代码方式)
    Linux磁盘分区、打包压缩、软硬链接练习
    linux关于目录或文件权限的练习
  • 原文地址:https://www.cnblogs.com/darrenji/p/3908618.html
Copyright © 2020-2023  润新知