关键词的几种用法

2008-09-05 15:30:45.0     浏览:673     来源:中国IT实验室
关键词:  C#编程     关键词  

注意:在不隐藏继承成员的声明中使用 new 修饰符将生成警告。

  示例

  在该例中,嵌套类 MyClass 隐藏了基类中具有相同名称的类。该例不仅说明了如何使用完全限定名访问隐藏类成员,同时也说明了如何使用 new 修饰符消除警告消息。

以下是引用片段:
  1 using System;
  2
  3 public class MyBaseC
  4
  5 {
  6
  7 public class MyClass
  8
  9 {
  10
  11 public int x = 200;
  12
  13 public int y;
  14
  15 }
  16
  17 }
  18
  19
  20
  21 public class MyDerivedC : MyBaseC
  22
  23 {
  24
  25 new public class MyClass // nested type hiding the base type members
  26
  27 {
  28
  29 public int x = 100;
  30
  31 public int y;
  32
  33 public int z;
  34
  35 }
  36
  37
  38
  39 public static void Main()
  40
  41 {
  42
  43 // Creating object from the overlapping class:
  44
  45 MyClass S1 = new MyClass();
  46
  47
  48
  49 // Creating object from the hidden class:
  50
  51 MyBaseC.MyClass S2 = new MyBaseC.MyClass();
  52
  53
  54
  55 Console.WriteLine(S1.x);
  56
  57 Console.WriteLine(S2.x);
  58
  59 }
  60
  61 }
  62


  输出

  100

  200

[上一页]   [第1页]   [第2页]