Thinking in Java——筆記(10)

来源:http://www.cnblogs.com/apolloqq/archive/2016/12/09/6151351.html
-Advertisement-
Play Games

Inner Classes ___ It allows you to group classes that logically belong together and to control the visibility of one within the other. It knows about ...


Inner Classes


  • It allows you to group classes that logically belong together and to control the visibility of one within the other.
  • It knows about and can communicate with the surrounding class.

Creating inner classes

  • You place the class definition inside a surrounding class.
  • More typically, an outer class will have a method that returns a reference to an inner class.
  • If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName.
  • When you create an inner class, an object of that inner class has a link to the enclosing object that made it, and so it can access the members of that enclosing object.
  • Inner classes have access rights to all the elements in the enclosing class.
  • The inner class can access methods and fields from the enclosing class as if it owned them.
  • The inner class secretly captures a reference to the particular object of the enclosing class that was responsible for creating it.
  • When you refer to a member of the enclosing class, that reference is used to select that member.
  • An object of an inner class can be created only in association with an object of the enclosing class when the inner class is non-static.

Using .this and .new

  • If you need to produce the reference to the outer-class object, you name the outer class followed by a dot and this.
  • Sometimes you want to tell some other object to create an object of one of its inner classes.
  • To do this you must provide a reference to the other outer-class object in the new expression, using the .new syntax.
  • To create an object of the inner class directly, you must use an object of the outer class to make an object of the inner class.
  • It’s not possible to create an object of the inner class unless you already have an object of the outer class. because the object of the inner class is quietly connected to the object of the outer class that it was made from.
  • If you make a nested class (a static inner class), then it doesn’t need a reference to the outer-class object.

Inner classes and upcasting

  • Normal (non-inner) classes cannot be made private or protected; they may only be given public or package access.
  • You can’t even downcast to a private inner class (or a protected inner class unless you’re an inheritor), because you can’t access the name.
  • The private inner class provides a way for the class designer to completely prevent any type-coding dependencies and to completely hide details about implementation.

Inner classes in methods and scopes

  • Inner classes can be created within a method or even an arbitrary scope.

Anonymous inner classes

  • What this strange syntax means is "Create an object of an anonymous class that’s inherited from Contents."
  • If your base class needs a constructor with an argument, you simply pass the appropriate argument to the base-class constructor.
  • The semicolon at the end of the anonymous inner class doesn’t mark the end of the class body. Instead, it marks the end of the expression that happens to contain the anonymous class.
  • If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class, the compiler requires that the argument reference be final.
  • With instance initialization, you can, in effect, create a constructor for an anonymous inner class.
  • In effect, an instance initializer is the constructor for an anonymous inner class. you can have only one of these constructors.
  • Anonymous inner classes can either extend a class or implement an interface, but not both. And if you do implement an interface, you can only implement one.

Factory Method revisited

  • Prefer classes to interfaces. If your design demands an interface, you’ll know it. Otherwise, don’t put it in until you are forced to.

Nested classes

  • You don’t need an outer-class object in order to create an object of a nested class.
  • You can’t access a non-static outer-class object from an object of a nested class.
  • Fields and methods in ordinary inner classes can only be at the outer level of a class, so ordinary inner classes cannot have static data, static fields, or nested classes.
  • A nested class does not have a special this reference, which makes it analogous to a static method.

Classes inside interfaces

  • A nested class can be part of an interface, any class you put inside an interface is automatically public and static.
  • You can even implement the surrounding interface in the inner class.
  • It’s convenient to nest a class inside an interface when you want to create some common code to be used with all different implementations of that interface.
  • You can use a nested class to hold your test code.

Reaching outward from a multiply nested class

  • It doesn’t matter how deeply an inner class may be nested—it can transparently access all of the members of all the classes it is nested within.

Why inner classes?

  • Typically, the inner class inherits from a class or implements an interface, and the code in the inner class manipulates the outer-class object that it was created within.
  • You could say that an inner class provides a kind of window into the outer class.
  • What is it that distinguishes an inner class implementing an interface from an outer class implementing the same interface? The answer is that you can’t always have the convenience of interfaces—sometimes you’re working with implementations.
  • Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.
  • One way to look at the inner class is as the rest of the solution of the multiple-inheritance problem.
  • But inner classes effectively allow "multiple implementation inheritance." That is, inner classes effectively allow you to inherit from more than one non-interface.
  • You’ll ordinarily have some kind of guidance from the nature of the problem about whether to use a single class or an inner class.
  • If you have abstract or concrete classes instead of interfaces, you are suddenly limited to using inner classes if your class must somehow implement both of the others.

Closures & callbacks

  • A closure is a callable object that retains information from the scope in which it was created.
  • An inner class is an object-oriented closure, it has permission to manipulate all the members, even private ones.
  • With a callback, some other object is given a piece of information that allows it to call back into the originating object at some later point.
  • If a callback is implemented using a pointer, however, you must rely on the programmer to behave properly and not misuse the pointer.
  • The closure provided by the inner class is a good solution—more flexible and far safer than a pointer.
  • When you create an inner class, you do not add to or modify the interface of the outer class.
  • The value of the callback is in its flexibility; you can dynamically decide what methods will be called at run time.

Inner classes & control frameworks

  • To apply an application framework, you typically inherit from one or more classes and override some of the methods.
  • A common problem in application programming is the graphical user interface (GUI), which is almost entirely event-driven.
  • Inner classes allow you to have multiple derived versions of the same base class, Event, within a single class.
  • Notice how inner classes almost look like multiple inheritance.
  • It’s more flexible to read the events from a file instead of hardcoding them.
  • You’ll see how elegantly inner classes are used to describe the actions of a graphical user interface.

Inheriting from inner classes

  • The problem is that the "secret" reference to the enclosing class object must be initialized, and yet in the derived class there’s no longer a default object to attach to.
  • You must use a special syntax to make the association explicit.

Can inner classes be overridden?

  • "overriding" an inner class as if it were another method of the outer class doesn’t really do anything.
  • The two inner classes are completely separate entities, each in its own namespace.

Local inner classes

  • A local inner class cannot have an access specifier because it isn’t part of the outer class, but it does have access to the final variables in the current code block and all the members of the enclosing class.
  • Local inner class can have a constructor, Anonymous inner class cannot have a named constructor, only an instance initializer.
  • Since the name of the local inner class is not accessible outside the method, the only justification for using a local inner class instead of an anonymous inner class is if you need a named constructor and/or an overloaded constructor, since an anonymous inner class can only use instance initialization.

Inner-class identifiers

  • Every class produces a .class file that holds all the information about how to create objects of this type (this information produces a "meta-class" called the Class object).
  • If inner classes are anonymous, the compiler simply starts generating numbers as inner-class identifiers.
    

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

-Advertisement-
Play Games
更多相關文章
  • 一些迴圈代碼,有時候要知道頁面執行的時間,可以添加以下幾行代碼到頁面頭部和尾部: 頭部: 尾部: 最後輸出: ...
  • 有感於很多新人都不知道怎麼學習軟體開發,個人感覺還是因為練習做的太少,軟體開發知識想看懂太難了,必須是邊讀資料邊動手練習。莫說是新人,Java老人研究新技術的時候也是邊讀資料邊練習。因此整理和編排了一系列的練習題,原發表於技術幫網站,但畢竟博客園更大,特轉摘於此,希望對新人學習有幫助。 1 請設計部 ...
  • 受到Unix時間戳的啟發,我發現時間轉成秒數後會非常好處理,在程式當中不再是以字元串的形式處理,不管時間的加減還是獲取隨機的時間點都變得非常方便, 如果有需要,也很容易轉換成需要的時間格式。 一:時間轉成秒數 stackoverflow.com上還有更多的寫法,有興趣可以自己去看。當然方法一最簡單明 ...
  • (譯者註:本人目前在杭州某家互聯網公司工作,崗位是測試研發,非常喜歡python,目前已經使用Django為公司內部搭建了幾個自動化平臺,因為沒人教沒人帶,基本靠野路子自學,走過好多彎路,磕磕碰碰一路過來,前段時間偶爾看到《Django By Example》這本書,瞬間淚流滿面,當初怎麼沒有找到這 ...
  • 屬實C++不會。 目前幫朋友弄個小項目需要小折騰一下。 c# 一直採用 log4net ,c++的呢,找找有個log4cplus 知識有限,做個通用類吧。別把精力放在這裡。 動手創建個靜態類。 為了保持一致性,由於好幾年前還有一些BCL的動態庫。 所以命名規則還採用原來的風格。 BCLLogHelp ...
  • (本文圖片量非常大,網速不好的話,載入可能比較慢,手機黨慎入!!!)IntelliJ在業界被公認為最好的java開發工具之一,尤其在智能代碼助手、代碼自動提示、重構、J2EE支持、Ant、JUnit、CVS整合、代碼審查、 創新的GUI設計等方面的功能可以說是超常的。IDEA是JetBrains公司... ...
  • 程式講究拆解和構造,根據需求拆解實現的步驟,一個步驟一個步驟的來實現,不斷測試,碰到問題可以百度查詢(順便提升一下自己的搜索技術[需求越清楚,搜索的耗時也越少])和交流。 ...
  • 首先,我想說,雖然我是個學java的。。。 其實我的java也很爛。但是我覺得python挺好玩,想學習下。之前用python做了簡單的爬蟲,也算是對python入門了。不過自己要學的地方還有很多。 我學習的資料都是網上找的,阮一峰老師的教程和菜鳥教程,都很不錯。剛開始學習,沒有必要買厚厚一本書去看 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...