• 重写、隐藏基类方法(new, override)


     public class Father
        {
            public void Write() {
                Console.WriteLine("");
            }
        }
    
        public class Mother
        {
            public virtual void Write()
            {
                Console.WriteLine("");
            }
        }
    
        public class Boy : Father
        {
            public new void Write()
            {
                Console.WriteLine("");
            }
        }
    
        public class Girl : Mother
        {
            public override void Write()
            {
                Console.WriteLine("女");
            }
        }
            static void Main(string[] args)
            {
                Father father = new Boy();
                father.Write();
    
                Boy boy = new Boy();
                boy.Write();
    
    
                Mother mother = new Mother();
                mother.Write();
    
                Girl girl = new Girl();
                girl.Write();
    
                Console.ReadLine();
            }

    输出:




    添加调用父方法:

        public class Boy : Father
        {
            public new void Write()
            {
                base.Write();
                Console.WriteLine("");
            }
        }
    
        public class Girl : Mother
        {
            public override void Write()
            {
                base.Write();
                Console.WriteLine("");
            }
        }

    输出:






    可见,在程序运行结果上new 和override是一样的。

  • 相关阅读:
    5、垂直拆分---分库--mycat
    4、读写分离---双主双从(mycat)
    3、读写分离---一主一从(mycat)
    2、安装启动(Mycat)
    1、入门(Mycat)
    Nginx 相关参数记录(2)
    Nginx 相关参数记录(1)
    Linux
    一大波学习内容!
    开源镜像站
  • 原文地址:https://www.cnblogs.com/season2009/p/2941427.html
Copyright © 2020-2023  润新知