方法逐步进行

huangapple go评论69阅读模式
英文:

Methods step by step

问题

类 B {
    B() {
        输出("构造函数 b");
    } 

    {
        输出("块 b");
    }
}

类 A 扩展自 B {
    A() {
        超级();
        输出("构造函数 a");
    }

    {
        输出("块 a");
    }

    公共静态无返回主函数(String[] 参数) {
        A a =A();
    }
}
输出:
块 b
构造函数 b
块 a
构造函数 a
英文:

I have kind of code. And I want to know why "block b" running first, but not "constructorb". The same thing at Class A. Thanks.

class B {
    B() {
        System.out.println("constructorb");
    } 

    {
        System.out.println("block b");
    }
}

class A extends B {
    A() {
        super();
        System.out.println("constructor a");
    }

    {
        System.out.println("block a");
    }

    public static void main(String[] args) {
        A a = new A();
    }
}
output:
block b
constructorb
block a
constructor a

答案1

得分: 1

Instance initializers(即{}块)在构造函数中被有效地(如果不是字面上的)合并。与此类似,字段初始化也发生在相同的情况下。实例初始化程序和字段初始化都插入在super(...)构造函数调用(无论是隐式还是显式)之后,但在构造函数其余的代码之前。因此,当您有以下代码时:

public class Foo {

  private int bar = 5;

  {
    System.out.println("Instance initializer");
  }

  public Foo() {
    System.out.println("Constructor");
  }
}

编译器会将其编译,就好像编写了以下代码一样:

public class Foo {

  private int bar;

  public Foo() {
    super();
    this.bar = 5;
    System.out.println("Instance initializer");
    System.out.println("Constructor");
  }
}

然而,字段的初始化顺序和实例初始化程序的执行顺序是由它们在源代码中的顺序决定的。但请注意,构造函数的位置是无关紧要的。

这在《Java语言规范》第8.8.7.1节中有所描述。

英文:

Instance initializers (i.e. the {} blocks) are effecitvely, if not literally, folded into the constructor. The same thing happens with field initialization. Both instance initializers and field initializations are inserted just after the super(...) constructor call (whether implicit or explicit) but before the rest of the constructor code. So when you have:

public class Foo {

  private int bar = 5;

  {
    System.out.println("Instance initializer");
  }

  public Foo() {
    System.out.println("Constructor");
  }
}

It's compiled as if the following was written:

public class Foo {

  private int bar;

  public Foo() {
    super();
    this.bar = 5;
    System.out.println("Instance initializer");
    System.out.println("Constructor");
  }
}

The order that fields are initialized and instance initializers are executed is, however, determined by their order in the source code. But note that the placement of the constructor is irrelevant.

This is described in §8.8.7.1 of the Java Language Specification.

答案2

得分: 0

因为block a位于非静态代码块中。非静态代码块在对象创建时执行,位于构造函数之前。

英文:

It is because block a is in a non-static block. A non-static block executes when the object is created, before the constructor

答案3

得分: 0

这是一个普通的对象实例初始化过程。

在实例初始化的过程中有两个原则:

  1. 静态方法 会在 构造方法 之前运行。
  2. 父类 会在 子类 之前运行。

现在,你有一个名为 B父类,以及一个名为 A 的扩展自 B 的 子类
所以,类 B 应该在类 A 之前运行。

接下来,在类 B 中,你定义了一个 静态方法,如下:

    {
        System.out.println("block b");
    }

所以,字符串 block b 会首先被打印出来。然后构造方法会运行:

    B() {
        System.out.println("constructorb");
    } 

所以,字符串 constructorb 会第二个被打印出来。

现在,类 B 运行完成。然后类 A 会按照顺序先运行 静态方法,然后再运行 构造方法

英文:

It is a normal Object instance initialization process.

There are two principles in the process of instance initialization:

  1. the static method would run before constructor method.
  2. the parent class would run before child class.

Now, you have parent class named B, and child class named A extends B.
So, the class B should run before class A.

Next. In class B, you define a static method which is

    {
        System.out.println("block b");
    }

So, the string block b is printed first. And then constructor method runs:

    B() {
        System.out.println("constructorb");
    } 

So, the string constructorb is printed second.<br>

Now, class B runs finish. And then class A runs as the order static mehod first and constructor method second.

huangapple
  • 本文由 发表于 2020年7月26日 20:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定