zoukankan      html  css  js  c++  java
  • 为什么使用抽象类,什么时候使用抽象类

    假设有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基础知识0-5(单双向队列)
    python基础知识0-4
    python函数篇0-1
    面试题17:合并两个排序的链表
    面试题16:反转链表
    面试题15:链表中倒数第k个结点
    面试题14:调整数组顺序使奇数位于偶数前面
    面试题13:在O(1)时间删除链表结点
    面试题12:打印1到最大的n位数
    面试题11:数值的整数次方
  • 原文地址:https://www.cnblogs.com/darrenji/p/3908618.html
Copyright © 2011-2022 走看看