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]

GreenDAO 3介绍与应用

前言 当我看到需要在android工程下新建一个module-->java library的generator library的时候,便去翻阅了一下官方文档,此时发现出现了GreenDao 3,可以不...
  • gongsunjinqian
  • gongsunjinqian
  • 2016年11月15日 15:33
  • 2494

DWR初次使用遇到的问题:Servlet dwr-invoker is not available

问题描述:Servlet dwr-invoker is not available 问题详细: 严重: Allocate exception for servlet dwr-invokerjava.l...
  • xiayanhua225
  • xiayanhua225
  • 2010年07月19日 19:56
  • 3643

greendao3出现 Error:Execution failed for task ':app:greendao'

错误如上图,最近下载一个Greendao的demo运行出现上述错误, 这个是build配置错误 修改build 就可以了 1       Moudle的gradle中 apply pl...
  • qq_33210042
  • qq_33210042
  • 2018年02月01日 13:01
  • 31

greendao3出现 Error:Execution failed for task ':app:greendao'.

greendao编译不通过,出现下面错误:Error:Execution failed for task ':app:greendao'. > org.eclipse.jdt.internal.com...
  • huangxiaoguo1
  • huangxiaoguo1
  • 2017年04月21日 15:26
  • 6722

GreenDao3编译出现 Error:Execution failed for task ':greendaotest:greendao'. > org.eclipse.jdt.

原因Gradle配置有问题解决办法 1.Moudle的gradle中apply plugin: 'org.greenrobot.greendao'greendao { schemaVersi...
  • qq_37219980
  • qq_37219980
  • 2017年05月05日 11:35
  • 2051

Error:Execution failed for task ':greendaotest:greendao'. > org.eclipse.jdt.

今天在做项目的时候需要用到greendao,将它集成到androStudio中的时候,爆了如下错: Error:Execution failed for task ':greendaotest:gr...
  • twopai
  • twopai
  • 2017年03月24日 17:52
  • 1801

Android ORM 框架之 greenDAO应用基础

greenDAO是时下Android最流行的一款ORM框架,其性能高,可加密,使用简洁,做android开发,如果会使用它,工作量会大大减小。其性能与其他ORM框架之比较可以查阅其官网。 目前gre...
  • u012585964
  • u012585964
  • 2016年09月08日 02:00
  • 2605

GreenDao 3.0 使用详解之注解实体类

GreenDao 3.0 使用详解之注解实体类 ONE Goal , ONE Passion ! Modelling entities 实...
  • fengltxx
  • fengltxx
  • 2016年12月17日 16:32
  • 3364

信息17.10.3

最近准备初赛,老师讲的内容都是理解但是代码就是敲不出来23333 基本知识点都掌握啦,但对于计算机本身结构和名人这点还是不行唉。 还是要好好加油呀! 有些东西还是要用脑子去记一记。感觉浴谷老师上...
  • treeink
  • treeink
  • 2017年10月03日 19:02
  • 33

Error:Execution failed for task ':app:greendao'. Error:Execution failed for task ':app:greendao'. >

Error:Execution failed for task ':app:greendao'. > org.eclipse.jdt.internal.compiler.impl.CompilerOp...
  • monkin2011
  • monkin2011
  • 2017年08月01日 23:41
  • 519
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:17.10.3 Constructor execution
举报原因:
原因补充:

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