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

     

     

  • 相关阅读:
    Red Hat Enterprise Linux Server 5.5新增功能简介
    DNS Flood Detector让DNS更安全
    iPad之Linux平台实践
    Fedora 10下应用网络模拟器NS心得
    深入了解Linux远程桌面
    Linux下基于LDAP统一用户认证的研究
    Linux架设Jsp环境
    Fedora 13 Alpha测试手记横空出世
    高層タワー [MISSION LEVEL: B]
    機械の総合病院 [MISSION LEVEL: C]
  • 原文地址:https://www.cnblogs.com/darrenji/p/3908618.html
Copyright © 2011-2022 走看看