package com.shape.base;
public class Shape {
private int field = 20;
public Shape() {
System.out.println("field=" + field);
System.out.println("基类构造器");
draw();
}
public int getField() {
return field;
}
public void draw() {
System.out.println("Shape.draw()");
}
}
package com.shape.test;
import com.shape.base.Shape;
import com.shape.sub.Circle;
import com.shape.sub.Square;
import java.util.Random;
public class Test extends Shape{
private Shape s = new Shape();
public Test() {
super();
//System.out.println("i=" + i);
System.out.println("导出类构造器");
}
public void draw() {
System.out.println("导出类draw()");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test();
}
}
类的初始化过程
首先介绍一下类的加载过程
当我们运行 java Test 时候 程序首先加载类为Test类,然后加载它的基类Shape,如果Shape还有基类的话 ,会继续加载下去 ,如此类推 ,直到根基类,
类的加载介绍完了
接下来讲初始化
当类加载完毕之后 开始new Test() 对象了 ,所以会调用Test类的构造方法 ,这时会先调用基类Shape的构造方法 ,基类的构造方法完毕之后,然后是Test类成员的初始化
,成员初始化完毕之后才会执行导出类Test的构造方法其余的语句