在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 } }
Ian Nelson.. 57
您不需要显式调用基础构造函数,它将被隐式调用.
稍微扩展您的示例并创建一个控制台应用程序,您可以自己验证此行为:
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."); } } }
Rob Cooper.. 29
只要它是无参数的,它是隐含的.这是因为您需要实现获取值的构造函数,请参阅下面的代码以获取示例:
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! } }
Keith.. 8
这是基本无参数构造函数的含义,但是当前类中的默认值需要它:
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" } }
John Downey.. 6
这是隐含的.
您不需要显式调用基础构造函数,它将被隐式调用.
稍微扩展您的示例并创建一个控制台应用程序,您可以自己验证此行为:
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" } }
这是隐含的.