class A extends
java.lang.Object
{
。。。
}
class B extends A{
public static void main(String args[]){
ClassA ca=new ClassA ();
}
Object
载入内存
1.
A
类载入内存,执行
A
类的
static
语句块
2.
B
类载入内存,执行
B
类的
static
语句块
3.
A
类的非
static
语句块
4.
A
类的构造方法
5.
B
类的非
static
语句块
6.
B
类的构造方法
例子:
class A{
int i=9,j; //
非
static
语句块
public A(){ //
构造方法
prt("i="+i+",j="+j);
j=10;
}
static {//static
语句块
int x1=prt("A is superclass.");
}
static int prt(String s){
System.out.println(s);
return 11;
}
}
class B extends A{
int k=prt("B is key."); //
非
static
语句块
public B(){//
构造方法
prt("k="+k+",j="+j);
}
static int x2=prt("B is childclass."); //static
语句块
public static void main(String args[]){
prt("A is key.");
B is =new B();
}
}
输出:
A is superclass.
B is childclass.
A is key.
i=9,j=0
B is key.
k=11,j=10
B is childclass.
A is key.
i=9,j=0
B is key.
k=11,j=10