Discuss / Java / 分享一下我昨天的面试题

分享一下我昨天的面试题

Topic source

HelloA  class

public class HelloA {

public HelloA() {
System.out.println("helloA");
}

{
System.out.println("AAA");
}

static {
System.out.println("AAA static");
}

}

HelloB  class

public class HelloB extends HelloA {

public HelloB() {
System.out.println("Hellob");
}

{
System.out.println("BBB");
}

static {
System.out.println("BBB static");
}

public static void main(String[] args) {
new HelloB();
}
}

问:运行结果是什么???

Joker.fu_95

#2 Created at ... [Delete] [Delete and Lock User]

先静后动,先父后子

勿忘我

#3 Created at ... [Delete] [Delete and Lock User]

“静态代码块-》构造代码块-》构造器”,然后有继承关系的“先父再子”。顺序不变

YouthInXian

#4 Created at ... [Delete] [Delete and Lock User]

AAA static

BBB static

AAA

helloA

BBB

Hellob

よろしく

#5 Created at ... [Delete] [Delete and Lock User]

只有{}的这种叫构造代码块。它会在每次创建对象时被自动执行,不能像普通方法一样被特别调用,并且执行顺序排在构造方法前。

保持热爱

#6 Created at ... [Delete] [Delete and Lock User]

父类静态代码块->子类静态代码块->父类构造代码块->父类构造方法->子类构造代码块->子类构造方法


  • 1

Reply