Java程序設(shè)計(jì)英文版課件:ch10 Inner classes_第1頁
Java程序設(shè)計(jì)英文版課件:ch10 Inner classes_第2頁
Java程序設(shè)計(jì)英文版課件:ch10 Inner classes_第3頁
Java程序設(shè)計(jì)英文版課件:ch10 Inner classes_第4頁
Java程序設(shè)計(jì)英文版課件:ch10 Inner classes_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論