zoukankan      html  css  js  c++  java
  • C#多态联系之虚方法

     class Class1
        {
            static void Main(string[] args)
            {
                YuanGong yg = new YuanGong();
                JingLi jl = new JingLi();
                LaoBan lb = new LaoBan();
    
    
                //声明一个数组的父类
                YuanGong[] ayg = { yg, jl, lb };
                //遍历数组
                for (int i = 0; i < ayg.Length; i++)
                {
                    ayg[i].DaKa();
                }
                Console.ReadKey();
            }
    
        }
        public class YuanGong
        {
            //virtual虚拟,在子类中改变后,用父类实例化子类后可以调用子类方法
            public virtual void DaKa()
            {
                Console.WriteLine("员工九点打卡");
            }
        }
        public class JingLi:YuanGong
        {
            //override 接收对应 virtual
            public override void DaKa()
            {
                Console.WriteLine("经理十点打卡");
            }
        }
        public class LaoBan:YuanGong
        {
            public override void DaKa()
            {
                Console.WriteLine("老板不打卡");
            }
        }
  • 相关阅读:
    LibreOJ2095
    Codeforces
    LibreOJ2241
    LibreOJ2044
    LibreOJ2043
    LibreOJ2045
    LibreOJ2042
    LibreOJ2097
    洛谷P4175
    POJ2888
  • 原文地址:https://www.cnblogs.com/zywf/p/4515883.html
Copyright © 2011-2022 走看看