.net New有几种用法

3种。
1)new 运算符:用于创建对象和调用构造函数。
2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。
3)new 约束:用于在泛型声明中约束可能用作类型参数的参数的类型。
针对2,在使用一个类继承一个基类,并且写了同样的方法,此时会正常的使用,但是会提示,建议使用new关键字,显示隐藏。
对同一成员同时使用 new 和 override 是错误的做法,因为这两个修饰符的含义互斥。 new 修饰符会用同样的名称创建一个新成员并使原始成员变为隐藏的。 override 修饰符会扩展继承成员的实现。

在不隐藏继承成员的声明中使用 new 修饰符将会生成警告。

2789632-e6ab0b5b3abacd8a.png

针对3,new约束指定泛型类声明中的任何类型参数都必须具有公共的无参数构造函数。

class ClassFour<T> where T : new()
    {
        public T GetNewItem()
        {
            return new T();
        }
    }

ClassFour<Employee> EmployeeFactory = new ClassFour<Employee>();
此处编译器会检查Employee是否具有公有的无参构造函数。
//若没有则会有The Employee must have a public parameterless constructor 错误。

Code Practice:


2789632-7e9580d0e0e5fe6a.png

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TheWayToUseNew
{
    public class ClassOne
    {
        public void PrintName()
        {
            Console.WriteLine("Name");
        }
    }

    public class ClassTwo : ClassOne
    {
        new public void PrintName()
        {
            Console.WriteLine("NameTwo");
        }

    }

    public class ClassThree : ClassOne
    {
        public void PrintName()
        {
            Console.WriteLine("NameThree");
        }

    }

    class ClassFour<T> where T : new()
    {
        public T GetNewItem()
        {
            return new T();
        }
    }
    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 Program
    {

        static void Main(string[] args)
        {
            // first way for instantiation
            ClassOne classOne = new ClassOne();
            classOne.PrintName();

            // second way to hide base class method
            ClassTwo classTwo = new ClassTwo();
            classTwo.PrintName();

            // third way to not hide base class method
            ClassThree classThree = new ClassThree();
            classThree.PrintName();

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

            Console.ReadLine();

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值