public Butler(IWeapon weapon): this(weapon, new Communicate())
{}
這是什麼(武器,新Communicate())是什麼意思?c#語法幫助構造函數之後會發生什麼?
public Butler(IWeapon weapon): this(weapon, new Communicate())
{}
這是什麼(武器,新Communicate())是什麼意思?c#語法幫助構造函數之後會發生什麼?
它呼籲同類型的另一個構造,傳遞weapon
傳遞給這個構造,和一個新的Communicate
對象。我認爲也有一樣的東西:
// might actually be private
public Butler(IWeapon weapon, Communicate communicate) {...}
這種做法是讓你有多個構造函數是有用的,但只有把代碼在最複雜的一個。所有其他構造函數只是將參數(或必要時的默認值)傳遞給所選的構造函數。
鏈式構造函數將被援引具有: this(...)
這是繼承嗎?它看起來像「繼承」一個構造函數。 – 2010-12-14 03:00:06
@Camilo no;它只調用* current *類型的方法;注意到繼承相關 – 2010-12-14 06:05:43
它要求在同一個班,但具有不同參數的構造函數的構造體,例如:
public class Test
{
public Test() : this("Richard")
{
}
public Test(string name)
{
}
}
在這樣的情況下,可選參數將是最好的,對吧? – 2010-12-14 02:54:51
這意味着Butler擴展了武器對象作爲參數傳遞的當前類的實例,並將Communicate對象的新實例傳遞給類構造函數。
它調用帶簽名管家(IWeapon,ICommunicate)的另一個構造函數。
這是一個禿鷹:) – JohnIdol 2010-12-13 11:32:27