public class A {
static{
System.out.println("a static");
}
public A(){
System.out.println("a construct");
}
}public class B extends A {
static {
System.out.println("b static");
}
public B(){
System.out.println("b construct");
}
}public class Main {
public static void main(String[] args) {
A ab=new B();
ab=new B(); }}
输出后的结果为
a static
b static
a construct
b construct
a construct
b construct
请问各位大虾为什么会是这个结果哪
static{
System.out.println("a static");
}
public A(){
System.out.println("a construct");
}
}public class B extends A {
static {
System.out.println("b static");
}
public B(){
System.out.println("b construct");
}
}public class Main {
public static void main(String[] args) {
A ab=new B();
ab=new B(); }}
输出后的结果为
a static
b static
a construct
b construct
a construct
b construct
请问各位大虾为什么会是这个结果哪
ab= new B();
输出是
a static
b static
a construct
a construct
b construct
1。static
2。子类,构造函数。
? static是类加载时就执行的,不管有没有实例化这个类,把上面的改为A ab=new A();
ab= new B();
输出是
a static
b static
a construct
a construct
b construct顺序应该是:
a static
a construct
b static
a construct
b construct
static区块比构造方法更先执行
在实例化子类的对象的时候 会自动调用父类的构造方法