第10章 內部類,第10章

來源:互聯網
上載者:User

第10章 內部類,第10章

  • 內部類:在類中定義的類,外層的叫外部類、外圍類。
  • 書中說P191,如果想在外部類的非靜態方法之外的任意位置建立某個內部類對象,那麼必須像在main方法(靜態方法)中那樣,具體地指明這個對象的類型:OuterClassName.InnerClassName。(在外部類非靜態方法中可以直接使用InnerClassName)
  • 書上(10.2)的例子,說在內部類,可以直接存取其外圍類的所有成員(方法和欄位),就像內部類自己擁有它們似的,這帶來了很大的便利。(但是在main函數(外圍類的static函數)中,內部類的對象不能訪問外部類的成員)
  • 內部類自動擁有對其外圍類所有成員的訪問權p192。這是如何做到的呢?當某個外圍類的對象建立了一個內部類對象時,此內部類對象必定會秘密地捕獲一個指向那個外圍類對象的引用。然後,
  •  迭代器設計模式p192:外圍類存資料,並且存在產生內部類對象的方法,私人內部類實現Selector介面以作為迭代器訪問資料。
  • 使用"OuterClassName.this"(p193):書上說有時候,需要在內部類的中,產生包含它的外部類對象的引用,可以使用此文法
 1 public class DotThis { 2  3 void f() { 4  5 System.out.println("DotThis.f()"); 6  7 } 8  9 public class Inner {10 11 public DotThis outer() {12 13 f();14 15 return DotThis.this;16 17 // A plain "this" would be Inner's "this"18 19 }20 21 }22 23 public Inner inner() {24 25 return new Inner();26 27 }28 29 public static void main(String[] args) {30 31 DotThis dt = new DotThis();32 33 DotThis.Inner dti = dt.inner();34 35 dti.outer().f();36 37 }38 39 } /*40 41  * Output: DotThis.f()42 43  */// :~
View Code
  • 使用“OuterClass對象的引用.new”:建立內部類對象,必須先指明外部類對象的引用,而不是"new OuterClassName.InnerClassName();"。

Eg:

 1 public class DotNew { 2  3   public class Inner { 4  5   public Inner(){ 6  7   System.out.println("Inner class constructor."); 8  9   }10 11   }12 13   public static void main(String[] args) {14 15     DotNew dn = new DotNew();16 17     DotNew.Inner dni = dn.new Inner();     //dn已經指明外部類,故不需(也不能)dn.new DotNew.Inner();18 19     //!DotNew.Inner dni2 = new DotNew.Inner();20 21   }22 23 } ///:~
View Code
  • 匿名內部類(p197):在函數內部定義類的一種方式,不顯示定義類名,一次性使用。一般有父類、父介面。在定義的地方之外,也能使用匿名內部類的函數。匿名內部類定義結束分好分隔。
  • 靜態函數用作類似於建構函式的功能(p198),靜態函數裡面有一段代碼實現執行個體初始化功能。
  • 書上(198-199)說:包含匿名內部類的函數,參數如果會在匿名內部類內部被使用個,則必須是final的;(但是實際使用的時候,去掉final也不見報錯)
  • 執行個體初始化:什麼是”執行個體初始化“? 匿名內部類由於類名被省略,故沒有建構函式,用”執行個體初始化“功能來代替建構函式,書上例子Parcel10.java所示,實現”執行個體初始化“功能的代碼必須要被大括弧圍住。
  • 匿名內部類既可以擴充類,也可以實現介面,但是兩者不可以同時兼備。而且不能實現多個介面。
  •  使用“匿名內部類”的“Factory 方法設計模式”改寫interfaces/Games.java

 

 1 package exercise; 2 import static net.mindview.util.Print.*; 3  4 interface Game { 5     boolean move(); 6 } 7  8 interface GameFactory { 9     Game getGame();10 }11 12 class Checkers implements Game{13     private int moves = 0;14     private static final int MOVES = 3;15     public boolean move(){16         print("Checkers move " + moves);17         return ++moves != MOVES;18     }19     public static GameFactory factory = new GameFactory(){20         public Game getGame(){21             return new Checkers();22         }23     };24 }25 26 27 public class Common {28     29     public static void serviceConsumer(GameFactory fact){30         Game g = fact.getGame();31         while(g.move());32     }33     34     public static void main(String[] args) {35         serviceConsumer(Checkers.factory);36     }37 38 }
View Code

 

聯繫我們

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