Java內部類(5):應用例

來源:互聯網
上載者:User

標籤:back   ble   i++   void   調用順序   mil   main   public   一個   

例1-閉包(Closure)

閉包是一個可調用的對象(通過Callback),它記錄了一些資訊,這些資訊來自於建立它的範圍

 1 interface Incrementable { 2     void increment(); 3 } 4  5 class Callee1 implements Incrementable { 6     private int i = 0; 7  8     @Override 9     public void increment() {10         i++;11         System.out.println(i);12     }13 }14 15 class MyIncrement {16     public void increment() {17         System.out.println("Other operation");18     }19 20     static void f(MyIncrement mi) {21         mi.increment();22     }23 }24 25 class Callee2 extends MyIncrement {26     private int i = 0;27 28     @Override29     public void increment() {30         super.increment();31         i++;32         System.out.println(i);33     }34 35     private class Closure implements Incrementable {36         @Override37         public void increment() {38             Callee2.this.increment();39         }40     }41 42     Incrementable callBack() {43         return new Closure();44     }45 }46 47 class Caller {48     private Incrementable callbackReference;49 50     Caller(Incrementable callbackReference) {51         this.callbackReference = callbackReference;52     }53 54     void go() {55         callbackReference.increment();56     }57 }58 59 /**60  * 關鍵字: 內部類應用, 閉包(Closure)<br/>61  * 1.閉包是一個可調用的對象(通過Callback),它記錄了一些資訊,這些資訊來自於建立它的範圍<br/>62  */63 public class Test007 {64 65     public static void main(String[] args) {66         Callee1 c1 = new Callee1();67         c1.increment(); // 168         Callee2 c2 = new Callee2();69         MyIncrement.f(c2); // Other operation|170         Caller caller1 = new Caller(c1);71         caller1.go(); // 272         Caller caller2 = new Caller(c2.callBack());73         caller2.go(); // Other operation|274     }75 }

 

例2-複雜情況下的調用順序

 1 class Egg2 { 2  3     protected class Yolk { 4         public Yolk() { 5             System.out.println("Egg2.Yolk()"); 6         } 7  8         public void f() { 9             System.out.println("Egg2.Yolk.f()");10         }11     }12 13     private Yolk y = new Yolk();14 15     public Egg2() {16         System.out.println("New Egg2()");17     }18 19     public void insertYolk(Yolk yy) {20         y = yy;21     }22 23     public void g() {24         y.f();25     }26 }27 28 class Test008Sub extends Egg2 {29     public Test008Sub() {30         System.out.println("AA");31         insertYolk(new Yolk());32     }33 34     public class Yolk extends Egg2.Yolk {35         public Yolk() {36             System.out.println("BigEgg2.Yolk()");37         }38 39         public void f() {40             System.out.println("BigEgg2.Yolk.f()");41         }42     }43 }44 45 // 複雜情況下的調用順序46 public class Test008 {47     public static void main(String[] args) {48         new Test008Sub().g();49         // Egg2.Yolk()50         // New Egg2()51         // AA52         // Egg2.Yolk()53         // BigEgg2.Yolk()54         // BigEgg2.Yolk.f()55     }56 }

 

Java內部類(5):應用例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.