扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:佚名 来源:中国自学编程网 2007年11月19日
关键字:
注意:在不隐藏继承成员的声明中使用 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
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。