首页 > 编程 > C# > 正文

C# new 用法实例

2020-09-12 13:00:52
字体:
来源:转载
供稿:网友

    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);
        }
    }
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表