• 抽象类和虚方法


    namespace ConsoleApplication1
    {
      
        public abstract class door
        {
            public void show()
            {
                Console.WriteLine("this is a");
            }
    
            public virtual void talk()
            {
                Console.WriteLine("this is t1");
            }
    
            public abstract void run();
        }
    
        public class door1:door
        {
            public new void show()
            {
                Console.WriteLine("this is b");
            }
    
            public override void talk()
            {
                Console.WriteLine("this is t2");
            }
    
            public override void run()
            {
                Console.WriteLine("this is run1");
            }
        }
        
    
    
    
    }

    program.cs中:

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                door d = new door1();
                d.show();
                d.talk();
                d.run();
    
                Console.ReadLine();
    
            }
        }
    }

    结果:

    实例化时,若左边是父类右边是子类,实例化结束调用方法时,普通方法执行父类方法,虚方法执行子类方法

  • 相关阅读:
    Java的Set集合中的retainAll()方法
    蒲丰投针问题
    根据贝叶斯公式求解三门问题
    概率论公式(续)
    数字(number)
    寻找最美的你(select)
    木棍
    lowbit
    搜索题
    day7
  • 原文地址:https://www.cnblogs.com/wy1992/p/7485257.html
Copyright © 2020-2023  润新知