Thinking in Java 3 Edition }///:~ 仅就ship()而言,内部类的用法同其它类没什么两样。实际上唯一的区 别就是,类的名字被嵌套在 Parcel1里面了。不过,过一会你就会看 到,这并不是唯一的区别。 比较常见的,还是让宿主类提供一个会返回内部类的 reference的方 法,就像这样 // c08: Parcel2 / Returning a reference to an inner class public class Parcel2 class Contents private int i =1li public int value()i return i; class Destination I private String label Destination(String whereTo) string rn labeli public Destination to(String s) public Contents cont()t return new Contents()i public void ship(string dest) t cont (i Destination d =to(dest)i System. out. println(d. readLabel())i public static void main(String[] args)t Parcel2 p= new Parcel2()i p ship("Tanzania")i Parcel2 q= new Parcel2()i / Defining references to inner classes: Parcel2 Contents c= g cont ( Parcel2 Destination d =g to(" Borneo")i }/// 除非是在“宿主类( outer class)”的非 static方法里面,否则无论你在 那里创建内部类的对象,都必须用 OuterClassName. Inner| assName 的形式来表示这个对象的类型,就像main()里面那样 内部类与上传 16页共47页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
Thinking in Java 3 rd Edition www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com ✄ 16 ☎ ✆ 47 ☎ } ///:~ < sh ip( )à(§AZe¦ñ¢J+3( ¨(ѾvF Parcel 1 ˶,X^^3º¡º< g X+3(¨ 8 · (ÇPQ3׺ef( reference (µ } ¦L //: c08:Parcel2.java // Returning a reference to an inner class. public class Parcel2 { class Contents { private int i = 11; public int value() { return i; } } class Destination { private String label; Destination(String whereTo) { label = whereTo; } String readLabel() { return label; } } public Destination to(String s) { return new Destination(s); } public Contents cont() { return new Contents(); } public void ship(String dest) { Contents c = cont(); Destination d = to(dest); System.out.println(d.readLabel()); } public static void main(String[] args) { Parcel2 p = new Parcel2(); p.ship("Tanzania"); Parcel2 q = new Parcel2(); // Defining references to inner classes: Parcel2.Contents c = q.cont(); Parcel2.Destination d = q.to("Borneo"); } } ///:~ ¯FÍPQ(ou ter cl ass)Ï(¯ static µË¶Ä9Ü ¡F YË12(¥!Æ OuterClassName.InnerClassName (@´Q? ×¥!(h} main ( )˶Y¦
Thinking in Java 3 Edition 到目前为止,内部类还没有表现出什么非常惊人的特质。毕竟,如果你所 追求的只是隐臧机制,那么Java已经有了——只要赋予类 package权 限(只能在 package内部访问)就行了,何必要把它做成内部类。 但是,当你将它上传到基类,特别是 interface的时候,就会发现,内 部类还是有它自己的特性的。(实际上,将对象上传给它所实现的接口与 将它上传给基类是完全相同。)这样,任何人都不能看到或者访问到内部 类了——也就是 interface的实现了,于是“隐藏实现”就变得轻而易 举了。你所得到的,只是一个基类或 interface的 reference。 首先,要用单独的文件来定义公用接口,这样它们才能“全程使用 //: c08: Destination. java public interface Destination i String readLabel ()i }///:~ public interface Contents int value(i }/// 现在客户程序员能用 Contents和 Destination接口了。(要记住, interface就表示它成员自动就是 public的。) 当你拿到基类或 interface的 reference的时候,有可能你会没办法找 出它的具体类型,就像下面所演示的: //: c08: TestParcel java // Returning a reference to an inner class lass Parcels i private class PContents implements Contents t private int i =1l lue( return o protected class DEstination implements private string labeli private DEstination(string whereTo) t blic strin return⊥abe pub tion dest(St return new DEstination(s)i public Contents cont()t return new PContentsoi 第17页共47页 www.wgqqh.com/shhgs/tij.html
Thinking in Java 3 rd Edition ✄ 17 ☎ ✆ 47 ☎ www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com g«+m?í!Z¯·?ª()@ABÄÅ¡D CD(óIJ"YZ Java ôõm,55ó% package (ó;F package )}, %PAT ¡AJAgB)¨ in terface (Mºìí mAýþ()*((ñ¢J¥!JA¬ADñí( AJA¬Bc>) ¦tªÆX;<g3g ,55[ in terface (ñí,)ÍIJñíÏEàS ,¡Dg(ó3×B3 in terface ( reference ~/%Hï( {QO4K ¦AZ;ÍcâçÏL //: c08:Destination.java public interface Destination { String readLabel(); } ///:~ //: c08:Contents.java public interface Contents { int value(); } ///:~ íFÖ×â%&; Con ten ts Destination ,(%8F in terface ?A&ý pu blic () ¡ÏgB3 in terface ( reference (MmÝ;¡º,3 !A(º»h}¶D(L //: c08:TestParcel.java // Returning a reference to an inner class. class Parcel3 { private class PContents implements Contents { private int i = 11; public int value() { return i; } } protected class PDestination implements Destination { private String label; private PDestination(String whereTo) { label = whereTo; } public String readLabel() { return label; } } public Destination dest(String s) { return new PDestination(s); } public Contents cont() { return new PContents();