![](/img/trans.png)
[英]C# in VS2005: in an inherited class, do you need to explicitly call the super-constructor?
[英]In C#, do you need to call the base constructor?
在C#中,如果我有一個帶有默認構造函數的繼承類,我是否必須顯式調用基類的構造函數或者它是否會被隱式調用?
class BaseClass
{
public BaseClass()
{
// ... some code
}
}
class MyClass : BaseClass
{
public MyClass() // Do I need to put ": base()" here or is it implied?
{
// ... some code
}
}
您不需要顯式調用基礎構造函數,它將被隱式調用。
稍微擴展您的示例並創建一個控制台應用程序,您可以自己驗證此行為:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyClass foo = new MyClass();
Console.ReadLine();
}
}
class BaseClass
{
public BaseClass()
{
Console.WriteLine("BaseClass constructor called.");
}
}
class MyClass : BaseClass
{
public MyClass()
{
Console.WriteLine("MyClass constructor called.");
}
}
}
只要它是無參數的,它是隱含的。 這是因為您需要實現獲取值的構造函數 ,請參閱下面的代碼以獲取示例:
public class SuperClassEmptyCtor
{
public SuperClassEmptyCtor()
{
// Default Ctor
}
}
public class SubClassA : SuperClassEmptyCtor
{
// No Ctor's this is fine since we have
// a default (empty ctor in the base)
}
public class SuperClassCtor
{
public SuperClassCtor(string value)
{
// Default Ctor
}
}
public class SubClassB : SuperClassCtor
{
// This fails because we need to satisfy
// the ctor for the base class.
}
public class SubClassC : SuperClassCtor
{
public SubClassC(string value) : base(value)
{
// make it easy and pipe the params
// straight to the base!
}
}
這是基本無參數構造函數的含義,但是當前類中的默認值需要它:
public class BaseClass {
protected string X;
public BaseClass() {
this.X = "Foo";
}
}
public class MyClass : BaseClass
{
public MyClass()
// no ref to base needed
{
// initialise stuff
this.X = "bar";
}
public MyClass(int param1, string param2)
:this() // This is needed to hit the parameterless ..ctor
{
// this.X will be "bar"
}
public MyClass(string param1, int param2)
// :base() // can be implied
{
// this.X will be "foo"
}
}
這是隱含的。
派生類是基於基類構建的。 如果你考慮一下,基礎對象必須在內存中實例化,然后才能將派生類附加到它上面。 因此,將在創建派生對象的方式上創建基礎對象。 所以不,你不要調用構造函數。
AFAIK,如果需要向其傳遞任何值,則只需要調用基礎構造函數。
您不需要顯式調用基本構造函數,它將被隱式調用,但有時您需要將參數傳遞給構造函數,在這種情況下,您可以執行以下操作:
using System;
namespace StackOverflow.Examples
{
class Program
{
static void Main(string[] args)
{
NewClass foo = new NewClass("parameter1","parameter2");
Console.WriteLine(foo.GetUpperParameter());
Console.ReadKey();
}
}
interface IClass
{
string GetUpperParameter();
}
class BaseClass : IClass
{
private string parameter;
public BaseClass (string someParameter)
{
this.parameter = someParameter;
}
public string GetUpperParameter()
{
return this.parameter.ToUpper();
}
}
class NewClass : IClass
{
private BaseClass internalClass;
private string newParameter;
public NewClass (string someParameter, string newParameter)
{
this.internalClass = new BaseClass(someParameter);
this.newParameter = newParameter;
}
public string GetUpperParameter()
{
return this.internalClass.GetUpperParameter() + this.newParameter.ToUpper();
}
}
}
注意:如果有人知道更好的解決方案,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.