java 多态理解(2)

继承与清理
简单的例子如下所示

class Characteristic implements cp{
    private String s;
    public Characteristic(String s) {
        // TODO Auto-generated constructor stub
        this.s=s;
        cp.p.print("Creating Characteristic "+s);
    }
    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        cp.p.print("disposing Characteristic "+s);
    }
}

class Descripting implements cp{
    private String s;
    public Descripting(String s) {
        // TODO Auto-generated constructor stub
        this.s=s;
        cp.p.print("Creating Descripting "+s);
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        cp.p.print("dispose Descripting "+s);
    }

}

class LivingCreature implements cp{
    private Characteristic c=new Characteristic("is alive");
    private Descripting d=new Descripting("Basic Living Creature");
    public LivingCreature() {
        // TODO Auto-generated constructor stub
        cp.p.print("LivingCreature()");
    }
    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        cp.p.print("LivingCreature dispose");
        c.dispose();
        d.dispose();
    }
}
class Animal extends LivingCreature{
    private Characteristic c=new Characteristic("has heart");
    private Descripting d=new Descripting("Animal not Vegetable");
    public Animal() {
        // TODO Auto-generated constructor stub
        cp.p.print("Animal()");
    }
    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        cp.p.print("Animal dispose");
        c.dispose();
        d.dispose();
        super.dispose();
    }
}
class Amphibian extends Animal{
    private Characteristic c=new Characteristic("can live in water");
    private Descripting d=new Descripting("Both water and land");
    public Amphibian() {
        // TODO Auto-generated constructor stub
        cp.p.print("Amphibian()");
    }
    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        cp.p.print("Amphibian dispose");
        c.dispose();
        d.dispose();
        super.dispose();
    }
}
interface cp{
     void dispose();
     class p{
         static void print(String s){
             System.out.println(s);
         }
     }
}
public class Flog extends Amphibian{
    private Characteristic c=new Characteristic("Croaks");
    private Descripting d=new Descripting("Eats Bugs");
    public Flog() {
        // TODO Auto-generated constructor stub
        cp.p.print("Flog()");
    }
    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        cp.p.print("Flog dispose");
        c.dispose();
        d.dispose();
        super.dispose();
    }
    public static void main(String args[]){
        Flog f=new Flog();
        cp.p.print("Bye!");
        f.dispose();
    }
}

运行结果如下所示:

Creating Characteristic is alive
Creating Descripting Basic Living Creature
LivingCreature()
Creating Characteristic has heart
Creating Descripting Animal not Vegetable
Animal()
Creating Characteristic can live in water
Creating Descripting Both water and land
Amphibian()
Creating Characteristic Croaks
Creating Descripting Eats Bugs
Flog()
Bye!
Flog dispose
disposing Characteristic Croaks
dispose Descripting Eats Bugs
Amphibian dispose
disposing Characteristic can live in water
dispose Descripting Both water and land
Animal dispose
disposing Characteristic has heart
dispose Descripting Animal not Vegetable
LivingCreature dispose
disposing Characteristic is alive
dispose Descripting Basic Living Creature

Flog 类中包含两个类型Characteristic,Description的成员对象。
由结果可见销毁的顺序(dispose方法调用)和初始化的顺序相反。
对于字段,则意味着与声明的顺序相反。

你可能感兴趣的:(java基础知识)