加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 智能机器人、大数据、CDN、图像分析、语音技术!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

C# new 用法示例

发布时间:2023-06-17 11:00:35 所属栏目:语言 来源:未知
导读:   int a = new int ();这句相当于int a = 0;*a是空



   int[] a = new int [20];这句的本质是指针指向一个int20的空间(在堆里)



   这个与class类似的。

  int a = new  int ();这句相当于int a = 0;*a是空
 
      int[] a = new int [20];这句的本质是指针指向一个int20的空间(在堆里)
 
      这个与class类似的。
 
      1)new 运算符:用于创建对象和调用构造函数。这种大家都比较熟悉,没什么好说的了。
 
      2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。
 
      3)new 约束:用于在泛型声明中约束可能用作类型参数的参数的类型。

      关于第二种用法看下例:
 
  using System;
 
  namespace ConsoleApplication1
 
  {
 
      public class BaseA
 
      {
 
          public int x = 1;
 
          public void Invoke()
 
          {
 
              Console.WriteLine(x.ToString());
 
          }
 
          public int Trueva lue
 
          {
 
              get { return x; }
 
              set { x = value; }
 
          }
 
      }
 
      public class DerivedB : BaseA
 
      {
 
          new public int x = 2;
 
          new public void Invoke()
 
          {
 
              Console.WriteLine(x.ToString());
 
          }
 
          new public int Trueva lue
 
          {
 
              get { return x; }
 
              set { x = value; }
 
          }
 
      }
 
      class Test
 
      {
 
          static void Main(string[] args)
 
          {
 
              DerivedB b = new DerivedB();
 
              b.Invoke();//调用DerivedB的Invoke方法,输出:2
 
              Console.WriteLine(b.x.ToString());//输出DerivedB的成员x值:2
 
              BaseA a = b;
 
              a.Invoke();//调用BaseA的Invoke方法,输出:1
 
              a.Trueva lue = 3;//调用BaseA的属性Trueva lue,修改BaseA的成员x的值
 
              Console.WriteLine(a.x.ToString());//输出BaseA的成员x的值:3
 
              Console.WriteLine(b.Trueva lue.ToString());//输出DerivedB的成员x的值,仍然是:1
 
  //可见,要想访问被隐藏的基类的成员变量、属性或方法,办法就是将子类造型为父类,然
 
  //后通过基类访问被隐藏的成员变量、属性或方法。
 
          }
 
       }
 
  }
 
      new约束指定泛型类声明中的任何类型参数都必须具有公共的无参数构造函数.请看下面实例:
 
  using System;
 
  using System.Collections.Generic;
 
  namespace ConsoleApplication2
 
  {
 
      public class Employee
 
      {
 
          private string name;
 
          private int id;
 
          public Employee()
 
          {
 
              name = "Temp";
 
              id = 0;
 
          }
 
          public Employee(string s, int i)
 
          {
 
              name = s;
 
              id = i;
 
          }
 
          public string Name
 
          {
 
              get { return name; }
 
              set { name = value; }
 
          }
 
          public int ID
 
          {
 
              get { return id; }
 
              set { id = value; }
 
          }
 
      }
 
      class ItemFactory<T> where T : new()
 
      {
 
          public T GetNewItem()
 
          {
 
              return new T();
 
          }
 
      }

      public class Test
 
      {
 
          public static void Main()
 
          {
 
              ItemFactory<Employee> EmployeeFactory = new ItemFactory<Employee>();
 
              ////此处编译器会检查Employee是否具有公有的无参构造函数。
 
              //若没有则会有The Employee must have a public parameterless constructor 错误。
 
              Console.WriteLine("{0}'ID is {1}.", EmployeeFactory.GetNewItem().Name, EmployeeFactory.GetNewItem().ID);
 
          }
 
      }
 
  }
 

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章