zoukankan      html  css  js  c++  java
  • Base

    base 关键字用于从派生类中访问基类的成员:

    • 调用基类上已被其他方法重写的方法。

    • 指定创建派生类实例时应调用的基类构造函数。

    基类访问只能在构造函数、实例方法或实例属性访问器中进行。

    从静态方法中使用 base 关键字是错误的。

    所访问的基类是类声明中指定的基类。 例如,如果指定 class ClassB : ClassA,则无论 ClassA 的基类如何,从 ClassB 上访问 ClassA 的成员

     
    基类 Person 和派生类 Employee 都有一个名为 Getinfo 的方法。 通过使用 base 关键字,可以从派生类中调用基类的 Getinfo 方法。
     
    public class Person
        {
            protected string ssn = "444-55-6666";
            protected string name = "John L. Malgraine";
    
            public virtual void GetInfo()
            {
                Console.WriteLine("Name: {0}", name);
                Console.WriteLine("SSN: {0}", ssn);
            }
        }
        class Employee : Person
        {
            public string id = "ABC567EFG";
            public override void GetInfo()
            {
                // Calling the base class GetInfo method:
                base.GetInfo();
                Console.WriteLine("Employee ID: {0}", id);
            }
        }
    
        class TestClass
        {
            static void Main()
            {
                Employee E = new Employee();
                E.GetInfo();
            }
        }
        /*
        Output
        Name: John L. Malgraine
        SSN: 444-55-6666
        Employee ID: ABC567EFG
        */
    
    public class Person
        {
            protected string ssn = "444-55-6666";
            protected string name = "John L. Malgraine";
    
            public virtual void GetInfo()
            {
                Console.WriteLine("Name: {0}", name);
                Console.WriteLine("SSN: {0}", ssn);
            }
        }
        class Employee : Person
        {
            public string id = "ABC567EFG";
            public override void GetInfo()
            {
                // Calling the base class GetInfo method:
                base.GetInfo();
                Console.WriteLine("Employee ID: {0}", id);
            }
        }
    
        class TestClass
        {
            static void Main()
            {
                Employee E = new Employee();
                E.GetInfo();
            }
        }
        /*
        Output
        Name: John L. Malgraine
        SSN: 444-55-6666
        Employee ID: ABC567EFG
        */
     
     
     
     
     
     
     
     

     

     
     
  • 相关阅读:
    WPF-ListView单元格设置文字换行
    WPF-GridView设置列宽按比例分配
    C#控制台输入密码星号显示
    安装pycharm
    超强的链接2
    redis
    day 46 htmljianjie
    day45 html超文本标记语言
    43 非阻塞 io多路复用
    42 队列 线程池 协程 geven模块 协程完成的socket
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/3999102.html
Copyright © 2011-2022 走看看