.NET(c#)new关键字的三种用法

三种用法如下:
在 C# 中,new 关键字可用作运算符、修饰符或约束。
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 TrueValue
        {
            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 TrueValue
        {
            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.TrueValue = 3;//调用BaseA的属性TrueValue,修改BaseA的成员x的值
            Console.WriteLine(a.x.ToString());//输出BaseA的成员x的值:3
            Console.WriteLine(b.TrueValue.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);
        }
    }
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
js中的new用法
按照javascript语言精粹中所说,如果在一个函数前面带上new来调用该函数,那么将创建一个隐藏连接到该函数的prototype成员的新对象,同时this将被绑定到那个新对象上。这个话很抽象,我想用实例来让自己加深理解。
1.如果就一个函数,没有返回值,没有prototype成员,然后使用new,会是什么结果呢?如果一个函数没有返回值,那么如果不使用new来创建变量,那么该变量的值为undefined.如果用了new,那么就是Object.说明一个函数的默认的Prototype是Object.

function Test1(str) {    this.a = str;}var myTest = new Test1("test1");alert(myTest); //[object Object]function Test1WithoutNew(str) {    this.a = str;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //undefined;
2.如果函数有返回值,但是返回值是基本类型。那么new出来的myTest还是object.因为基本类型的prototype还是Object. 而如果不使用new,那么返回值就是string的值。

function Test1(str) {    this.a = str;    return this.a;}var myTest = new Test1("test1");alert(myTest); //Objectfunction Test1WithoutNew(str) {    this.a = str;    return this.a;}var myTestWithoutNew = Test1WithoutNew("test1");alert(myTestWithoutNew); //"test1"
3。如果函数的返回值为new出来的对象,那么myTest的值根据new出来的对象的prototype而定。
function Test1(str) {    this.a = str;    return new String(this.a);}var myTest = new Test1("test1");alert(myTest); //String "test1"
4。接下来我们开始讨论new中的this。如果我们给Test1的prototype中加入一个方法叫get_string(),那么get_string()中的this指的就是这个新对象。能够得到在new时候赋予该对象的属性值。

var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2.get_string()); //“test2”var Test2 = function(str) {    this.a = str;}Test2.prototype.get_string = function () {    return this.a;};var myTest2 = Test2("test2");alert(myTest2)//undefined
5。如果我们修改了函数的prototype,又会发生什么样的情况呢? 那么就会发生类似继承的功能,其实就是js的伪类实现。

function Test1(str) {    this.b = str;}Test1.prototype.Get_Test1String = function () {    return this.b;};var Test2 = function(str) {    this.a = str;}Test2.prototype = new Test1("test1");Test2.prototype.get_string = function () {    return this.a;};var myTest2 = new Test2("test2");alert(myTest2); //Objectalert(myTest2.get_string()); //"test2"alert(myTest2.Get_Test1String()); //"test1"

原文地址:https://www.cnblogs.com/xiebaochun/p/3411895.html