现在的位置: 首页 > 综合 > 正文

一起温习C#基础

2013年05月25日 ⁄ 综合 ⁄ 共 2804字 ⁄ 字号 评论关闭

在 C# 中,new 关键字可用作运算符、修饰符或约束。

1)new 运算符:用于创建对象和调用构造函数。这种大家都比较熟悉,没什么好说的了。

2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。

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

关于第二种用法看下例:

Code:
  1. using System;   
  2.   
  3. namespace ConsoleApplication1   
  4.   
  5. {   
  6.   
  7.     public class BaseA   
  8.   
  9.     {   
  10.   
  11.         public int x = 1;   
  12.   
  13.         public void Invoke()   
  14.   
  15.         {   
  16.   
  17.             Console.WriteLine(x.ToString());   
  18.   
  19.         }   
  20.   
  21.         public int TrueValue   
  22.   
  23.         {   
  24.   
  25.             get { return x; }   
  26.   
  27.             set { x = value; }   
  28.   
  29.         }   
  30.   
  31.     }   
  32.   
  33.     public class DerivedB : BaseA   
  34.   
  35.     {   
  36.   
  37.         new public int x = 2;   
  38.   
  39.         new public void Invoke()   
  40.   
  41.         {   
  42.   
  43.             Console.WriteLine(x.ToString());   
  44.   
  45.         }   
  46.   
  47.         new public int TrueValue   
  48.   
  49.         {   
  50.   
  51.             get { return x; }   
  52.   
  53.             set { x = value; }   
  54.   
  55.         }   
  56.   
  57.     }   
  58.   
  59.     class Test   
  60.   
  61.     {   
  62.   
  63.         static void Main(string[] args)   
  64.   
  65.         {   
  66.   
  67.             DerivedB b = new DerivedB();   
  68.   
  69.             b.Invoke();//调用DerivedB的Invoke方法,输出:2   
  70.   
  71.             Console.WriteLine(b.x.ToString());//输出DerivedB的成员x值:2   
  72.   
  73.             BaseA a = b;   
  74.   
  75.             a.Invoke();//调用BaseA的Invoke方法,输出:1   
  76.   
  77.             a.TrueValue = 3;//调用BaseA的属性TrueValue,修改BaseA的成员x的值   
  78.   
  79.             Console.WriteLine(a.x.ToString());//输出BaseA的成员x的值:3   
  80.   
  81.             Console.WriteLine(b.TrueValue.ToString());//输出DerivedB的成员x的值,仍然是:1   
  82.   
  83. //可见,要想访问被隐藏的基类的成员变量、属性或方法,办法就是将子类造型为父类,然   
  84.   
  85. //后通过基类访问被隐藏的成员变量、属性或方法。   
  86.   
  87.         }   
  88.   
  89.      }   
  90.   
  91. }   
  92.   
  93. new约束指定泛型类声明中的任何类型参数都必须具有公共的无参数构造函数.请看下例:   
  94.   
  95. using System;   
  96.   
  97. using System.Collections.Generic;   
  98.   
  99. namespace ConsoleApplication2   
  100.   
  101. {   
  102.   
  103.     public class Employee   
  104.   
  105.     {   
  106.   
  107.         private string name;   
  108.   
  109.         private int id;   
  110.   
  111.         public Employee()   
  112.   
  113.         {   
  114.   
  115.             name = "Temp";   
  116.   
  117.             id = 0;   
  118.   
  119.         }   
  120.   
  121.         public Employee(string s, int i)   
  122.   
  123.         {   
  124.   
  125.             name = s;   
  126.   
  127.             id = i;   
  128.   
  129.         }   
  130.   
  131.         public string Name   
  132.   
  133.         {   
  134.   
  135.             get { return name; }   
  136.   
  137.             set { name = value; }   
  138.   
  139.         }   
  140.   
  141.         public int ID   
  142.   
  143.         {   
  144.   
  145.             get { return id; }   
  146.   
  147.             set { id = value; }   
  148.   
  149.         }   
  150.   
  151.     }   
  152.   
  153.     class ItemFactory<T> where T : new()   
  154.   
  155.     {   
  156.   
  157.         public T GetNewItem()   
  158.   
  159.         {   
  160.   
  161.             return new T();   
  162.   
  163.         }   
  164.   
  165.     }   
  166.   
  167.     public class Test   
  168.   
  169.     {   
  170.   
  171.         public static void Main()   
  172.   
  173.         {   
  174.   
  175.             ItemFactory<Employee> EmployeeFactory = new ItemFactory<Employee>();   
  176.   
  177.             ////此处编译器会检查Employee是否具有公有的无参构造函数。   
  178.   
  179.             //若没有则会有The Employee must have a public parameterless constructor 错误。   
  180.   
  181.             Console.WriteLine("{0}'ID is {1}.", EmployeeFactory.GetNewItem().Name, EmployeeFactory.GetNewItem().ID);   
  182.   
  183.         }   
  184.   
  185.     }   
  186.   
  187. }   
  188.   

 

抱歉!评论已关闭.