<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### [保證適當的清理](https://lingcoder.gitee.io/onjava8/#/book/08-Reuse?id=%e4%bf%9d%e8%af%81%e9%80%82%e5%bd%93%e7%9a%84%e6%b8%85%e7%90%86) Java 沒有 C++ 中析構函數的概念,析構函數是在對象被銷毀時自動調用的方法。原因可能是,在Java中,通常是忘掉而不是銷毀對象,從而允許垃圾收集器根據需要回收內存。通常這是可以的,但是有時你的類可能在其生命周期中執行一些需要清理的活動。初始化和清理章節提到,你無法知道垃圾收集器何時會被調用,甚至它是否會被調用。因此,如果你想為類清理一些東西,必須顯式地編寫一個特殊的方法來完成它,并確保客戶端程序員知道他們必須調用這個方法。最重要的是——正如在"異常"章節中描述的——你必須通過在 \*\*finally \*\*子句中放置此類清理來防止異常。 請考慮一個在屏幕上繪制圖片的計算機輔助設計系統的例子: ~~~ // reuse/CADSystem.java // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Ensuring proper cleanup // {java reuse.CADSystem} package reuse; class Shape { Shape(int i) { System.out.println("Shape constructor"); } 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 CADSystem extends Shape { private Circle c; private Triangle t; private Line[] lines = new Line[3]; public CADSystem(int i) { super(i + 1); for(int j = 0; j < lines.length; j++) lines[j] = new Line(j, j*j); c = new Circle(1); t = new Triangle(1); System.out.println("Combined constructor"); } @Override public void dispose() { System.out.println("CADSystem.dispose()"); // The order of cleanup is the reverse // of the order of initialization: t.dispose(); c.dispose(); for(int i = lines.length - 1; i >= 0; i--) lines[i].dispose(); super.dispose(); } public static void main(String[] args) { CADSystem x = new CADSystem(47); try { // Code and exception handling... } finally { x.dispose(); } } } /* Output: Shape constructor Shape constructor Drawing Line: 0, 0 Shape constructor Drawing Line: 1, 1 Shape constructor Drawing Line: 2, 4 Shape constructor Drawing Circle Shape constructor Drawing Triangle Combined constructor CADSystem.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 */ ~~~ 這個系統中的所有東西都是某種**Shape**(它本身是一種**Object**,因為它是從根類隱式繼承的) 。除了使用**super**調用該方法的基類版本外,每個類還覆蓋`dispose()`方法。特定的**Shape**類——**Circle**、**Triangle**和**Line**,都有 “draw” 構造函數,盡管在對象的生命周期中調用的任何方法都可以負責做一些需要清理的事情。每個類都有自己的`dispose()`方法來將非內存的內容恢復到對象存在之前的狀態。 在`main()`中,有兩個關鍵字是你以前沒有見過的,在"異常"一章之前不會詳細解釋:**try**和**finally**。**try**關鍵字表示后面的塊 (用花括號分隔 )是一個受保護的區域,這意味著它得到了特殊處理。其中一個特殊處理是,無論**try**塊如何退出,在這個保護區域之后的**finally**子句中的代碼總是被執行。(通過異常處理,可以用許多不同尋常的方式留下**try**塊。)這里,**finally**子句的意思是,“無論發生什么,始終調用`x.dispose()`。” 在清理方法 (在本例中是`dispose()`) 中,還必須注意基類和成員對象清理方法的調用順序,以防一個子對象依賴于另一個子對象。首先,按與創建的相反順序執行特定于類的所有清理工作。(一般來說,這要求基類元素仍然是可訪問的。) 然后調用基類清理方法,如這所示。 在很多情況下,清理問題不是問題;你只需要讓垃圾收集器來完成這項工作。但是,當你必須執行顯式清理時,就需要多做努力,更加細心,因為在垃圾收集方面沒有什么可以依賴的。可能永遠不會調用垃圾收集器。如果調用,它可以按照它想要的任何順序回收對象。除了內存回收外,你不能依賴垃圾收集來做任何事情。如果希望進行清理,可以使用自己的清理方法,不要使用`finalize()`。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看