All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
把WebForm数据导出到Excel中_[Asp.Net教程]
病毒及流氓软件自我复制的简单实现
Visual,Studio,2008,新功能界面_[Asp.Net教程]
对比两个DataTable是否相同的正确方法_[Asp.Net教程]
用ASP.NET,2.0,FormView控件控制显示_[Asp.Net教程]
用ASP.NET还原与恢复Sql,server_[Asp.Net教程]
Ajax 中的高级请求和响应_[AJAX教程]
XSL学习教程:运算符和函数_[XML教程]
如何让APACHE支持.htaccess_php资料_编程技术
asp.net中Session对象的概念以及属性
Reading number is top 10 pictures
恶搞漫画2
Born After 90 Beijing sports university campus flower photos2
关于海盗的研究
Perfect small Laurie2
毛俊杰-能量永动机
Fan bingbing black wings for platform and DanLuoWang believes beauty2
福利福利。。。。。。
Startling Russian girl blind date scene2
Female model behind the bitterness, often being overcharged4
这才是真正的人体艺术5
Download software ranking
I'm come from Beijing1
Unix video tutorial3
豪门浪荡史
终极变速大师Speeder3.26
致我们终将逝去的青春
The Bermuda triangle1
C#高级编程(第4版)
Tram sex maniac 2 (H) rar bag17
Photoshop 8.0图象编辑软件
天龙八部十二宫服务端
delv published in(发表于) 2014/1/27 6:49:27 Edit(编辑)
C#编程中的,New,关键词的几种用法_[Asp.Net教程]

C#编程中的,New,关键词的几种用法_[Asp.Net教程]

C#编程中的 New 关键词的几种用法_[Asp.Net教程]

  前段时间一个朋友问到C#的New关键字有几种用法,虽说在日常编程中经常用到这个小家伙,但它到底有几种用法还真没有留意过,现将从网上总结出的资料记下以供同仁学习。


  (1)new 运算符 用于创建对象和调用构造函数。


  (2)new 修饰符 用于隐藏基类成员的继承成员。


  (3)new 约束 用于在泛型声明中约束可能用作类型参数的参数的类型。


  new 运算符


  1.用于创建对象和调用构造函数


  例:Class_Test MyClass = new Class_Test();


  2.也用于为值类型调用默认的构造函数


  例:int myInt = new int();


  myInt 初始化为 0,它是 int 类型的默认值。该语句的效果等同于:int myInt = 0;


  3.不能重载 new 运算符。


  4.如果 new 运算符分配内存失败,则它将引发 OutOfMemoryException 异常。


  new 修饰符


  使用 new 修饰符显式隐藏从基类继承的成员。若要隐藏继承的成员,请使用相同名称在派生类中声明该成员,并用 new 修饰符修饰它。


  请看下面的类:


  1 public class MyClass
  2
  3 {
  4
  5 public int x;
  6
  7 public void Invoke() {}
  8
  9 }
  10


  在派生类中用 Invoke 名称声明成员会隐藏基类中的 Invoke 方法,即:


  1 public class MyDerivedC : MyClass
  2
  3 {
  4
  5 new public void Invoke() {}
  6
  7 }
  8


  但是,因为字段 x 不是通过类似名隐藏的,所以不会影响该字段。


  通过继承隐藏名称采用下列形式之一:


  1.引入类或结构中的常数、指定、属性或类型隐藏具有相同名称的所有基类成员。


  2.引入类或结构中的方法隐藏基类中具有相同名称的属性、字段和类型。同时也隐藏具有相同签名的所有基类方法。


  3.引入类或结构中的索引器将隐藏具有相同名称的所有基类索引器。


  4.在同一成员上同时使用 new 和 override 是错误的。


  注意:在不隐藏继承成员的声明中使用 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







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.