码迷,mamicode.com
首页 > 编程语言 > 详细

Java复用类中正确清理

时间:2017-08-30 17:10:26      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:package   bsp   注意   sys   xtend   line   system   super   ext   

//: object/E05_DataOnly2.java
/****************** Exercise 5 ******************
 * Modify Exercise 4 so the values
 * of the data in DataOnly are assigned to and
 * printed in main().
 ************************************************/
package object;

class Shape
{
    Shape(int i)
    {
    	System.out.println("Shape 构造啦!");
    }

    void dispose()
    {
    	System.out.println("Shape dispose"); 
    }
}


class Circle extends Shape
{

    Circle(int i)
    {
        super(i);
        System.out.println("Drawing Circle");
    }

    @Override
    void dispose()
    {
    	System.out.println("Erasing Circle");
        super.dispose();
    }
}

class Triangle extends Shape
{

    Triangle(int i)
    {
        super(i);
        System.out.println("Drawing Triangle");
    }

    @Override
    void dispose()
    {
    	System.out.println("Erasing Triangle");
        super.dispose();
    }
}

class Line extends Shape
{
    private int start, end;

    Line(int start, int end)
    {
        super(start);
        this.start = start;
        this.end = end;
        System.out.println("Drawing Line: " + start + ", " + end);
    }

    @Override
    void dispose()
    {
    	System.out.println("Erasing Line: " + start + ", " + end);
        super.dispose();
    }
}

public class E05_DataOnly2 extends Shape
{
    private Circle circle;
    private Triangle triangle;
    private Line[] lines = new Line[3];

    public E05_DataOnly2(int i)
    {
        super(i + 1);                                             //Shape 构造啦!
       // super(i + 1);
        for (int j = 0; j < lines.length; j++)                    //j=0:Shape 构造啦! Drawing Line: 0, 0;j=1:Shape 构造啦! Drawing Line: 1, 1
            lines[j] = new Line(j, j * j);                        //j=2:Shape 构造啦! Drawing Line: 2, 4
        circle = new Circle(1);                                   //Shape 构造啦! Drawing Circle
        triangle = new Triangle(1);                               //Shape 构造啦! Drawing Triangle
        System.out.println("Combined 构造啦");                     //Combined 构造啦
    }

    @Override
    void dispose()
    {
    	System.out.println("CDASystem dispose() ");               //CDASystem dispose()
        
        circle.dispose();                                         //Erasing Triangle        Shape dispose
        triangle.dispose();                                       //Erasing Circle          Shape dispose
        for (int i = lines.length - 1; i >= 0; i--)               //Erasing Line: 2, 4  Shape dispose       
            lines[i].dispose();                                   //Erasing Line: 1, 1  Shape dispose       
                                                                  //Erasing Line: 0, 0  Shape dispose       
        super.dispose();                                          //Shape dispose
    }

    public static void main(String[] args)
    {
        E05_DataOnly2 x = new E05_DataOnly2(47);
        try
        {
            //...
        } finally
        {
            x.dispose();
        }
    }
}/*Output
        Shape 构造啦!
        Shape 构造啦!
        Drawing Line: 0, 0
        Shape 构造啦!
        Drawing Line: 1, 1
        Shape 构造啦!
        Drawing Line: 2, 4
        Shape 构造啦!
        Drawing Circle
        Shape 构造啦!
        Drawing Triangle
        Combined 构造啦
        CDASystem dispose() 
        Erasing Triangle
        Shape dispose
        Erasing Circle
        Shape dispose
        Erasing Line: 2, 4
        Shape dispose
        Erasing Line: 1, 1
        Shape dispose
        Erasing Line: 0, 0
        Shape dispose
        Shape dispose
        *///:~

以上注释是程序输出结果与程序执行过程的对照,需要注意的是:子类的建立与销毁是伴随着父类的建立与销毁而进行的。

Java复用类中正确清理

标签:package   bsp   注意   sys   xtend   line   system   super   ext   

原文地址:http://www.cnblogs.com/woaidongguagua/p/7453983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!