搜索

查看: 3044|回复: 11

[ASP.NET] C#抽象类的用法介绍

[复制链接]
发表于 2023-5-4 11:33:15 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:33:15 3044 11 看全部
假设有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个或多个类中有重复部分的时候,我们可以抽象出来一个基类,如果希望这个基类不能被实例化,就可以把这个基类设计成抽象类。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
回复

使用道具 举报

发表于 2023-6-28 20:35:14 | 显示全部楼层
冀苍鸾 2023-6-28 20:35:14 看全部
感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
回复

使用道具 举报

发表于 2023-6-29 03:33:01 | 显示全部楼层
哈哈SE7 2023-6-29 03:33:01 看全部
楼主,大恩不言谢了!知鸟论坛是最棒的!
回复

使用道具 举报

发表于 2023-6-29 17:39:36 | 显示全部楼层
风吹吹蛋蛋疼风w 2023-6-29 17:39:36 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-29 23:23:08 | 显示全部楼层
我是的十八簿 2023-6-29 23:23:08 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-30 00:10:45 | 显示全部楼层
素色流年783 2023-6-30 00:10:45 看全部
楼主,我太崇拜你了!我想我是一天也不能离开知鸟论坛
回复

使用道具 举报

发表于 2023-6-30 01:39:26 | 显示全部楼层
123456809 2023-6-30 01:39:26 看全部
楼主,大恩不言谢了!知鸟论坛是最棒的!
回复

使用道具 举报

发表于 2023-6-30 02:03:12 | 显示全部楼层
123456823 2023-6-30 02:03:12 看全部
楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
回复

使用道具 举报

发表于 2023-6-30 09:09:08 | 显示全部楼层
伊索谗言 2023-6-30 09:09:08 看全部
我看不错噢 谢谢楼主!知鸟论坛越来越好!
回复

使用道具 举报

发表于 2023-7-3 07:16:38 | 显示全部楼层
心随674 2023-7-3 07:16:38 看全部
其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
回复

使用道具 举报

  • 您可能感兴趣
点击右侧快捷回复 【请勿灌水】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

RSS订阅| SiteMap| 小黑屋| 知鸟论坛
联系邮箱E-mail:zniao@foxmail.com
快速回复 返回顶部 返回列表