zoukankan      html  css  js  c++  java
  • 重写、隐藏基类方法(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是一样的。

  • 相关阅读:
    Seial port API and tool
    Simple HTTPD
    VC与Cygwin的结合
    zlib
    嵌入式开发系统编程文件格式解析
    ZB4O
    Wireshark基本介绍和学习TCP三次握手
    freeware
    Console2 A Better Windows Command Prompt
    iniparser
  • 原文地址:https://www.cnblogs.com/season2009/p/2941427.html
Copyright © 2011-2022 走看看