package com.mufeng.theeighthchapter;

class Characteristic {// 特征

private String s;

public Characteristic(String s) {

// TODO Auto-generated constructor stub

this.s = s;

System.out.println("Creating Characteristic " + s);

}

protected void dispose() {

System.out.println("disposing Characteristic " + s);

}

}

class Description {// 描述

private String s;

public Description(String s) {

// TODO Auto-generated constructor stub

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");

public LivingCreature() {

// TODO Auto-generated constructor stub

System.out.println("LivingCreature()");

}

protected void dispose() {

System.out.println("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");

public Animal() {

// TODO Auto-generated constructor stub

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");

public Amphibian() {

// TODO Auto-generated constructor stub

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() {

// TODO Auto-generated constructor stub

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

源码解析

层次结构中的每个类都包含

Characteristic和

Description这两种类型的成员对象,并且它们也必须被销毁。所以万一某个子对象要依赖于其他对象,销毁的顺序应该和初始化顺序相反。对于字段,则意味着与声明的顺序相反(因为字段的初始化是按声明的顺序进行的)。对于基类(遵循

C++中析构函数的形式),应该首先对于其导出类进行清理,然后才是基类。这是因为导出类的清理可能会调用基类中的某些方法,所以需要使基类中的构件仍起作用而不应该过早地销毁它们。从输出结果可以看到,

Frog对象的所有部分都是按照创建的逆序进行销毁的。

在这个例子中可以看到,尽管通常不必执行清理工作,但是一旦选择要执行,就必须谨慎和小心。

java创建包顺序_Java中包含继承关系时对象的创建与销毁顺序详解(附源码)相关推荐

  1. java 继承先后顺序_Java中的继承关系的加载顺序

    /* 在加载类时,Java虚拟机先加载父类再加载子类,再对静态初始化块. 静态成员变量(类变量).静态方法进行一次初始化. 只有调用new方法时才会创建类的对象.按照父子继承关系进行初始化, 首先执行 ...

  2. java反射源码_java反射技术详解附源码

    在学校学习Java时,由于学的不扎实,也没经历过太多实战项目,所以很多重要的知识点瞟一眼就过去了,比如现在要讲的反射,当时直接就忽略掉了,可现在发现很多地方需要反射,不得不重新学习一下,上学欠了太多债 ...

  3. java构造块_java中的静态代码块、构造代码块、构造方法详解

    运行下面这段代码,观察其结果: package com.test; public class HelloB extends HelloA { public HelloB() { } { System. ...

  4. java 继承对象 初始化_java中具有继承关系的类及其对象初始化顺序

    先说结论 对于具有继承关系的类,它们的类和对象构造顺序为:父类的类构造器() -> 子类的类构造器() -> 父类成员变量的赋值和实例代码块 -> 父类的构造函数 -> 子类成 ...

  5. java 不同包 调用_java中不同包之间的调用

    <java中不同包之间的调用>由会员分享,可在线阅读,更多相关<java中不同包之间的调用(4页珍藏版)>请在金锄头文库上搜索. 1.建立两个类 PackageA.Packag ...

  6. java equals重写原则_java中为何重写equals时必须重写hashCode方法详解

    前言 大家都知道,equals和hashcode是java.lang.Object类的两个重要的方法,在实际应用中常常需要重写这两个方法,但至于为什么重写这两个方法很多人都搞不明白. 在上一篇博文Ja ...

  7. Linux系统中软件的“四”种安装原理详解:源码包安装、RPM二进制安装、YUM在线安装、脚本安装包...

    一.Linux软件包分类 1.1 源码包 优点: 开源,如果有足够的能力,可以修改源代码: 可以自由选择所需的功能: 软件是编译安装,所以更加适合自己的系统,更加稳定.效率更高: 卸载方便: 缺点: ...

  8. java byte数组转long_Java中byte、byte数组与int、long的转换详解

    一.Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) ...

  9. java shiro登录实例_Shiro安全框架入门篇(登录验证实例详解与源码)

    转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...

  10. Java的reactor模式_Reactor模式详解+源码实现

    1.Reactor模式介绍 Reactor模式是事件驱动模型,有一个或多个并发输入源,有一个Service Handler,有多个Request Handlers:这个Service Handler会 ...

最新文章

  1. linux bunzip2命令
  2. 小技巧:with用法 pycharm控制台输出带颜色的文字 打印进度条的
  3. NEERC2017 Archery Tournament 线段树 新套路
  4. bat与C语言混合编程,BAT与HTML混合编程的方法
  5. LWIP之UDP协议
  6. 为什么使用linux内核,为什么Linux内核使用它所做的数据结构?
  7. Unit 3 return codes and traps
  8. java aciss_C语言ACISS表.doc
  9. SVNAdmin - 好用的开源SVN管理系统
  10. PSNR峰值信噪比(python代码实现+SSIM+MSIM)
  11. JDK8下载安装与Win10下Java环境变量配置
  12. 如何查看Mac系统的位数
  13. 微信H5公众号chooseImg上传图片
  14. 股票数据接口-陈科肇
  15. 低于90分的成绩 java_查询平均成绩低于60分的学生学号、姓名及成绩。
  16. 为什么要有不同的参考文献格式?
  17. 一文看懂RPA的技术原理、产品形态、设计与构建
  18. trainging contest#1(2011大连现场赛)I BY bly
  19. listagg()转mysql
  20. TerminateThread 导致LoadLibary 死锁

热门文章

  1. Wannafly挑战赛25 A 因子
  2. FutureTask 源码解析
  3. HDU 1325 Is It A Tree?(并查集)
  4. jar 添加环境变量
  5. Repeater思路整理
  6. 虚拟机架设 ftp 服务器 pureadmin,使用pure-ftpd搭建ftp服务器(简单实现被动模式)...
  7. 芝麻HTTP:TensorFlow LSTM MNIST分类
  8. 大数据时代已经来临,正在改变我们的生活
  9. Linux如何查看哪个进程占用的SWAP分区比较多
  10. Redis的安装和使用之二------phpredis与phpRedisAdmin