Java多态-继承与清理

通过组合和继承方法来创建新类时,永远不必担心对象的清理问题,子对象通常会留给垃圾回收器进行处理。如果确是遇到清理问题,那必须用心为新的类创建dispose()方法(在这里我们选用此名)。并且由于继承的缘故,如果我们有其他作为垃圾回收一部分的特殊清理动作,就必须在导出类中覆盖被继承的dispose()方法。当覆盖被继承的diopose()方法时,务必记住调用基类版本dispose()方法;否则,基类的清理动作就不会发生。下例便是一个证明:

package polymorphism;

class Characteristic{
    private String s;
    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{
    private Characteristic p =
            new Characteristic("is alive");
    private Description t =
            new Description("Basic Living Creature");
    LivingCreature(){
        System.out.println("LivingCreature Creature");
    }
    
    protected void dispose(){
        System.out.println("LivingCreature dispsoe");
        t.dispose();
        p.dispose();
    }
}

class Animal extends LivingCreature{
    private Characteristic p = 
            new Characteristic("has heart");
    private Description t = 
            new Description("Animal not Vegetable");
    
    Animal(){System.out.print("Animal()");}
    protected void dispsoe(){
        System.out.print("Animal dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
}

class Amphibian extends Animal{
    private Characteristic p = new
            Characteristic("can i live in water");
    private Description t = 
            new Description("Both water and land");
    Amphibian(){
        System.out.print("Amphibian()");
    }
    protected void dispsoe(){
        System.out.print("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.print("Frog dispose");
        t.dispose();
        p.dispose();
        super.dispose();
    }
    public static void main(String[] args){
        Frog frog = new Frog();
        System.out.println("bye");
        frog.dispose();
    }
    
}

下面是运行结果:
 
Creating Characteristic is alive
Creating DescriptionBasic Living Creature
LivingCreature Creature
Creating Characteristic has heart
Creating DescriptionAnimal not Vegetable
Animal()Creating Characteristic can i live in water
Creating DescriptionBoth water and land
Amphibian()Creating Characteristic Croaks
Creating DescriptionEats Bugs
Frog()
bye
Frog disposedisposing DescriptionEats Bugs
disposing Characteristic Croaks
LivingCreature dispsoe
disposing DescriptionBasic Living Creature
disposing Characteristic is alive

层次结构中的每个类都包含Charactersitic和Description这两种类型的成员对象,并且它们也必须被销毁。所以万一某个子对象要依赖其他对象,销毁顺序应该和初始化顺序相反。对于字段,则意味着与声明的顺序相反。对于基类(遵循C++中的析构函数),应该首先使用基类对其导出类进行清理,然后是基类。这是因为导出类的清理可能会调用基类的某些方法,所以需要使其基类中的构件仍起作用而不应过早的销毁。

Java多态-继承与清理,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。