17.10.3 Constructor execution

原创 2006年05月21日 02:51:00
Variable initializers are transformed into assignment statements, and these
assignment statements are executed
before the invocation of the base class instance constructor. This ordering
ensures that all instance fields are
initialized by their variable initializers before any statements that have
access to that instance are executed.
[Example: For example:
C# LANGUAGE SPECIFICATION
260
using System;
class A
{
public A() {
PrintFields();
}
public virtual void PrintFields() {}
}
class B: A
{
int x = 1;
int y;
public B() {
y = -1;
}
public override void PrintFields() {
Console.WriteLine("x = {0}, y = {1}", x, y);
}
}
When new B() is used to create an instance of B, the following output is
produced:
x = 1, y = 0
The value of x is 1 because the variable initializer is executed before the
base class instance constructor is
invoked. However, the value of y is 0 (the default value of an int) because
the assignment to y is not executed
until after the base class constructor returns.
It is useful to think of instance variable initializers and constructor
initializers as statements that are automatically
inserted before the constructor-body. The example
using System;
using System.Collections;
class A
{
int x = 1, y = -1, count;
public A() {
count = 0;
}
public A(int n) {
count = n;
}
}
class B: A
{
double sqrt2 = Math.Sqrt(2.0);
ArrayList items = new ArrayList(100);
int max;
public B(): this(100) {
items.Add("default");
}
public B(int n): base(n ? 1) {
max = n;
}
}
contains several variable initializers; it also contains constructor
initializers of both forms (base and this). The
example corresponds to the code shown below, where each comment indicates
an automatically inserted
statement (the syntax used for the automatically inserted constructor
invocations isn?t valid, but merely serves to
illustrate the mechanism).
using System.Collections;
Chapter 17 Classes
261
class A
{
int x, y, count;
public A() {
x = 1; // Variable initializer
y = -1; // Variable initializer
object(); // Invoke object() constructor
count = 0;
}
public A(int n) {
x = 1; // Variable initializer
y = -1; // Variable initializer
object(); // Invoke object() constructor
count = n;
}
}
class B: A
{
double sqrt2;
ArrayList items;
int max;
public B(): this(100) {
B(100); // Invoke B(int) constructor
items.Add("default");
}
public B(int n): base(n ? 1) {
sqrt2 = Math.Sqrt(2.0); // Variable initializer
items = new ArrayList(100); // Variable initializer
A(n ? 1); // Invoke A(int) constructor
max = n;
}
}
end example]

相关文章推荐

java反射之Constructor

  • 2013年01月27日 22:06
  • 27KB
  • 下载

整理 execution表达式 切入点语法定义

execution表达式 切入点语法定义

实例 c# constructor

  • 2013年10月13日 23:29
  • 2KB
  • 下载

execution切入点表达式

Spring AOP 用户可能会经常使用 execution切入点指示符。 类型匹配模式: 1:*:匹配任何数量字符;比如模式 (*,String) 匹配了一个接受两个参数的方法,第一个可以是任意类型...

Constructor.rar

  • 2010年06月01日 16:16
  • 351KB
  • 下载

PLAN_HASH_VALUE - How equal (and stable?) are your execution plans - part 1

http://oracle-randolf.blogspot.tw/2009/07/planhashvalue-how-equal-and-stable-are.html     Oracle ...
  • loryliu
  • loryliu
  • 2017年02月14日 15:19
  • 209

BOA Constructor 教程

  • 2009年02月02日 17:33
  • 2.02MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:17.10.3 Constructor execution
举报原因:
原因补充:

(最多只允许输入30个字)