zoukankan      html  css  js  c++  java
  • Java继承与清理

    【程序实例】

     1 import java.util.*;
     2 
     3 class Characteristic {
     4     private String s;
     5     Characteristic(String s) {
     6         this.s=s;
     7         System.out.println("Creating Characteristic "+s);
     8     }
     9     protected void dispose() {
    10         System.out.println("Disposing Characteristic "+s);
    11     }
    12 }
    13 
    14 class Description {
    15     private String s;
    16     Description(String s) {
    17         this.s=s;
    18         System.out.println("Creating Description "+s);
    19     }
    20     protected void dispose() {
    21         System.out.println("Disposing Description "+s);
    22     }
    23 }
    24 
    25 class LivingCreature {
    26     private Characteristic p=new Characteristic("is alive");
    27     private Description t=new Description("Basic Living Creature");
    28     LivingCreature() {
    29         System.out.println("LivingCreature()");
    30     }
    31     protected void dispose() {
    32         System.out.println("LivingCreature dispose");
    33         t.dispose();
    34         p.dispose();
    35     }
    36 }
    37 
    38 class Animal extends LivingCreature {
    39     private Characteristic p=new Characteristic("has heart");
    40     private Description t=new Description("Animal not Vegetable");
    41     Animal() {
    42         System.out.println("Animal()");
    43     }
    44     protected void dispose() {
    45         System.out.println("Animal dispose");
    46         t.dispose();
    47         p.dispose();
    48         super.dispose();
    49     }
    50 }
    51 
    52 class Amphibian extends Animal {
    53     private Characteristic p=new Characteristic("can live in water");
    54     private Description t=new Description("Both water and land");
    55     Amphibian() {
    56         System.out.println("Amphibian()");
    57     }
    58     protected void dispose() {
    59         System.out.println("Amphibian dispose");
    60         t.dispose();
    61         p.dispose();
    62         super.dispose();
    63     }
    64 }
    65 
    66 public class Frog extends Amphibian {
    67     private Characteristic p=new Characteristic("Croaks");
    68     private Description t=new Description("Eats Bugs");
    69     public Frog() {
    70         System.out.println("Frog()");
    71     }
    72     protected void dispose() {
    73         System.out.println("Frog dispose");
    74         t.dispose();
    75         p.dispose();
    76         super.dispose();
    77     }
    78 
    79     public static void main(String[] args) {
    80         Frog frog=new Frog();
    81         System.out.println("Bye!");
    82         frog.dispose();
    83     }
    84 }

    【运行结果】

     1 Creating Characteristic is alive
     2 Creating Description Basic Living Creature
     3 LivingCreature()
     4 Creating Characteristic has heart
     5 Creating Description Animal not Vegetable
     6 Animal()
     7 Creating Characteristic can live in water
     8 Creating Description Both water and land
     9 Amphibian()
    10 Creating Characteristic Croaks
    11 Creating Description Eats Bugs
    12 Frog()
    13 Bye!
    14 Frog dispose
    15 Disposing Description Eats Bugs
    16 Disposing Characteristic Croaks
    17 Amphibian dispose
    18 Disposing Description Both water and land
    19 Disposing Characteristic can live in water
    20 Animal dispose
    21 Disposing Description Animal not Vegetable
    22 Disposing Characteristic has heart
    23 LivingCreature dispose
    24 Disposing Description Basic Living Creature
    25 Disposing Characteristic is alive

     总结:

      1.java中进行清理时要小心,在清理函数dispose()中数据成员的销毁顺序应该与声明的顺序相反,并且记得要调用基类的清理函数对基类进行清理(采用super.dispose()语句实现);

      2.对于字段,与声明的顺序相反进行清理;对于基类,应该首先对其导出类进行清理,然后才是基类(这是因为导出类的清理可能会调用基类中的某些方法,所以需要基类中的构件仍起作用而不应该过早地销毁它们。

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

  • 相关阅读:
    用Visual Studio 2005/2008提取EXE文件中的资源[图片|htm|光标文件]
    C# 操作Excel之旁门左道 [ C# | Excel ]
    ExtJs 备忘录(1)—— Form表单(一) [ 控件使用 ]
    Win7(64位)安装Microsoft SQL Server Management Studio Express[error 29506]
    ExtJs 备忘录(8)—— 管理界面搭建和其他部分控件介绍
    Visual Studio 模板 —— 自定义WebForm模板
    让Visual Studio 也支持JS代码折叠 [ Visual Studio | #region | #endregion ]
    ExtJs 备忘录(3)—— Form表单(三) [ 数据验证 ]
    ExtJs 备忘录(7)—— GirdPanl表格(三) [ 统计|查看、修改单行记录 ]
    让Visual Studio 也支持JS代码折叠 —— 续 [ Visual Studio | Js | ScriptOutline | SmallOutline ]
  • 原文地址:https://www.cnblogs.com/acode/p/5315437.html
Copyright © 2011-2022 走看看