• C# 虚方法(virtual)


    使用base调用父类中的虚方法。

    class School
        {
            private string _name;
            public School(){ }
            public School(string name)
            {
                _name = name;
            }
            public virtual void Method_virtual()   //虚方法
            {
                Console.Write(_name + ":");
            }
        }
        class Teacher : School
        {
            public Teacher(string name):base(name){ }
            public override void Method_virtual()
            {
                base.Method_virtual();
                Console.WriteLine("给学生们上课!");
            }
        }
        class Student : School
        {
            public Student(string name) : base(name) { }
            public override void Method_virtual()
            {
                base.Method_virtual();
                Console.WriteLine("同学上台演讲!");
            }
        }
        class Monitor : School
        {
            public Monitor(string name) : base(name) { }
            public override void Method_virtual()
            {
                base.Method_virtual();
                Console.WriteLine("班长上台预留课后作业!");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                School[] school = new School[4];
                school[0] = new Teacher("李老师");
                school[1] = new Student("王二");
                school[2] = new Student("张三");
                school[3] = new Monitor("刘四");
                Console.WriteLine("上课铃声响了!");
                foreach (School s in school)
                {
                    s.Method_virtual();
                }
                Console.ReadKey();
            }
        }
  • 相关阅读:
    重写
    mongodb版本区别
    mysql备份还原
    mysql备份恢复
    mysql的锁
    mysql索引
    mysql日志详解
    mysql基本语法
    mysql主从bin-log的三种方式
    mysql的GTID主从复制方式
  • 原文地址:https://www.cnblogs.com/han1982/p/2935727.html
Copyright © 2020-2023  润新知