C# base() 构造函数顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2021373/
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
C# base() constructor order
提问by Xenoprimate
Possible Duplicate:
C# constructor execution order
可能的重复:
C# 构造函数执行顺序
class Foo
{
public int abc;
Foo()
{
abc = 3;
}
}
class Bar : Foo
{
Bar() : base()
{
abc = 2;
}
}
In the example above, when an object of Bar is created, what will be the value of BarObject.abc? Is the base constructor called first, or is Bar() run, /then/ the base() constructor?
在上面的例子中,当一个 Bar 对象被创建时,BarObject.abc 的值是什么?是首先调用基本构造函数,还是运行 Bar(),然后/然后/base() 构造函数?
采纳答案by Paolo
It'll be 2. Constructors run in order from base class first to inherited class last.
它将是 2。构造函数按从基类到继承类最后的顺序运行。
Note that initialisers (both static and instance variables) run in the opposite direction.
请注意,初始化程序(静态和实例变量)以相反的方向运行。
The full sequence is here: http://www.csharp411.com/c-object-initialization/
回答by kemiller2002
The variable abc will be set to be 3 and then changed to be 2 (the base constructor is called first).
变量 abc 将设置为 3,然后更改为 2(首先调用基本构造函数)。
回答by JonH
First base class constructor is called followed by the derived class constructor. The result is 2. You should explicitly state the accessibility of that class variable. Is it protected, private or public?
首先调用基类构造函数,然后调用派生类构造函数。结果是 2。您应该明确说明该类变量的可访问性。它是受保护的,私有的还是公共的?
I see you changed it to public now, so it will be 2.
我看到你现在把它改成了公开的,所以它会是 2。
This link will further help you understand constructors, how they are used, when they are called, and order of constructor call when you use inheritance:
此链接将进一步帮助您了解构造函数、它们的使用方式、调用它们的时间以及使用继承时构造函数调用的顺序:
http://www.yoda.arachsys.com/csharp/constructors.html
http://www.yoda.arachsys.com/csharp/constructors.html
Also you may want to actually try this out yourself, you will learn more by practicing and writing code then just reading it.
此外,您可能想亲自尝试一下,您将通过练习和编写代码然后阅读它来了解更多信息。
Try to declare Bar and output its value. Use some properties:
尝试声明 Bar 并输出其值。使用一些属性:
class Foo
{
public int abc;
public Foo()
{
abc = 3;
}
public int ABC
{
get { return abc; }
set { abc = value; }
}
}
class Bar : Foo
{
public Bar() : base()
{
abc = 2;
}
}
class Program
{
static void Main(string[] args)
{
Bar b = new Bar();
Console.WriteLine(b.ABC);
Console.ReadLine();
}
}
A simple printout would yield the result you are looking for. Here is the output:
一个简单的打印输出将产生您正在寻找的结果。这是输出:
Don't you just love my namespace
:-). By the way you could also use automatic properties so that the property is simply public int ABC {get;set;}.
难道你不只是爱我的namespace
:-)。顺便说一下,您还可以使用自动属性,以便该属性只是 public int ABC {get;set;}。
回答by Chris Pitman
The base constructor will be called first, but this code does not compile. Private fields are not accesable from sub-classes. At the very least a field must be protectedto be used in a sub-class.
基础构造函数将首先被调用,但此代码不会编译。子类不能访问私有字段。至少一个字段必须受到保护才能在子类中使用。
But even knowing this, the behaviour you are attempting is confusing because it is surprising. Just the fact you had to ask which order things go in implies that it will get messed up when the order is forgotten.
但即使知道这一点,您尝试的行为也令人困惑,因为它令人惊讶。事实上,您必须询问事物的顺序,这意味着当忘记顺序时它会变得一团糟。
回答by Aaron M
The base constuctor is called first, and you would have a value of 2 for abc
首先调用基础构造函数,abc 的值为 2
回答by bradjive
Assuming you make abc protected so that this compiles, it will be 2; however, base()
is called first.
假设您使 abc 受保护以便编译,它将是 2;然而,base()
首先被调用。
For stuff like this, write a simple test application and setup some breakpoints to find the answer.
对于这样的事情,编写一个简单的测试应用程序并设置一些断点来找到答案。