zoukankan      html  css  js  c++  java
  • C#之override学习案例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace override_test
    {
        class TestOverride
        {
            public class Employee
            {
                public string name;
                protected decimal basepay;
                public Employee(string name, decimal basepay)  //构造函数
                {
                    this.name = name;
                    this.basepay = basepay;
                }
                public virtual decimal CalculatePay()
                {
                    return basepay;
                }
            }
            public class SalesEmployee : Employee    //实现继承
            {
                private decimal salesbonus;
                public SalesEmployee(string name, decimal basepay, decimal salesbonus)
                    : base(name, basepay)
                {
                    this.salesbonus = salesbonus;
                }
                public override decimal CalculatePay()
                {
                    return basepay + salesbonus;
                }
            }
            static void Main()
            {
                SalesEmployee employee1 = new SalesEmployee("Alice",1000,500);
                Employee employee2 = new Employee("Bob", 1200);
                Console.WriteLine("Employee" + employee1.name + "earned:" + employee1.CalculatePay());
                Console.WriteLine("Employee"+employee2.name+"earned:"+employee2.CalculatePay());
            }
        }
    }             //override的作用就是用来重写基类的方法,如果不重写且基类方法依然为virtual,则类似于普通的继承

  • 相关阅读:
    Feature分支
    Bug分支
    分支管理策略
    解决冲突
    创建与合并分支
    分支管理
    Java并发与多线程教程(1)
    Java Thread(线程)案例详解sleep和wait的区别
    Spring在Thread中注入Bean无效的解决方式
    浅谈Java中的hashcode方法
  • 原文地址:https://www.cnblogs.com/zztong/p/6695221.html
Copyright © 2011-2022 走看看