三種用法如下:
在 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"