C# 中的基本构造函数 - 首先调用哪个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/140490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Base constructor in C# - Which gets called first?
提问by Mike Comstock
Which gets called first - the base constructor or "other stuff here"?
哪个首先被调用 - 基本构造函数或“这里的其他东西”?
public class MyExceptionClass : Exception
{
public MyExceptionClass(string message, string extrainfo) : base(message)
{
//other stuff here
}
}
采纳答案by craigb
The base constructor will be called first.
基础构造函数将首先被调用。
try it:
尝试一下:
public class MyBase
{
public MyBase()
{
Console.WriteLine("MyBase");
}
}
public class MyDerived : MyBase
{
public MyDerived():base()
{
Console.WriteLine("MyDerived");
}
}
回答by Mastermind
I'd say base
我会说基地
EDIT see:
编辑见:
there it says:
那里说:
using System;
class Base
{
public Base()
{
Console.WriteLine("BASE 1");
}
public Base(int x)
{
Console.WriteLine("BASE 2");
}
}
class Derived : Base
{
public Derived():base(10)
{
Console.WriteLine("DERIVED CLASS");
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
}
}
This program outputs
BASE2
DERIVED CLASS
这个程序输出
基地2
派生类
回答by mmcdole
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=777
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=777
Base Constructor gets called first.
基础构造函数首先被调用。
回答by CheGueVerra
The Exception Constructor will be called, then your Child class constructor will be called.
将调用异常构造函数,然后将调用您的 Child 类构造函数。
Simple OO principle
简单的面向对象原则
Have a look here http://www.dotnet-news.com/lien.aspx?ID=35151
回答by Paolo Tedesco
Actually, the derived class constructor is executed first, but the C# compiler inserts a call to the base class constructor as first statement of the derived constructor.
实际上,首先执行派生类构造函数,但 C# 编译器插入对基类构造函数的调用作为派生构造函数的第一条语句。
So: the derived is executed first, but it "looks like" the base was executed first.
所以:派生的先执行,但它“看起来”是先执行的。
回答by Sam Meldrum
Base class constructors get called before derived class constructors, but derived class initializers get called before base class initializers. E.g. in the following code:
基类构造函数在派生类构造函数之前被调用,但派生类构造函数在基类构造函数之前被调用。例如在以下代码中:
public class BaseClass {
private string sentenceOne = null; // A
public BaseClass() {
sentenceOne = "The quick brown fox"; // B
}
}
public class SubClass : BaseClass {
private string sentenceTwo = null; // C
public SubClass() {
sentenceTwo = "jumps over the lazy dog"; // D
}
}
Order of execution is: C, A, B, D.
执行顺序为:C、A、B、D。
Check out these 2 msdn articles:
查看这 2 篇 msdn 文章:
回答by kafuchau
The base constructor will be called first, otherwise, in cases where your "other stuff" must make use of member variables initialized by your base constructor, you'll get compile time errors because your class members will not have been initialized yet.
基构造函数将首先被调用,否则,在您的“其他东西”必须使用由基构造函数初始化的成员变量的情况下,您将收到编译时错误,因为您的类成员尚未初始化。
回答by Chris Cudmore
base(?) is called before any work is done in the child constructor.
base(?) 在子构造函数中完成任何工作之前被调用。
This is true, even if you leave off the :base() (in which case, the 0-parameter base constructor is called.)
这是真的,即使您不使用 :base() (在这种情况下,将调用 0 参数基构造函数。)
It works similar to java,
它的工作原理类似于java,
public Child()
{
super(); // this line is always the first line in a child constructor even if you don't put it there! ***
}
*** Exception: I could put in super(1,2,3) instead. But if I don't put a call to super in explicitly, super() is called.
*** 例外:我可以输入 super(1,2,3) 代替。但是如果我没有明确地调用 super ,就会调用 super() 。
回答by Joel B Fant
As others have said, the base constructor gets called first. However, constructors are not really the first thing that happens.
正如其他人所说,基础构造函数首先被调用。然而,构造函数并不是真正首先发生的事情。
Let's say you have classes like this:
假设您有这样的课程:
class A {}
class B : A {}
class C : B {}
First, field initializers will be called in order of most-derived to least-derived classes. So first field initializers of C
, then B
, then A
.
首先,将按照派生最多到最少派生类的顺序调用字段初始值设定项。所以第一个字段初始值设定项C
,然后B
,然后A
。
The constructors will then be called in the opposite order: First A
's constructor, then B
, then C
.
然后以相反的顺序调用构造函数:FirstA
的构造函数,然后是B
,然后是C
。
回答by Joel B Fant
Constructor calls are called (fired) from the bottom up, and executed from the top down. Thus, if you had Class C which inherits from Class B which inherits from Class A, when you create an instance of class C the constructor for C is called, which in turn calls the instructor for B, which again in turn calls the constructor for A. Now the constructor for A is executed, then the constructor for B is executed, then the constructor for C is executed.
构造函数调用自下而上调用(触发),并自上而下执行。因此,如果您有从继承自 A 类的 B 类继承的 C 类,当您创建类 C 的实例时,C 的构造函数被调用,它依次调用 B 的指导者,后者又再次调用构造函数A. 现在执行 A 的构造函数,然后执行 B 的构造函数,然后执行 C 的构造函数。