• 继承(父类为虚方法以及子类的重写)


    定义父类为虚方法时需要关键字  virtual

    子类重写需要关键字  override

    示例代码:

     //父类
       public class People
        {
            //虚方法
            //VR Virtual Reality  虚拟现实
            //AR Augmented Reality 增强现实
            public virtual void SayHello()
            {
                Console.WriteLine("父类打招呼的方法");
            }

    }

     //子类(重写父类方法)
      public  class Chinese:People
        {
          public override void SayHello()
          {
              Console.WriteLine("你好!");
          }
        }


        //子类
        public class American:People
        {
            public override void SayHello()
            {
                Console.WriteLine("Hello");
            }
        }

     public  static void Main(string[] args)
            {
              //统一调用
                List<People> list = new List<People>()
               {
                new Chinese(),
                new American()
               };

                People people = new People();
                people.SayHello();
                foreach (People item in list)
                {
                    item.SayHello();
                }
                Console.ReadKey();
            }

  • 相关阅读:
    USACO3.4.3Electric Fence
    (转)《算法艺术与信息学竞赛》题目 提交方式对照表
    UVA10382 Watering
    SGU104 Little shop of flowers
    UVA10673 Play with Floor and Ceil
    SGU123 The sum
    SGU106 The equation
    SGU105 Div 3
    UVA10905 Children's Game
    SGU101 200分类
  • 原文地址:https://www.cnblogs.com/sujulin/p/7066314.html
Copyright © 2020-2023  润新知