[英]base() and this() constructors best practices
我應該在什么條件下按照構造函數的括號(甚至在代碼的其他位置:base()
進行:base()
和:this()
構造函數調用。 這些呼叫什么時候是好的做法,什么時候是強制性的?
: base(...)
如果省略對基本構造函數的調用,它將自動調用默認的基本構造函數。
如果沒有默認構造函數,則必須顯式調用基本構造函數。
即使存在默認構造函數,您仍可能希望調用與默認構造函數不同的構造函數。 在這種情況下,您可能仍希望使用base(foo, bar)
來調用與基本構造函數不同的構造函數。
當您想調用基類的默認構造函數時,我不認為忽略base()
並不是一個壞習慣,盡管如果您想明確一點,將其包括在內也沒有什么害處。 這是一個品味問題。
: this(...)
使用此語法,您可以在同一個類中以彼此不同的方式調用一個構造函數。 從來沒有強制執行此操作,但有時可能有用。
何時有用的一個示例是在構造函數中重用通用代碼。 例如,在C#3.5中或之前,您可能想在構造函數上模擬可選參數:
Foo(int x, int y)
{
this.x = x;
this.y = y;
}
Foo(int x) : this(x, 10) {} // y defaults to 10
在C#4.0中,現在可以使用可選參數,從而減少了對這種方法的需求。
在構造函數中重用代碼的另一種方法是將其分解為一個靜態函數,該函數從每個希望使用它的構造函數中調用。
首先,當它們是強制性的時。
當Derived
類是從Base
類Derived
,並且Base
沒有默認(無參數)構造函數時, Derived
必須使用參數顯式調用base()
。
public class Base {
public Base(int i) { }
}
public class Derived : Base {
// public Derived() { } wouldn't work - what should be given for i?
public Derived() : base(7) { }
public Derived(int i) : base(i) { }
}
什么時候是好習慣? 每當您要調用其他構造函數時。
假設在我之前的示例中,將內容添加到“派生”中的構造函數中。
public class Derived : Base {
// public Derived() { } wouldn't work - what should be given for i?
public Derived() : base(7) {
Console.WriteLine("The value is " + 7);
}
public Derived(int i) : base(i) {
Console.WriteLine("The value is " + i);
}
}
您在這里注意到重復嗎? 調用this()構造函數更簡單。
public class Derived : Base {
// public Derived() { } wouldn't work - what should be given for i?
public Derived() : this(7) { }
public Derived(int i) : base(i) {
Console.WriteLine("The value is " + i);
}
}
存在繼承且父類已提供您要實現的功能時,請使用base
。
使用this
當你想引用當前實體(或個體經營),用它在構造函數中的頁眉/簽名時,你不想重復了在另一個構造已定義的功能。
基本上, 在構造函數的標頭中使用base和this是為了使您的代碼保持DRY ,使其更易於維護和減少冗長
這是一個絕對沒有意義的示例,但我認為它說明了顯示如何使用兩者的想法。
class Person
{
public Person(string name)
{
Debug.WriteLine("My name is " + name);
}
}
class Employee : Person
{
public Employee(string name, string job)
: base(name)
{
Debug.WriteLine("I " + job + " for money.");
}
public Employee() : this("Jeff", "write code")
{
Debug.WriteLine("I like cake.");
}
}
用法:
var foo = new Person("ANaimi");
// output:
// My name is ANaimi
var bar = new Employee("ANaimi", "cook food");
// output:
// My name is ANaimi
// I cook food for money.
var baz = new Employee();
// output:
// My name is Jeff
// I write code for money.
// I like cake.
查找“ C#中的構造函數鏈接”。 基本上,它看起來像這樣:
MyClass():base() //default constructor calling superclass constructor
{
}
MyClass(int arg):this() //non-basic constructor calling base constructor
{
//extra initialization
}
它有助於消除構造函數中的代碼重復-將它們分為基本部分和特定部分。
當您希望自動調用基類的構造函數作為構造函數的第一條指令時,可以使用:base()。 :this()類似,但是它在同一類上調用另一個構造函數。
在base :()和this()中:您可以將常量值或基於構造函數參數的表達式作為參數傳遞。
當基類沒有默認構造函數(不帶任何參數的默認構造函數)時,必須調用基本構造函數。 我不知道:this()是強制性的情況。
public class ABaseClass
{
public ABaseClass(string s) {}
}
public class Foo : AChildClass
{
public AChildClass(string s) : base(s) {} //base mandatory
public AChildClass() : base("default value") {} //base mandatory
public AChildClass(string s,int i) : base(s+i) {} //base mandatory
}
public class AnotherBaseClass
{
public ABaseClass(string s) {}
public ABaseClass():this("default value") {} //call constructor above
}
public class Foo : AnotherChildClass
{
public AnotherChildClass(string s) : base(s) {} //base optional
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.