Java複用類中正確清理

來源:互聯網
上載者:User

標籤: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複用類中正確清理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.