public class HelloB extends HelloA{
public HelloB()
{
System.out.println("HelloB");
} {
System.out.println("I am B class");
}
static{
System.out.println("static B");
}
public static void main(String[] args){
System.out.println("HelloB的主方法");
new HelloA();
new HelloA();
new HelloB();
new HelloB();
}
}
class HelloA{
public HelloA()
{
System.out.println("HelloA");
} {
System.out.println("I am A class");
}
static{
System.out.println("static A");
}
}
运行结果:
static A
static B
HelloB的主方法
I am A class
HelloA
I am A class
HelloA
I am A class
HelloA
I am B class
HelloB
I am A class
HelloA
I am B class
HelloB
相关例子:http://www.cnblogs.com/wangzhongqiu/p/6288452.html
public class testdaimakuia {
public static void main(String[] args) {
B b = new B();
}
} class A {
{
System.out.println("A的构造块");
} A() {
System.out.print("A");
}
} class B {
{
System.out.println("B的构造块");
} B() {
System.out.print("B");
} A a = new A();
}
执行结果:
B的构造块
A的构造块
AB
静态代码块>mian方法>构造代码块==成员变量(按照出现先后顺序执行)>构造方法
public class testdaimakuia {
public static void main(String[] args) {
B b = new B();
}
} class A {
static {
System.out.println("A的静态代码块");
}
{
System.out.println("A的构造块");
} A() {
System.out.print("A");
}
} class B {
static {
System.out.println("B的静态代码块");
}
{
System.out.println("B的构造块");
} B() {
System.out.print("B");
} static A a = new A(); }
执行结果:
B的静态代码块
A的静态代码块
A的构造块
AB的构造块
B
静态成员变量==静态代码块(按照出现先后顺序执行)>mian方法>构造代码块==成员变量(按照出现先后顺序执行)>构造方法