当前位置: 代码迷 >> J2SE >> 多态的继承与清理有关问题
  详细解决方案

多态的继承与清理有关问题

热度:99   发布时间:2016-04-24 02:10:05.0
多态的继承与清理问题
代码有点长 请打下耐心帮忙啊

Java code
package polymorphism;//import static net.mindview.util.Print.*;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()");  }  protected void dispose() {    print("LivingCreature dispose");    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.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) {    Frog frog = new Frog();    System.out.println("Bye!");    frog.dispose();  }}


输出结果是这样的:

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

但是 初始化顺序不是 先初始化块然后才是构造器吗,结果不应该是先把
private Characteristic p = new Characteristic("Croaks");
private Description t = new Description("Eats Bugs");
给初始化吗,谁能给个解释啊,先谢谢啦

------解决方案--------------------
程序由main开始,new Frog()时现对其父类Amphibian初始化,发现其父类还有父类Animal,暂且放下Amphibian,初始化Animal。。。。。
------解决方案--------------------
初始化顺序:
父类静态域
父类普通域
父类构造器
本类静态域
本类普通域
本类构造器
  相关解决方案