• 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();
            }
        }
  • 相关阅读:
    ListView
    Android 控件知识点
    Android之alertDialog、ProgressDialog
    Android 控件知识点,
    知晓当前是哪一个活动
    Activity的启动模式
    iOS9之Bitcode
    Xcode7免证书真机调试实践
    XCode6之后预编译文件的创建
    Swift计算文本宽高
  • 原文地址:https://www.cnblogs.com/han1982/p/2935727.html
Copyright © 2020-2023  润新知