Java对象初始化

时间:2021-10-09 14:15:22

《Java编程思想(第4版)》note

Frog.java
//Cleanup and inheritance
class Characteristic{
    private String s;
    public static String static_Chara;
    static{
        static_Chara = "****Characteristic static Init!" ;
        System.out.println(static_Chara);
    }
    Characteristic(String s){
        this.s = s;
        System.out.println("Creating Characteristic " + s);
    }
    protected void dispose(){
        System.out.println("disposing Characteristic " + s);
    }
}
class Description{
    private String s;
    Description(String s){
        this.s = s;
        System.out.println("Creating Description " + s);
    }
    protected void dispose(){
        System.out.println("disposing Description " + s);
    }
}
class LivingCreature{
    public static String static_LC;
    static{
        static_LC = "****LivingCreature static Init!" ;
        System.out.println(static_LC);
    }
    private Characteristic p = new Characteristic("is alive");
    private Description t = new Description("Basic Living Creature");
    LivingCreature(){
        System.out.println("LivingCreature()");
    }
    protected void dispose(){
        System.out.println("LivingCreature dispose");
        t.dispose();
        p.dispose();
    }
}
class Animal extends LivingCreature{
    public static String static_Animail;
    static{
        static_Animail = "****Animal static Init!" ;
        System.out.println(static_Animail);
    }
    private Characteristic p = new Characteristic("has heart");
    private Description t = new Description("Animal not vegetable");
    Animal(){
        System.out.println("Animal()");
    }
    protected void dispose(){
        System.out.println("Animal dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
}
class Amphibian extends Animal{
    private Characteristic p = new Characteristic("can live in water");
    private Description t = new Description("Both water and land");
    Amphibian(){
        System.out.println("Amphibian()");
    }
    protected void dispose(){
        System.out.println("Amphibian dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
}
public class Frog extends Amphibian{
    private Characteristic p = new Characteristic("Croaks");
    private Description t = new Description("Eats Bugs");
    public Frog(){
        System.out.println("Frog()");
    }
    protected void dispose(){
        System.out.println("Frog dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
    public static void main(String[] args){
        System.out.println("main()");
        Frog frog = new Frog();
        System.out.println("Bye!");
        frog.dispose();
    }
}
输出
D:\Javawork\thinkinjava\ch5>java Frog
****LivingCreature static Init!
****Animal static Init!
main()
****Characteristic static Init!
Creating Characteristic is alive
Creating Description Basic Living Creature
LivingCreature()
Creating Characteristic has heart
Creating Description Animal not vegetable
Animal()
Creating Characteristic can live in water
Creating Description Both water and land
Amphibian()
Creating Characteristic Croaks
Creating Description Eats Bugs
Frog()
Bye!
Frog dispose
disposing Description Eats Bugs
disposing Characteristic Croaks
Amphibian dispose
disposing Description Both water and land
disposing Characteristic can live in water
Animal dispose
disposing Description Animal not vegetable
disposing Characteristic has heart
LivingCreature dispose
disposing Description Basic Living Creature
disposing Characteristic is alive

对象创建过程(无继承):假设类名为Obj
需要Obj.class时
1.加载类文件
2.初始化static
new Obj()
3.分配空间,清零
4.字段定义处的初始化
5.Obj(),构造器。

有继承时。如上述示例。
此时继承关系为:Frog → Amphibian → Animal → LivingCreature
1.main方法在Frog中,所以,先加载Frog.class,发现继承关系(extends),继续加载其基类,如此往上。
2.加载完成后,首先执行根基类的static初始化动作,然后逐层往下执行static初始化。
3.执行main函数。
4.遇到new Frog()
5.逐层往上,先执行根基类的初始化,调用其构造器。再执行下层基类初始化,构造器。

初始化时遇到new Characteristic(),加载Characteristic.class,static初始化,(字段定义处初始化),构造器。

so output:

****LivingCreature static Init!
****Animal static Init!
main()
****Characteristic static Init!
Creating Characteristic is alive
Creating Description Basic Living Creature