zoukankan      html  css  js  c++  java
  • C# 4.0 类与继承

    1.隐藏基类成员

        派生类不能删除它继承的任何成员,但可以隐藏它。

        1.1 要隐藏一个继承的数据成员,需要声明一个新的相同类型的成员,并使用相同的名称。

        1.2 通过在派生类中声明新的带有相同的签名的函数成员,可以隐藏基类的函数成员。(不包含返回值类型)。

        1.3 使用new修饰符隐藏基类成员。(也可以隐藏静态成员)

        举例说明 new 和Override的区别

        1.4使用override 声明Print

     1  class MyBaseClass
     2 
     3   {
     4 
     5 vittual public void Print(){Console.WriteLine("this is the base class");} 
     6 
     7  }    
     8 
     9 class MyDerivedClass : MybaseClass
    10 
    11   {
    12 
    13 override public void Print(){Console.WriteLine("this is the derived class");} 
    14 
    15  }    
    16 
    17 class Progrem
    18 
    19 {
    20 
    21 static void Main()
    22 
    23 {
    24 
    25 MyDerivedClass derived=new MyDerivedClass();
    26 
    27 MyBaseClass mybc=(MybaseClass)derived;
    28 
    29 derived.Print();
    30 
    31 mybc.Print();
    32 
    33 }
    34 
    35 }

      

    输出结果为:

    this is the derived class

    this is the derived class

      1.5使用new声明Print.

      

     1 class MyBaseClass
     2 
     3   {
     4 
     5 vittual public void Print(){Console.WriteLine("this is the base class");} 
     6 
     7  }    
     8 
     9 class MyDerivedClass : MybaseClass
    10 
    11   {
    12 
    13 new public void Print(){Console.WriteLine("this is the derived class");} 
    14 
    15  }    
    16 
    17 class Progrem
    18 
    19 {
    20 
    21 static void Main()
    22 
    23 {
    24 
    25 MyDerivedClass derived=new MyDerivedClass();
    26 
    27 MyBaseClass mybc=(MybaseClass)derived;
    28 
    29 derived.Print();
    30 
    31 mybc.Print();
    32 
    33 }
    34 
    35 }

    输出结果为:

    this is the derived class

    this is the base class

  • 相关阅读:
    stack2
    xctf 实时数据监测
    note-service2
    stack pivot学习
    sctf_2019_easy_heap 利用off-by-null构造出double free来向任一地址写入(经典)
    ciscn_2019_s_1 unlink或者of-by-null
    starctf_2019_babyshell 绕过循环检测注入shellcode
    sublime 安装package control
    windows上安装nodejs,升级npm,安装webpack指南
    mysql 常见语句
  • 原文地址:https://www.cnblogs.com/maomihaozi/p/3827287.html
Copyright © 2011-2022 走看看