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++中的析构函数),应该首先使用基类对其导出类进行清理,然后是基类。这是因为导出类的清理可能会调用基类的某些方法,所以需要使其基类中的构件仍起作用而不应过早的销毁。

转载于:https://www.cnblogs.com/fxyfirst/p/3775614.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值