




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Chapter 10Inner classes Creating inner classes Link to the outer class Using .this and .new Inner classes and upcasting Inner classes in methods and scopes Anonymous inner classes Nested classes Classes inside interfacesCreating inner classespublic class Parcel1 public class Parcel1 class Contents c
2、lass Contents private int i = 11; public int value() return i; class Destination class Destination private String label;private String label; Destination(String whereTo) label = whereTo; / Using inner classes looks just like / using any other class, within Parcel1: public void ship(String public voi
3、d ship(String destdest) ) Contents c = new Contents();new Contents(); Destination d = new Destination(new Destination(destdest); ); System.out.println(d.readLabel(); :Link to the outer class name-hiding and code organization scheme object of that inner class has a link to the enclosing object have a
4、ccess rights to all the elements (how about private)interface Selector boolean end();Object current();void next();public class Sequence private Object items;private int next = 0;private class SequenceSelector implements Selector private int i = 0;public Object current() return itemsi;public static v
5、oid main(String args) Sequence sequence = new Sequence(10);for (int i = 0; i 10; i+)sequence.add(Integer.toString(i);Selector selector = sequence.selector();selector.next();Using .this and .new .this produce the reference to outer-class. See example DotThis How to get the .this to outer-class? You s
6、hould use .new to make an instance. See example DotNewInner classes and upcastingCome into inner class by starting upcasting to a base class, and in particular to an interface.(Example TestParcel)lInner class,is unseen and unavailable,lhiding the implementationlAll we get back is a reference to the
7、base class or the interface.Inner classes in methods and scopesInner classes can be created within a method or even an arbitrary scope, two reasons for above:limplement an interface,we can create and return a reference.lto solve a complicated problem,we dont want it publicly available.See Example Pa
8、rcel5&Parcel6Anonymous inner classes public Contents cont() return new Contents() private int i = 11; public int value() return i; ; / Semicolon required in this case Contents( ) method combines the creation of the return value with the definition of the class that represents that return valuelc
9、lass is anonymous; lit has no name.Anonymous inner classespublic class Parcel7b class MyContents implements Contents private int i = 11; public int value() return i; public Contents contents() return new MyContents(); public static void main(String args) Parcel7b p = new Parcel7b(); Contents c = p.c
10、ontents(); Extreme Example Parcel8 & Parcel9Nested classesThe inner class is static. A nested class means:(Example Parcel11)lnot need an outer-class object in order to create an object of a nested class.lnot access a non-static outer-class object from an object of a nested class.lNested classes
11、can have static fields.public class Parcel11 private static class ParcelContents implements Contents private int i = 11;public int value() return i; protected static class ParcelDestinationimplements Destination private String label;private ParcelDestination(String whereTo) label = whereTo;public St
12、ring readLabel() return label; / Nested classes can contain other static elements:public static void f() static int x = 10;static class AnotherLevel public static void f() static int x = 10;public static Destination destination(String s) return new ParcelDestination(s);public static Contents content
13、s() return new ParcelContents();public static void main(String args) Contents c = contents();Destination d = destination(Tasmania); /:Classes inside interfaceslAny class you put inside an interface is automatically public and static lclass is static,not violate the rules for interfacespublic interface ClassInInterface void howdy(); class Test implements ClassInInterface public void howdy() System.out.println(Howdy!); public static vo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 高校發(fā)展型資助育人體系構(gòu)建:三全育人視域下的實(shí)踐創(chuàng)新研究
- 風(fēng)電機(jī)組混凝土材料特性與應(yīng)用研究
- 高校預(yù)算管理一體化體系構(gòu)建與實(shí)施路徑研究
- “卻”字句法語義功能研究:歷時(shí)演變視角
- 燃燒節(jié)能技術(shù)課件
- CFD數(shù)值模擬在燒結(jié)礦環(huán)煙罩結(jié)構(gòu)優(yōu)化中的應(yīng)用
- 會(huì)展設(shè)計(jì)師崗位面試問題及答案
- 獸藥檢測員崗位面試問題及答案
- 文檔機(jī)器翻譯-洞察闡釋
- Last-milelast-mile配送效率提升-洞察闡釋
- 2024年輸配電及用電工程職稱評(píng)審題庫-多選、判斷
- 急救車藥品管理制度
- 2024年職業(yè)技能:拍賣師專業(yè)知識(shí)考試題與答案
- 大學(xué)英語4綜合教程課件教學(xué)課件教學(xué)
- 2024秋人教版七年級(jí)上冊單詞表(英譯漢)
- 2024年吉林省長春市中考英語試卷(含答案與解析)
- GB/T 150.4-2024壓力容器第4部分:制造、檢驗(yàn)和驗(yàn)收
- 2023-2024學(xué)年全國小學(xué)二年級(jí)下英語人教版期末考試試卷(含答案解析)
- 第22課 現(xiàn)代科技革命和產(chǎn)業(yè)發(fā)展(課件)-【中職專用】《世界歷史》(高教版2023基礎(chǔ)模塊)
- TDT 1015.2-2024 地籍?dāng)?shù)據(jù)庫 第2部分:自然資源(正式版)
- 2023年山東省藝術(shù)本科(美術(shù)類)第一次投檔分?jǐn)?shù)線
評(píng)論
0/150
提交評(píng)論