Java內部類學習筆記

来源:http://www.cnblogs.com/heleiz/archive/2016/09/25/5901487.html
-Advertisement-
Play Games

20160923 定義:將一個類的定義放在另一個類的內部; 從外部類的非靜態方法之外,創建某個內部類的對象:OutClassName.InnerClassName; 內部類擁有所有其外部類的成員的訪問權; 成員內部類不能含有static修飾的變數和方法,因為成員內部類需要先創建了外部類,才能創建它自 ...


20160923

  • 定義將一個類的定義放在另一個類的內部;
  • 從外部類的非靜態方法之外,創建某個內部類的對象:OutClassName.InnerClassName;
  • 內部類擁有所有其外部類的成員的訪問權;
  • 成員內部類不能含有static修飾的變數和方法,因為成員內部類需要先創建了外部類,才能創建它自己的
  • 內部類中生成外部類對象的引用,可以使用OutClassName.this;
 1 public class DoThis {
 2     void f(){System.out.println("DoThis.f()");}
 3     public class Inner{
 4         public DoThis getOuter(){
 5             return DoThis.this;
 6         }
 7     }
 8     public Inner getInner(){
 9         return new Inner();
10     }
11     public static void main(String[] args) {
12         DoThis dThis = new DoThis();
13         DoThis.Inner dThisInner = dThis.getInner();
14         dThisInner.getOuter().f();
15     }
16 }
  • 創建內部類的對象,必須使用外部類對象的引用;
1 public class DoNew {
2     public class Inner{};
3     public static void main(String[] args) {
4         DoNew doNew = new DoNew();
5         DoNew.Inner dInner = doNew.new Inner();
6     }
7 }
  • 創建嵌套類(靜態內部類),不需要外部類對象的引用;
  • private修飾的內部類,只能在其外部類內部訪問;protected修飾的內部類,只有其外部類、其外部類的子類、其外部類同一個包中的其他類可以訪問;
 1 class Parcel4{
 2     private class PContents implements Contents {
 3         private int i = 11;
 4         @Override
 5         public int value() { return i; }
 6     }
 7     protected class PDestination implements Destination {
 8         private String label;
 9         private PDestination(String whereTo) { label = whereTo; }
10         @Override
11         public String readLabel() { return label; }
12     }
13     public Destination destination(String s){ return new PDestination(s); }
14     public Contents contents(){ return new PContents(); }
15     Parcel4.PContents t;//PContents是private,只能在Parcel4內部訪問
16 }
17 public class TestParcel {
18     public static void main(String[] args) {
19         Parcel4 p = new Parcel4();
20         Contents contents = p.contents();
21         Destination destination = p.destination("Tasmania");
22         //Parcel4.PContents pc = p.new PContents();//PContents是private,只能在Parcel4內部訪問,此處報錯
23     }
24 }
  • 複雜的內部類:在方法或作用域內定義內部類,理由如下:
  1. 實現了某個類型的介面,可以創建並返回對介面的引用
  2. 需解決複雜的問題,想創建一個類輔助實現解決方案,但不希望這個類被公用
  • 舉例

    1、一個定義在方法中的類

 1 public class Parcel5 {
 2     public Destination destination(String s){
 3         class PDestination implements Destination{
 4             private String label;
 5             private PDestination(String whereTo){
 6                 label = whereTo;
 7             }
 8             @Override
 9             public String readLabel() { return label; }
10         }
11         return new PDestination(s);
12     }
13     public static void main(String[] args) {
14         Parcel5 p = new Parcel5();
15         Destination destination = p.destination("Tasmania");
16     }
17 }

    2、一個定義在作用域中的類,作用域在方法的內部

 1 public class Parcel6 {
 2     private void internalTracking(boolean b) {
 3         if (b){
 4             class TrackingSlip {
 5                 private String id;
 6                 public TrackingSlip(String s) {
 7                     id = s;
 8                 }
 9                 String getSlip(){ return id;}
10             }
11             TrackingSlip ts = new TrackingSlip("slip");
12             String s = ts.getSlip();
13         }
14         //Can't use it here!Out of scope:
15         //TrackingSlip ts = new TrackingSlip("x");
16     }
17     public void track(){internalTracking(true);}
18     public static void main(String[] args) {
19         Parcel6 p = new Parcel6();
20         p.track();
21     }
22 }

    3、一個實現了介面的匿名類

    4、一個匿名類,擴展了有非預設構造器的類

    5、一個匿名類,執行欄位初始化

    6、一個匿名類,通過實例初始化實現構造(匿名類沒有構造器)

  • 傳遞給匿名內部類的參數,並且在匿名內部類中使用,該參數須定義為final;
  • 匿名內部類可擴展類,也可以實現介面,但不能同時;如果實現介面,只能實現一個介面;
  • 《Java編程思想》199頁,10.6.1再訪工廠方法,使用匿名內部類的例子,非常好;
  • 嵌套類:static修飾的內部類,無法訪問非靜態的外部類對象
  • 嵌套類可以作為介面的一部分,甚至實現外部介面
  •  1 public interface ClassInInterface {
     2     void howdy();
     3     class Test implements ClassInInterface{
     4         @Override
     5         public void howdy() { System.out.println("Howdy"); }
     6         public static void main(String[] args){
     7             new Test().howdy();
     8         }
     9     }
    10 }
  • 內部類能夠多層嵌套,並且能夠透明的訪問所有它嵌入的外部類的成員
  •  1 class A{
     2     private void f(){}
     3     class B {
     4         private void g(){}
     5         public class C {
     6             void h() {
     7                 g();
     8                 f();
     9             }
    10         }
    11     }
    12 }
    13 public class MultiNestingAccess {
    14     public static void main(String[] args) {
    15         A a = new A();
    16         A.B ab = a.new B();
    17         A.B.C abc =ab.new C();
    18         abc.h();
    19     }
    20 }
  • 為什麼需要內部類?
    • 《Java編程思想》204頁解釋為:每個內部類都能獨立的繼承自一個(介面的)實現,所以無論外圍類是否已經繼承了某個(介面的)實現,對於內部類都沒有影響;
    • 簡單講就是,內部類實現介面、繼承某個類,比外部類實現介面少了許多顧慮,外部類實現介面需要考慮全面,在其他地方是否有影響;
    • 內部類可以繼承多個具體類或抽象類,與介面配合,使“多重繼承”的解決方案變得完美;
    • 內部類可以有多個實例,每個實例都有自己的狀態信息,並且與外部類對象相互獨立;
    • 在單個外部類中,可以使多個內部類以不同的方式實現同一個介面,或繼承同一個類;
    • 內部類對象的創建,並不依賴於外部類對象的創建
    • 內部類沒有“is-a”關係,內部類是獨立的實體
  • 閉包,記錄了創建閉包的作用域的一些信息,使得閉包可調用其外部作用域數據;內部類就是面向對象的閉包;
  • 回調,
  • 通過內部類提供閉包功能舉例:
  •  1 package com.helei.innerclasses;
     2 interface Incrementable {
     3     void increment();
     4 }
     5 class Callee1 implements Incrementable {
     6     private int i = 0;
     7     @Override
     8     public void increment() {
     9         i++;
    10         System.out.println(i);;
    11     }
    12 }
    13 class MyIncrement {
    14     public void increment() { System.out.println("Other operation");}
    15     static void f(MyIncrement mi) {mi.increment();}
    16 }
    17 class Callee2 extends MyIncrement {
    18     private int i = 0;
    19     public void increment() {
    20         super.increment();
    21         i++;
    22         System.out.println(i);
    23     }
    24     private class Closure implements Incrementable {
    25         public void increment() {
    26             Callee2.this.increment();
    27         }
    28     }
    29     Incrementable getCallbackReference() {
    30         return new Closure();
    31     }
    32 }
    33 class Caller {
    34     private Incrementable callbackReference;
    35     Caller(Incrementable cbh){callbackReference = cbh;}
    36     void go() {callbackReference.increment();}
    37 }
    38 public class Callbacks {
    39     public static void main(String[] args) {
    40         Callee1 c1 = new Callee1();
    41         Callee2 c2 = new Callee2();
    42         MyIncrement.f(c2);
    43         Caller caller1 = new Caller(c1);
    44         Caller caller2 = new Caller(c2.getCallbackReference());
    45         caller1.go();
    46         caller1.go();
    47         caller2.go();
    48         caller2.go();
    49     }
    50 }
  • 看了好幾遍才梳通了以上流程,最好能夠敲一遍,通過debug調試過一遍
  • 內部類與控制框架
  • 內部類的繼承
  • 內部類覆蓋,無效;可以顯式繼承某內部類;
  • 局部內部類,與匿名內部類的區別

 

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、首先他們兩個介面都是為了實現對象的序列化,使之可以傳遞,所謂序列化就是將對象信息裝換成可以存儲的介質的過程。 2、Serializable是jdk所提供的序列化介面,該介面存在於io包下,可想用於輸入輸出,使用非常簡單,只要讓你的類實現此介面就ok了;可以使用transient關鍵字修飾你不想序 ...
  • 約定:句子以空格為詞語分割符號,以句號為結束符號。 實現思路: 用函數explode(separator,string,limit)對字元串進行分割,再對得到的數據最後一個成員分割切掉符號。用一個新的數組倒序接收轉為字元串,並補上句號。 代碼實現: 效果: ...
  • 1.簡單示例: SpringBoot中的的配置簡單屬性類支持ConfigurationProperties方式,看一個簡單的示例。 1 @ConfigurationProperties(prefix = "org.dragonfei.demo") 2 public class DemoPropert ...
  • python開發【第一篇】:目錄 python開發【第二篇】:python初體驗 python開發【第三篇】: python開發【第四篇】: python開發【第五篇】: python開發【第六篇】: ...
  • hibernate入門 1、orm hibernate是一個經典的開源的orm[數據訪問中間件]框架 ORM( Object Relation Mapping)對象關係映射 通過 對象模型 操作 資料庫關係模型 hibernate處於項目的持久層位置,因此又稱為持久層框架 2、hibernate核心 ...
  • DOM基於樹形,SAX基於事件,DOM4J和JDOM基於底層API ...
  • 在剛學習SpringMVC框架整合時,你也許會產生疑問為什麼Spring.xml和SpringMVC.xml中都有註解過濾。 <context:component-scan base-package="myproject"> 和<context:component-scan base-package ...
  • 前言 最近要找工作,免不得要有一番筆試,今年好像突然就都流行線上筆試了,真是搞的我一塌糊塗。有的公司呢,不支持Python,Java我也不會,C有些數據結構又有些複雜,所以是時候把STL再看一遍了…不會告訴你距離上次使用可能已經有半年以上了。 STL是什麼 STL為C++的標準模版庫,又稱為C++泛 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...