• 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.对象的所有部分都是按照创建的逆序进行销毁。

  • 相关阅读:
    页面监控容器实现记录
    负载均衡基础理论
    asp.net部署时加密config文件
    还原bak到localdb的问题:The logical database file cannot be found ldf
    Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序
    Window vista 以上制作自定义证书并为端口配置ssl
    1-6、算法设计常用思想之迭代法
    1-5、算法设计常用思想之穷举法
    1-4、算法设计常用思想之动态规划法
    游戏开发-cocos creator踩坑-bind(this)导致的事件监听off不掉
  • 原文地址:https://www.cnblogs.com/acode/p/5315437.html
Copyright © 2020-2023  润新知