Thinking in Java 3rd Edition
a0 1
a1 a2 47 a1
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
2002 7
chinapub ! " # $ % ! & ’ ( ) * +,-,&/ 0 (1 2
3 4 5 (
6 7,8 9 3,) ; < =,> 8 (? @ A B 3 C D E
Bruce EckelFG (H I J K ( L ( M N O P A ! Q
$ $ R S T T U 3
1000V W ( X YZ R S ( @ 4 [ \ ] ^ _‘ a b,c ! Q,d (e f 9 g
7 h i j kl,D m (n o 4 c p q r s t ( u * v w $ x y 1 (Uz { !,(| }
~ / 3 3 c e * e X ( $ ( * u c,> m i
* 3 ; i = ( 1 3 e = X? ¢ £? ¥ Q$ X 3?,
§ currency1 3 ' (, ' (,( D E V %? fi fl ( –? X A F? ·? fl,? … 3
C ¥ ‰ Q$?,` m
Bruce Eckel? (′?,?,1000V W? ˉ? ˙? = G ¨ X D E? m 3 m ˇ( 8? Y — (?
The
genesis of the computer revolution was in a machine,The genesis of our
programming languages thus tends to look like that machine.ˇ 3 w Z | ′,; $ X O
Bruce% ( ˉ 3 K ( — 1 F – J ( 1 (? ; a B,V ` J J? ' ( X X? ‰ (
f o ( M % (r? F V,X ; F K c o ; K? ^ ( X ^ X b,(,? (
Q? g
10? - 7?u ` K mC % k l,N O? 3 C a F K E 3? K 3?
www.wgqqh.com/shhgs/tij.html? ˉ #? (a y 1 ( a? ˉ? X Y v w #,”? (? 1 ( 7 ; ^
! 3 J — (
shhgs
2003 9 8
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a0 2
a1 a2 47 a1
,3 ( * ¥ !(
8 $
m " X ^ # $ ( % & ; ’ ( ! ˉ
u D E,m ) * +,,& A fl? % v w -?,/
F
0 ^,-? A ; ˙? F?1 2 3 3V m O 4 ( 55 !,6 3 T O 4 % 7 A ( 8 QO 4
-? 9 1 2,3 c,!( ; X? (?,< g
X < 3,! (= >? @ · A 3 ; ˙? 3 ¥ !J A g V B (C D A,E
(?V 8 ˇ(F ; f < 9 Q } 3 G H (I J K (" L? P 3 k g
M 3,< g Z G H 55A N O w? P Q
] R O 55 S V,X T U? ; A? ! V W X Y (K? ^ 3 D 4 Z ; P T [ r \ g +,u *
-? ] 3 ^ _ ‘,( a? E P A | }?b (ˇ
A ; ˙,& O 4 (@ · L cd? e f g ( h i m (j
[ E k l cm & A n 0
( @ · X o?
(? p?D m q? r (? v w s? ˇ t % % g,
N O A m Y u,<
,v 1 O 4 u 4 (?w x
ˇ m? y ¥ !( z ( -? Q1 {
% | 1 2
% P -? | },~ 3 E F
- J m F {?(
Z E 3 P A,6? A? A ; ) 3
,% 1 2 3?,
3 (
-? A (? p? % #? q s Z
Thinking in Java 3rd Edition
a3 3
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
r F % #? q A? y 1 (r ˇ u ‰ A 8 Z e? E? (,( L
E
< ! 3?,A,3 E ` 8 (,
E P
( $ ( S X ' A [
( D E? 3 ( M? P
( O 4 (? ˉ? X Z T YA,
( ^ 8 (,g
(? D X ( E (
* < g 3 C F
$ ‰ (? m v $
( A? (L
//,c08:music5:Music5.java
// Interfaces,
package c08.music5;
import com.bruceeckel.simpletest.*;
import c07.music.Note;
interface Instrument {
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 4
a4 a5 47 a4
// Compile-time constant,
int I = 5; // static & final
// Cannot have method definitions,
void play(Note n); // Automatically public
String what();
void adjust();
}
class Wind implements Instrument {
public void play(Note n) {
System.out.println("Wind.play() " + n);
}
public String what() { return "Wind"; }
public void adjust() {}
}
class Percussion implements Instrument {
public void play(Note n) {
System.out.println("Percussion.play() " + n);
}
public String what() { return "Percussion"; }
public void adjust() {}
}
class Stringed implements Instrument {
public void play(Note n) {
System.out.println("Stringed.play() " + n);
}
public String what() { return "Stringed"; }
public void adjust() {}
}
class Brass extends Wind {
public void play(Note n) {
System.out.println("Brass.play() " + n);
}
public void adjust() {
System.out.println("Brass.adjust()");
}
}
class Woodwind extends Wind {
public void play(Note n) {
System.out.println("Woodwind.play() " + n);
}
public String what() { return "Woodwind"; }
}
public class Music5 {
private static Test monitor = new Test();
// Doesn't care about type,so new types
// added to the system still work right,
static void tune(Instrument i) {
//,.,
i.play(Note.MIDDLE_C);
}
static void tuneAll(Instrument[] e) {
for(int i = 0; i < e.length; i++)
tune(e[i]);
}
Thinking in Java 3rd Edition
a3 5
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
public static void main(String[] args) {
// Upcasting during addition to the array,
Instrument[] orchestra = {
new Wind(),
new Percussion(),
new Stringed(),
new Brass(),
new Woodwind()
};
tuneAll(orchestra);
monitor.expect(new String[] {
"Wind.play() Middle C",
"Percussion.play() Middle C",
"Stringed.play() Middle C",
"Brass.play() Middle C",
"Woodwind.play() Middle C"
});
}
} ///:~
§ A K (y 1 · X P A J A g 3?
(? ˇ 3?
( 3?
( A (y 1 ·? 3? (? ¢ J? ;
Q£? g h? ˇ
3 A (? LA ˙ % & ¥?% FY § currency1 J ¥ !(1 2
X < < 3? b ˇ( A m ' 3,(? ( 7 )
X? t ˇ55[ $ 55 X,m? fi
u 4 (fl 3 C fl? % m?,– g?
ˇ(· F
*? V ( fl F3 9 ˇ(} v 1?V 8
ˇ 7 ) m A (?,? Q V X? ˇ(?; ˙? 1? ( · m 3 E m
( M X,m?,L
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 6
a4 a5 47 a4
X _? 3 O % 8?”? ˇ( X?
(? ; 8 3 fl ( D m currency1(B … ‰
P D m (? k F
- P A? E ; m %? t? V
[ E J A ` t 3
D %,? 3,? ( i
9 Q 1 2 3 ′ ( L
//,c08:Adventure.java
// Multiple interfaces,
interface CanFight {
void fight();
}
interface CanSwim {
void swim();
}
interface CanFly {
void fly();
}
class ActionCharacter {
public void fight() {}
}
class Hero extends ActionCharacter
implements CanFight,CanSwim,CanFly {
public void swim() {}
public void fly() {}
}
public class Adventure {
public static void t(CanFight x) { x.fight(); }
public static void u(CanSwim x) { x.swim(); }
public static void v(CanFly x) { x.fly(); }
public static void w(ActionCharacter x)
{ x.fight(); }
public static void main(String[] args) {
Hero h = new Hero();
t(h); // Treat it as a CanFight
u(h); // Treat it as a CanSwim
Thinking in Java 3rd Edition
a3 7
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
v(h); // Treat it as a CanFly
w(h); // Treat it as an ActionCharacter
}
} ///:~
E < g
,”? ( E?
·
( M k F? Z
9?,ˉ
( A () ˙
( c > m (O 4
(¨ 9? (? E 8 A J,$ g 8 Q(
ˉ? | 1 2 3 ′ h (¥ ! YZ h? D m O 4
m $ (
(O 4,D E ; ; 1 2 ¥ !,
! m? ˇ 1 c( 1 2 ! Q( ¥ ! E v A? § * t 3 [ $ A v — currency1 J A?,
^ X % % & ) currency1 (K 3 % F ) F
* (+,J % # (z j? (L; J A g V B h
( ¢ 7? ¢ J?P T B ˇ c > (L % + % & 1 2 (¥ ! 3 ˇ? Q,3? Lg h
,? (?,?
( % B (+, E Xk & (O 4? v w V /
¢ J? ˉ? N O? ;,B (? v w V / P A T
m FX X O 4 3 & (·? Z ; P A
3 ; m %
V ( M ;,– g 3? FJ *
m 3 3 3? (!? m? A ( 3? ˉ X 3 L
//,c08:InterfaceCollision.java
interface I1 { void f(); }
interface I2 { int f(int i); }
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 8
a4 a5 47 a4
interface I3 { int f(); }
class C { public int f() { return 1; } }
class C2 implements I1,I2 {
public void f() {}
public int f(int i) { return 1; } // overloaded
}
class C3 extends C implements I2 {
public int f(int i) { return 1; } // overloaded
}
class C4 extends C implements I3 {
// Identical,no problem,
public int f() { return 1; }
}
// Methods differ only by return type,
//! class C5 extends C implements I1 {}
//! interface I4 extends I1,I3 {} ///:~
U? % )? a? (X @ – EX ; < ^ e f
g Q currency1 ( ˇ? ˉ P b e } (?,!? ( L
!
"
# $? F% ( k J? [,? % ( * D
E currency1 Z T
E 8 o
′ ( [ E 8 P V
3 ′ ( F e ·? D g (? 3 ′ (
} L
//,c08:HorrorShow.java
// Extending an interface with inheritance,
interface Monster {
void menace();
}
interface DangerousMonster extends Monster {
void destroy();
}
Thinking in Java 3rd Edition
a3 9
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
interface Lethal {
void kill();
}
class DragonZilla implements DangerousMonster {
public void menace() {}
public void destroy() {}
}
interface Vampire extends DangerousMonster,Lethal {
void drinkBlood();
}
class VeryBadVampire implements Vampire {
public void menace() {}
public void destroy() {}
public void kill() {}
public void drinkBlood() {}
}
public class HorrorShow {
static void u(Monster b) { b.menace(); }
static void v(DangerousMonster d) {
d.menace();
d.destroy();
}
static void w(Lethal l) { l.kill(); }
public static void main(String[] args) {
DangerousMonster barney = new DragonZilla();
u(barney);
v(barney);
Vampire vlad = new VeryBadVampire();
u(vlad);
v(vlad);
w(vlad);
}
} ///:~
" # ¥ # T,3 C 0 3 ′ (
" $ 9?,
% (? 8 ˇD m (? ·; ) 7 ) 3 E 7 V 1 2 ′ (
( M E Q? § V?B
ˇ j D < g (
( % 7
7 )
(cm & (
3 fl? ( 1 2 3? g (y,C
( > E? L
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 10
a4 a5 47 a4
//,c08:Months.java
// Using interfaces to create groups of constants,
package c08;
public interface Months {
int
JANUARY = 1,FEBRUARY = 2,MARCH = 3,
APRIL = 4,MAY = 5,JUNE = 6,JULY = 7,
AUGUST = 8,SEPTEMBER = 9,OCTOBER = 10,
NOVEMBER = 11,DECEMBER = 12;
} ///:~
3
( c ′ 3( H?
Q ] } f ‘ (
E } ¥? currency1 (
Y? & ’ ( ) 3
& ’ ( # P A? ] Q? ; F (‰?
# ( * +,-,u ( · Q?,? g ( 3
A m } ( Y? ( h c
( C % 8 F % c % V? v? c ˇ? K ( fl? U
ˉ % ‰ ( h c E }? 1 2 3 L
a6 a7 a7 a8
//,c08:Month.java
// A more robust enumeration system,
package c08;
import com.bruceeckel.simpletest.*;
public final class Month {
private static Test monitor = new Test();
private String name;
private Month(String nm) { name = nm; }
public String toString() { return name; }
public static final Month
JAN = new Month("January"),
FEB = new Month("February"),
MAR = new Month("March"),
APR = new Month("April"),
MAY = new Month("May"),
JUN = new Month("June"),
JUL = new Month("July"),
AUG = new Month("August"),
SEP = new Month("September"),
OCT = new Month("October"),
NOV = new Month("November"),
DEC = new Month("December");
public static final Month[] month = {
JAN,FEB,MAR,APR,MAY,JUN,
JUL,AUG,SEP,OCT,NOV,DEC
};
Thinking in Java 3rd Edition
a3 11
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
public static final Month number(int ord) {
return month[ord - 1];
}
public static void main(String[] args) {
Month m = Month.JAN;
System.out.println(m);
m = Month.number(12);
System.out.println(m);
System.out.println(m == Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
monitor.expect(new String[] {
"January",
"December",
"true",
"true",
"April"
});
}
} ///:~
# 3? ! c(? [ X ; 8A 3 1 2 A (? D m 7 1 2 (
(L* + / # - - - ¥ ![ v ) c 3? (
# ¥ !(c? E A 3 c?
E ¥ > v? (# ¥ ! E< ! Z T h c (
3 # (¥ ! D E A ; v
# ` 3 * # ( 0 ! D e f ( 3 (g ;, (
J § A (g X fl? c
j? F
(b i } D < g ( ; | r 1 1
2 u D E ; Z T # (? g ; m 3? g
,0 g 3 ′ (O 4 ( 1 2 (¥!; ] } > 8

0 ! ( ( m 3 ( &
( x? k l,3 1 2 h (y” A (F ; J > E 9 Q YZ
(? ˇ k
( ( ( ( x m V currency1( ;,fl? m (
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 12
a4 a5 47 a4
D O 4 (cm? ( A X ;? (
cm & ˇ E fl? ( · Q¥ A ] } f ‘? L
//,c08:RandVals.java
// Initializing interface fields with
// non-constant initializers,
import java.util.*;
public interface RandVals {
Random rand = new Random();
int randomInt = rand.nextInt(10);
long randomLong = rand.nextLong() * 10;
float randomFloat = rand.nextLong() * 10;
double randomDouble = rand.nextDouble() * 10;
} ///:~
7 ) cm &?
( ( M [ 3 currency1 cm & ( M ] } f ‘? 3 G H ( L
//,c08:TestRandVals.java
import com.bruceeckel.simpletest.*;
public class TestRandVals {
private static Test monitor = new Test();
public static void main(String[] args) {
System.out.println(RandVals.randomInt);
System.out.println(RandVals.randomLong);
System.out.println(RandVals.randomFloat);
System.out.println(RandVals.randomDouble);
monitor.expect(new String[] {
"%% -?\\d+",
"%% -?\\d+",
"%% -?\\d\\.\\d+E?-?\\d+",
"%% -?\\d\\.\\d+E?-?\\d+"
});
}
} ///:~
cm? X ( > A F (
E F? [ E F
a6 a7 a9a10a8 3 C,Vfl? m () C L
//,c08:nesting:NestingInterfaces.java
package c08.nesting;
Thinking in Java 3rd Edition
a3 13
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
class A {
interface B {
void f();
}
public class BImp implements B {
public void f() {}
}
private class BImp2 implements B {
public void f() {}
}
public interface C {
void f();
}
class CImp implements C {
public void f() {}
}
private class CImp2 implements C {
public void f() {}
}
private interface D {
void f();
}
private class DImp implements D {
public void f() {}
}
public class DImp2 implements D {
public void f() {}
}
public D getD() { return new DImp2(); }
private D dRef;
public void receiveD(D d) {
dRef = d;
dRef.f();
}
}
interface E {
interface G {
void f();
}
// Redundant "public",
public interface H {
void f();
}
void g();
// Cannot be private within an interface,
//! private interface I {}
}
public class NestingInterfaces {
public class BImp implements A.B {
public void f() {}
}
class CImp implements A.C {
public void f() {}
}
// Cannot implement a private interface except
// within that interface's defining class,
//! class DImp implements A.D {
//! public void f() {}
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 14
a4 a5 47 a4
//! }
class EImp implements E {
public void g() {}
}
class EGImp implements E.G {
public void f() {}
}
class EImp2 implements E {
public void g() {}
class EG implements E.G {
public void f() {}
}
}
public static void main(String[] args) {
A a = new A();
// Can't access A.D,
//! A.D ad = a.getD();
// Doesn't return anything but A.D,
//! A.DImp2 di2 = a.getD();
// Cannot access a member of the interface,
//! a.getD().f();
// Only another A can do anything with getD(),
A a2 = new A();
a2.receiveD(a.getD());
}
} ///:~
F? ( 8 $ ! ( ~ v F?( 3?
A [ E 3 ( E < g
(? v? " `
! (? ˇ j?
( " D ( 1 3 ′ ( # [ E ! (? )?
ˇ [ )?
ˇ YZ ! ( m Z ;?,A ; v?
! ( } " Y?
( " 3 # A [ ; v? (
( " 3 ; v 1 A ( h Q? X ; $ % t?A?,3
! ˇ( !,3?_? O 4 Y ( X ˙? t h
[ $ X J A
ˇ(C D,
",! ( &,currency1 ( ’ LA 3 i,e f 3
! ( e f g w Z E < g
1,i currency1 ( ) e f g? 6 *,+ 3 } (,P e f g R? 3 m A (¥ !55 ^
( ! "
Thinking in Java 3rd Edition
a3 15
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
/,[ E > X ^ (¨ 9 - m 55) currency1 (D m … ‰?
(,F * ( [?
( A X ; ! (
+,? (,) currency1 %? (? ( M X 3 O %? F (?
! ; FO 4 A (
f < 9 Q ) * }? (3 / * ˇ0 1 ( ; m (
2 3? ¢ £,3 ) *,3 g \ A (r
F3?O 4 M 3 c E ( v?
ˇ 3 fl? m 4 g () * A ; ˙? F5 6 J > 7 ( 9 Q? F ( 8 9 X ( 3 C fl? %
j F0,i X X ; Z % m A - g 3 < (= >
- ' ( 4 u?,< g 3?; #? m Z ˇ(,
1 2 (? | } ( c > 55 (O 4 k g A (P Q
L
//,c08:Parcel1.java
// Creating inner classes,
public class Parcel1 {
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; }
}
// Using inner classes looks just like
// using any other class,within Parcel1,
public void ship(String dest) {
Contents c = new Contents();
Destination d = new Destination(dest);
System.out.println(d.readLabel());
}
public static void main(String[] args) {
Parcel1 p = new Parcel1();
p.ship("Tanzania");
}
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 16
a4 a5 47 a4
} ///:~
<
( § A Z e ¢ J + 3 ( currency1 ( v F
4 5,X ^ ^ 3,?,<g X + 3 ( currency1
8? ( ˙ P Q 3,e f (
( }? 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");
}
} ///:~
fl F?P Q
ˇ(fl 9? FY?1 2 (¥ !?
(@ · Q? ¥ !( h }
Y?
Thinking in Java 3rd Edition
a3 17
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
g? + m? ! Z fl,() @ A B? ˉ? DC D ( I J " YZ
m,55 %; F },% P A T? A J A g B ) currency1
( M,m A () * (
¢ J ¥ !J A? A D? ( A J A? B c >
t,? X ; < g 3 g,55[
(?,)?I J? ˇ E S,? D g ( 3 B 3
( ~ / % H ( { QO 4 K? A Z ;?c ˇL
//,c08:Destination.java
public interface Destination {
String readLabel();
} ///:~
//,c08:Contents.java
public interface Contents {
int value();
} ///:~
F % & ;
",% 8 F
A & (? ˇ g B 3
( ( M m ;?,,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();
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 18
a4 a5 47 a4
}
}
public class TestParcel {
public static void main(String[] args) {
Parcel3 p = new Parcel3();
Contents c = p.cont();
Destination d = p.dest("Tanzania");
// Illegal -- can't access private class,
//! Parcel3.PContents pc = p.new PContents();
}
} ///:~
F?
FM 3 Z ; ˙,< g
4 FG H *? ( ˉ
4 6,C ′ L 4 ! ( D E,
4 6 X ; A 4 ",
4 6 7 ( [,? k
E? 4 6 ( 8 X ; 4 " $ % & ¥ & (,£ m (? ¢ J? "` X ; ¥ ! A?
! ( 3 ( fl? 8,
j? 7 4 D (? ; X ; 7?
! ( ˇ (+,,3?; I h J K,? h Q? D? 9 (— L *?
c? M < I J 9 Qˇ( ‰ % & (N z Q<
[ O? 4 ( G ;
E ‰ ( [?,? 3 V ‘ K (",
fl X ; v O 4 ! 3 ( A ;
3 8 (
P d + < g,(Q % D? ( Q? g
g ( %? G H S = (? R? (ˇ (+,> S ( m V currency1 ( X,D N ( ¥? E v 1 2 F " ` t? 3 1 T? Z T m e ¢
7 L
}? D $ (? F Z ; 1 2? e f (
j F ¢ 3 (? % 1 2 3 X | ˙ ′U? N O m Z 3
Thinking in Java 3rd Edition
a3 19
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
F Q( *, D ' ( % L
1,F ( O 4 3
2,F ( 1 T?O 4 3
3,3?,(V
4,3 8,? m,flW cˇ( (V
5,3 ] } cm & f ‘ (V
3 ^? f ‘ (V X ; m c)Q] } 2 (V
3 m,, ( [ § 8 (X? ˇL
//,c08:Wrapping.java
public class Wrapping {
private int i;
public Wrapping(int x) { i = x; }
public int value() { return i; }
} ///:~
,? g
m 3? c( c,˙ · m 3 C
3,F (1 T?
X M 3 (1 T? 1 23? ( v 1? r
ˇL
//,c08:Parcel4.java
// Nesting a class within a method,
public class Parcel4 {
public Destination dest(String s) {
class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
return new PDestination(s);
}
public static void main(String[] args) {
Parcel4 p = new Parcel4();
Destination d = p.dest("Tanzania");
}
} ///:~
4 " X 4 9 ( > A (
‰ m 3 C g? F 3? Y Em 3
4 " Z 1 X,m Z [ (?
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 20
a4 a5 47 a4
!,? [ X ; 4 "? J A 0 Fe f 55
\ ! 3 " ( A
4 " (B P 4 " v k F X? ],
D e f (4 " X 3 m (¥!
(,? Ft? 3 1 T? L
//,c08:Parcel5.java
// Nesting a class within a scope,
public class Parcel5 {
private void internalTracking(boolean b) {
if(b) {
class TrackingSlip {
private String id;
TrackingSlip(String s) {
id = s;
}
String getSlip() { return id; }
}
TrackingSlip ts = new TrackingSlip("slip");
String s = ts.getSlip();
}
// Can't use it here! Out of scope,
//! TrackingSlip ts = new TrackingSlip("x");
}
public void track() { internalTracking(true); }
public static void main(String[] args) {
Parcel5 p = new Parcel5();
p.track();
}
} ///:~
7 8 v F X? ],(1 2 m,{ (55A,currency1 ( 3 9? ( ^ _ < ) O4 A (Y 1 T u ‰ A ( Z e?
D % < J m C ‘ L
//,c08:Parcel6.java
// A method that returns an anonymous inner class,
public class Parcel6 {
public Contents cont() {
return new Contents() {
private int i = 11;
public int value() { return i; }
}; // Semicolon required in this case
}
Thinking in Java 3rd Edition
a3 21
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
public static void main(String[] args) {
Parcel6 p = new Parcel6();
Contents c = p.cont();
}
} ///:~
e f g (1 2 e f g (ˇ (O 4 fl F,3 9 ‰ V ( A m a b ( < J? 1 2( 3
¥ !L
return new Contents()
-? < g ( M?,$?c d X e ^,(O
4 ˇL
return new Contents() {
private int i = 11;
public int value() { return i; }
};
‘ ( D % (? p L?1 2 3 8
(V (¥ !ˇ
D e f (,(J A g
¢ J D V? K (G ‘ @ · L
class MyContents implements Contents {
private int i = 11;
public int value() { return i; }
}
return new MyContents();
V ^ W cQ1 2
(? D %,? ˉ?B D ( 3? c( cˇ( Y w ZT L
//,c08:Parcel7.java
// An anonymous inner class that calls
// the base-class constructor,
public class Parcel7 {
public Wrapping wrap(int x) {
// Base constructor call,
return new Wrapping(x) { // Pass constructor
argument,
public int value() {
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 22
a4 a5 47 a4
return super.value() * 47;
}
}; // Semicolon required
}
public static void main(String[] args) {
Parcel7 p = new Parcel7();
Wrapping w = p.wrap(10);
}
} ///:~
f ( cA? B ( c }?
A?
V b ( X? (j (fl g
C X } > A (? p k l V ( · fl g,(1 A F§ A r (1 c >
[ E FO 4 V (cm & ( M ] } f ‘ L
//,c08:Parcel8.java
// An anonymous inner class that performs
// initialization,A briefer version of Parcel4.java,
public class Parcel8 {
// Argument must be final to use inside
// anonymous inner class,
public Destination dest(final String dest) {
return new Destination() {
private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel8 p = new Parcel8();
Destination d = p.dest("Tanzania");
}
} ///:~
ˉ? FO 4 V ( M % g ‰? (¥ ! Y?,% D
P c( $ ( } ( cY ˉ? h,? ( M,ˉ?
ˉ? | ¥ cm & ] } g YZ T X? (? ˉ
| ] } 3? E cD ] } (ˇi 1 Y w Z, X ;FV?1 2 c
A ; d m, f ‘
ˇ? ; F? J 1 2 3 V ( c }? L
Thinking in Java 3rd Edition
a3 23
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
//,c08:AnonymousConstructor.java
// Creating a constructor for an anonymous inner
class,
import com.bruceeckel.simpletest.*;
abstract class Base {
public Base(int i) {
System.out.println("Base constructor,i = " + i);
}
public abstract void f();
}
public class AnonymousConstructor {
private static Test monitor = new Test();
public static Base getBase(int i) {
return new Base(i) {
{
System.out.println("Inside instance
initializer");
}
public void f() {
System.out.println("In anonymous f()");
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();
monitor.expect(new String[] {
"Base constructor,i = 47",
"Inside instance initializer",
"In anonymous f()"
});
}
} ///:~
F ·
3 O (,v A? V (B ( c V K X,A (
f ‘ Q ' 3?
ˇ ( c
( V % g A
//,c08:Parcel9.java
// Using "instance initialization" to perform
// construction on an anonymous inner class,
import com.bruceeckel.simpletest.*;
public class Parcel9 {
private static Test monitor = new Test();
public Destination
dest(final String dest,final float price) {
return new Destination() {
private int cost;
// Instance initialization for each object,
{
cost = Math.round(price);
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 24
a4 a5 47 a4
if(cost > 100)
System.out.println("Over budget!");
}
private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel9 p = new Parcel9();
Destination d = p.dest("Tanzania",101.395F);
monitor.expect(new String[] {
"Over budget!"
});
}
} ///:~
F f ‘ ˇ? E < g 3 X E?¥ cm & ] } f ‘
(K ˇ(p? g j } (K [ D E? ¢ J? f ‘ ^ V ( c A (F ; m (? X ;? f ‘? ; m 3 c
! ! ! !
' g? 3 I J K ( · $ [ m
( g k?,(r ^ m 3? ˉ? 12,3 YZ ((¥ ! 1 2 A (?P Q (¥!
ˇl 0,? A ; P Q ¥ !( &,55X % t ) currency1 (m ‰ ; P Q (D m… ‰
a11 a12 a13 a14? (,3 C L
//,c08:Sequence.java
// Holds a sequence of Objects,
import com.bruceeckel.simpletest.*;
interface Selector {
boolean end();
Object current();
void next();
}
public class Sequence {
private static Test monitor = new Test();
private Object[] objects;
private int next = 0;
public Sequence(int size) { objects = new
Object[size]; }
public void add(Object x) {
if(next < objects.length)
objects[next++] = x;
}
private class SSelector implements Selector {
private int i = 0;
Thinking in Java 3rd Edition
a3 25
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
public boolean end() { return i ==
objects.length; }
public Object current() { return objects[i]; }
public void next() { if(i < objects.length)
i++; }
}
public Selector getSelector() { return new
SSelector(); }
public static void main(String[] args) {
Sequence sequence = new Sequence(10);
for(int i = 0; i < 10; i++)
sequence.add(Integer.toString(i));
Selector selector = sequence.getSelector();
while(!selector.end()) {
System.out.println(selector.current());
selector.next();
}
monitor.expect(new String[] {
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9"
});
}
} ///:~
2 3? ( n O s z (,0 c? E
o % d (= > ′ (,0? ˉ? m (
‰? E ) Q 2 (¥! k o? £? X g,c = > (
o?p 3
,0 ( E? q g 2 * ( 3
,0 ( 7 ) 3 E A ( Q?
[ E ˇ
1 cE 1 2?r h % ˇ?(
3,F ; ( !? E < g
/ 1 2,3 2 o,3
,` u g,3? A Qp s ¥
2 (¥ ! f < 9 Q
currency1 ( Z e? t M <?,A ( 55
E? 55D ¢(
0 X ( > A? § P Q ( ! & E P Q ( cm & } A u m (3? J? ( Q< >
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 26
a4 a5 47 a4
,? § P Q & ( YZ A Z T g 3 C (v O,m 3 w y?%? x 1 2 A (ˇP Q ¥ !(
P Q ( & ( M,Y I y(
Q¥ & g z { (?,? ] ¢ 3? v w N O ¥ !(1 2 P Q ¥ !m ( 1 2 ¥ !( %? P Q ¥ !(
ˉ? Xg
A ˉ? K ′ V c· ^ % & (|?
ˉ? X %? ¥ ! P Q ¥ !u 4 (ˇ} YZ? E
P O 4 (? v 1? ˇ
a11 a12 a15 a14% | ¢ £
) (? 4? 8 F ( ¥ !? W?A (P Q ¥ ! [ 1 2 A (Y ¥ !(ˇ
$ ( M · X?,(? p L
P Q (¥ ! ; 1 2 (¥ !
X ; F (¥ ! fl (P Q ¥ ! ‰ ( m 3 C X ( & cm; g (‰ _ 3,(?X ; m cm
cm & 3 E m L
//,c08:Parcel10.java
// Nested classes (static inner classes),
public class Parcel10 {
private static class ParcelContents implements
Contents {
private int i = 11;
public int value() { return i; }
}
protected static class ParcelDestination
implements Destination {
private String label;
private ParcelDestination(String whereTo) {
label = whereTo;
}
public String 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 dest(String s) {
return new ParcelDestination(s);
Thinking in Java 3rd Edition
a3 27
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
}
public static Contents cont() {
return new ParcelContents();
}
public static void main(String[] args) {
Contents c = cont();
Destination d = dest("Tanzania");
}
} ///:~
F
4 5 & (¥ ! Z ( >? ( ¥?
& ( Qu,e f
" ( ( j J,< g
fl ( % ) ~ (
Q P Q ¥ ! / } X %
A m > E,? ·
X ; m t K ( i E
(3 7 ) ( X (¨ 9 55 F ( 4?L
//,c08:IInterface.java
// Nested classes inside interfaces,
public interface IInterface {
static class Inner {
int i,j,k;
public Inner() {}
void f() {}
}
} ///:~
F (? \? 2 x 3 u (
T m 3 C? u,‰ K g? ˉ z ( 3? YZ? E Q K L
//,c08:TestBed.java
// Putting test code in a nested class,
public class TestBed {
public TestBed() {}
public void f() { System.out.println("f()"); }
public static class Tester {
public static void main(String[] args) {
TestBed t = new TestBed();
t.f();
}
}
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 28
a4 a5 47 a4
} ///:~
A,H 0 3
7 ;7 ( ( {? ˉ % \ } %? E ]
0 ! 7 ;7? E Q1 ( M E X P A k ] % F] k u P
7 ;7 (? }," # $" # $" # $" # $
ˉ? %? w y P Q ¥ !(
YZ E FP Q (? 3 C `
Q? P Q 8? 2 ( (t? E
2 ( Q? A D (P Q
2 ( A,e f j ( h h N(? ( M,1 p X,? % (\ } ‰ (?
m? % ˙ currency1 (¥ !1 2 A ( (¥ ! % | Z 1? F
· ! P Q ¥ !( }? L
//,c08:Parcel11.java
// Creating instances of inner classes,
public class Parcel11 {
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 static void main(String[] args) {
Parcel11 p = new Parcel11();
// Must use instance of outer class
// to create an instances of the inner class,
Parcel11.Contents c = p.new Contents();
Parcel11.Destination d = p.new
Destination("Tanzania");
}
} ///:~
ˉ | 1 2 ¥ !? X ; | (
4 5 5 Q? PQ ( >? P Q ¥ !Q1 2 (¥ !L
Parcel11.Contents c = p.new Contents();
Thinking in Java 3rd Edition
a3 29
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
fl 1 2,P Q (¥ !? 9 ; X ; 1 2 (¥ ! (¥ !” ( g 1 2 A (P Q ¥ !? ˉ? 1 2 (
( ( Y X % P Q ¥ !(
,% & ’ ( ) * % & ’ ( ) * % & ’ ( ) * % & ’ ( ) *
a11 a12 a16 a14 (,currency1 m V & X Z? 55A E $ $ r A ( § P Q ( & }? L
//,c08:MultiNestingAccess.java
// Nested classes can access all members of all
// levels of the classes they are nested within,
class MNA {
private void f() {}
class A {
private void g() {}
public class B {
void h() {
g();
f();
}
}
}
}
public class MultiNestingAccess {
public static void main(String[] args) {
MNA mna = new MNA();
MNA.A mnaa = mna.new A();
MNA.A.B mnaab = mnaa.new B();
mnaab.h();
}
} ///:~
E < g
# + ( ( t m E u A?
! ( D,? FM 3?1 2 V,( (¥ !( M v?
( ˇ w $,j (1 T? Fu c(?` O (,
+,+,+,+,
7 ) ( c g P Q ¥ !(
J % 8 ( M · m C,U U F?m 3 w yP Q ¥ !(? H (ˇ
% ] } f ‘ F 0 < Q A? m W ¥ !,3 ( Q$ r 2 L
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 30
a4 a5 47 a4
//,c08:InheritInner.java
// Inheriting an inner class,
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~
E < g
8 ( X A (P Q - g % 1 2 c( M W ( c X,? A? G PQ ¥ !(
‰? F c L
enclosingClassReference.super();
Z ; Y (
% [ Z ;? ^ -,/ 0 1 2 3 -,/ 0 1 2 3 -,/ 0 1 2 3 -,/ 0 1 2 3
+? 1 2,3 8,A (P Q? ′ O 4,Y
Y,m Z fl ˉ [ $ X E P I h r? 3? < J } 3 fl? (p | }? P Q ( Y ˇ X,m Z? ¢ ˉ (L
//,c08:BigEgg.java
// An inner class cannot be overriden like a method,
import com.bruceeckel.simpletest.*;
class Egg {
private Yolk y;
protected class Yolk {
public Yolk()
{ System.out.println("Egg.Yolk()"); }
}
public Egg() {
System.out.println("New Egg()");
y = new Yolk();
}
}
public class BigEgg extends Egg {
Thinking in Java 3rd Edition
a3 31
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
private static Test monitor = new Test();
public class Yolk {
public Yolk()
{ System.out.println("BigEgg.Yolk()"); }
}
public static void main(String[] args) {
new BigEgg();
monitor.expect(new String[] {
"New Egg()",
"Egg.Yolk()"
});
}
} ///:~
,0 3 W ( c A,u B (W
c ;?,7 ) 1 2 ( / v w u? (
,8 % ( ! #? fl? #? 8 P Q ( M ( g +
,e > ((e A? m ( 4 % | $ r 8 Y m,(L
//,c08:BigEgg2.java
// Proper inheritance of an inner class,
import com.bruceeckel.simpletest.*;
class Egg2 {
protected class Yolk {
public Yolk()
{ System.out.println("Egg2.Yolk()"); }
public void f()
{ System.out.println("Egg2.Yolk.f()");}
}
private Yolk y = new Yolk();
public Egg2() { System.out.println("New Egg2()"); }
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}
public class BigEgg2 extends Egg2 {
private static Test monitor = new Test();
public class Yolk extends Egg2.Yolk {
public Yolk()
{ System.out.println("BigEgg2.Yolk()"); }
public void f() {
System.out.println("BigEgg2.Yolk.f()");
}
}
public BigEgg2() { insertYolk(new Yolk()); }
public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
monitor.expect(new String[] {
"Egg2.Yolk()",
"New Egg2()",
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 32
a4 a5 47 a4
"Egg2.Yolk()",
"BigEgg2.Yolk()",
"BigEgg2.Yolk.f()"
});
}
} ///:~
F
/ 3 (,8 $ ( A / 3 (,8,A (
,8 ; ˙ / 3 P A (,8 ¥ !J A?
/ 3 (? D u (,? (
/ 3 (,8 ( cu § B c( M
/ 3 (,8 v currency1 u?,< g u,? ^? (
4 5 4 5 4 5 4 5
' ^? [ E FK D (j 1 2
r X ; m? A X 7 ) P Q A? E K D (
E? P Q (D m &? D % ¥ r V 1,3 8 L
//,c08:LocalInnerClass.java
// Holds a sequence of Objects,
import com.bruceeckel.simpletest.*;
interface Counter {
int next();
}
public class LocalInnerClass {
private static Test monitor = new Test();
private int count = 0;
Counter getCounter(final String name) {
// A local inner class,
class LocalCounter implements Counter {
public LocalCounter() {
// Local inner class can have a constructor
System.out.println("LocalCounter()");
}
public int next() {
System.out.print(name); // Access local
final
return count++;
}
}
return new LocalCounter();
}
// The same thing with an anonymous inner class,
Counter getCounter2(final String name) {
return new Counter() {
// Anonymous inner class cannot have a named
// constructor,only an instance initializer,
{
Thinking in Java 3rd Edition
a3 33
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
System.out.println("Counter()");
}
public int next() {
System.out.print(name); // Access local
final
return count++;
}
};
}
public static void main(String[] args) {
LocalInnerClass lic = new LocalInnerClass();
Counter
c1 = lic.getCounter("Local inner "),
c2 = lic.getCounter2("Anonymous inner ");
for(int i = 0; i < 5; i++)
System.out.println(c1.next());
for(int i = 0; i < 5; i++)
System.out.println(c2.next());
monitor.expect(new String[] {
"LocalCounter()",
"Counter()",
"Local inner 0",
"Local inner 1",
"Local inner 2",
"Local inner 3",
"Local inner 4",
"Anonymous inner 5",
"Anonymous inner 6",
"Anonymous inner 7",
"Anonymous inner 8",
"Anonymous inner 9"
});
}
} ///:~
,e f % d * ( 3 g % currency1 r V?,e m,> (} F ; 7 ) r ( F ‰? ( r QK V (+
3 j (¢ 7? % 3 m ( c? !3 % c V ; ] }? f ‘
¥? r X V (+ 3? 1 2 V Y
(¥ ! 6 7 8 6 7 8 6 7 8 6 7 8
7 ),0 3
( { E?w? 1 2 (¥!ˇ(
,0 3 v ¥ !(?
ˇ? ;,| [ v w ; 0 A ( ¥ !( (
( {? { ! ˇ(? m,? (¨
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 34
a4 a5 47 a4
O LP Q ( J ; ` J (?
< ( 0 ! D 1 2 (( { k L
Counter.class
LocalInnerClass$2.class
LocalInnerClass$1LocalCounter.class
LocalInnerClass.class
ˉ V?,c Q? ( ˉ
F YZ A (,~ FP Q? (Y;? ( G H A i fl? E v K ′
V c(· a11 a12 a17 a147 ) ( ¨ ^ 0 ( { R ( (,
,; ˙ A j? \ }?,,Q
F < g,V \ 1 · ( 4,X; f? Z % m ˇ? " % Z % Z r B () *
,8 currency1 ( Z 3?
(K E i 1 2 A (Y P Q? E $ 3 o P Q (?
F? z j ¢ * % £ (? L? ˉ % (
(
Y Z X ˙ P Q j j ˉ? m % Y 3 O ; m,T g ˇYZ?
(? (P Q > 8 m Z currency1 3? Q g, m ; X g
D ( ) b k?,(3 C L
! ! ! !
" # $ % & ’ ( ) * +,- -,/ " # $ % & ’ ( ) * +,- -,/ " # $ % & ’ ( ) * +,- -,/ " # $ % & ’ ( ) * +,- -,/ 0? ¢ J? ˉ m D ( 8 V 3
(;a V +,,fl? U F? < m 3 ¥ C P A 1 I h £N V 8? (,r £
N,? ; ˙? z j T g? 8 V?
ˇ[ $ ; ˙? F? J 8 V fl
(
Thinking in Java 3rd Edition
a3 35
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
,; ',? 3 C +? 3? § F3 e 7 ) 8 ( · 8 currency1 '? m e ¥? L3 H ( 3 L
//,c08:MultiInterfaces.java
// Two ways that a class can implement multiple
interfaces,
interface A {}
interface B {}
class X implements A,B {}
class Y implements A {
B makeB() {
// Anonymous inner class,
return new B() {};
}
}
public class MultiInterfaces {
static void takesA(A a) {}
static void takesB(B b) {}
public static void main(String[] args) {
X x = new X();
Y y = new Y();
takesA(x);
takesA(y);
takesB(x);
takesB(y.makeB());
}
} ///:~
~ / % v O e (p,? X ; ( X ^ 3? Q
$ E @ (^ *? 3 o? E N O w H (3? ˉ m currency1 (,{ <? (N z Q< J e m Z B currency1 A? ; j? y 1
ˉ? fi J (X
3 3 O %? e ( YZ? ;,L
//,c08:MultiImplementation.java
// With concrete or abstract classes,inner
// classes are the only way to produce the effect
// of "multiple implementation inheritance."
package c08;
class D {}
abstract class E {}
class Z extends D {
E makeE() { return new E() {}; }
}
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 36
a4 a5 47 a4
public class MultiImplementation {
static void takesD(D d) {}
static void takesE(E e) {}
public static void main(String[] args) {
Z z = new Z();
takesD(z);
takesE(z.makeE());
}
} ///:~
ˉ? £N?V? ( 8
ˇ? YZ S m? [ c E £N? m,? g,? (fl ) * L
1,E m V E m A ( P Q ¥ ! (
2,3 P Q? E k J i A E X ( · Q? 3
3 8 3 J,m Z 3 (
3,¥ !1 2 ( " P Q ¥ !(1 2 Z
4,X F Z ˙,–? (? ˇ G 3 ( Q$ %
2 ( 0 ! m YZ? ; $?
2 3 ˇ )? 2 ;m 3
F? E r ` O 4 3
- ˙ A e f 3,? ^ Q % d (
m Z ; currency1 ' *
3 ; u (¥ ! A 8 Y,1 2 A (Y 1 T ^ D O 4? ; < ! 3? y ¥ !(
A X <,P Q (D m
1 2 A (1 T ˇ? w y Y P Q ¥ !(
A m i ¥ !(D m & S A
! ( F% D
w · " ( V x * b,k? a 3,% ˙ A; ] } f u
m,f u? ;? currency1 (¥ !3 D F? Q C ˙ A ^ Qu 9 ¥ !,?,(]?,3 fl? #? (p,? ˉ w · Q? f u Y?; w 7 % &,7 G X % w · j D < g ( F? J A m w · a
D (
3? (£N 55A 8 w · currency1 ' [ c?
Thinking in Java 3rd Edition
a3 37
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
//,c08:Callbacks.java
// Using inner classes for callbacks
import com.bruceeckel.simpletest.*;
interface Incrementable {
void increment();
}
// Very simple to just implement the interface,
class Callee1 implements Incrementable {
private int i = 0;
public void increment() {
i++;
System.out.println(i);
}
}
class MyIncrement {
void increment() {
System.out.println("Other operation");
}
static void f(MyIncrement mi) { mi.increment(); }
}
// If your class must implement increment() in
// some other way,you must use an inner class,
class Callee2 extends MyIncrement {
private int i = 0;
private void incr() {
i++;
System.out.println(i);
}
private class Closure implements Incrementable {
public void increment() { incr(); }
}
Incrementable getCallbackReference() {
return new Closure();
}
}
class Caller {
private Incrementable callbackReference;
Caller(Incrementable cbh) { callbackReference =
cbh; }
void go() { callbackReference.increment(); }
}
public class Callbacks {
private static Test monitor = new Test();
public static void main(String[] args) {
Callee1 c1 = new Callee1();
Callee2 c2 = new Callee2();
MyIncrement.f(c2);
Caller caller1 = new Caller(c1);
Caller caller2 = new
Caller(c2.getCallbackReference());
caller1.go();
caller1.go();
caller2.go();
caller2.go();
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 38
a4 a5 47 a4
monitor.expect(new String[] {
"Other operation",
"1",
"2",
"1",
"2"
});
}
} ///:~
D % ] 3 ^,?˙ P Q? ˇ?R?
T ˇu 4 ( currency1? (N z Q$ $ ! 5 G H 3
3 8,# #? k,3 A (
(F ; D O 4 (O X > D E
3 8 # E X;
Q?,”,? ; Q 3 (?,‰ m 3 C g? 1 2 ( M currency1 3 P Q (
,
8 - u ‰ 3?(D m?
! ( fl? % A ‰? … (‰ E?< g
Q(
,E,3 ; e f
3 ( 55 3 c (? g,
(? ; [ ; u
u ‰ Z? | X,X } w · 9 Q?,
( c % 3 (f u
ˇ(} ;,0 Ft M ^ M
Q?f u ˇ f u (4 g F) § currency1 ' *? E F % \ } ( M r ¥? v w u
g,#? C ; < X ;,? $ % & F ; fu,g?
m 3? ¢ ( P A? `
ˇ?v % `
ˇ 3 33 £N )O h (? +,(? ˉ | v % `? · %
Thinking in Java 3rd Edition
a3 39
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
8 § * (3 3V `? E,v % `?,3 £N? (? %? E ; m? () ~ %D O,
3?’ ′ ’ ( ˇ+,’ ·
(
` v % ` * (3 Q % ) v {? ˉ (~ % t 1 ¥ { 1 ! v YZ A v? {?
ˇ @ …? $ % & 1 2 v % % £N (b? C (? u 3 A B X V c 7 4? ( j F
#?< g ( (" ) 3 ` A ^ ˉ? ( fl? ˙ ¨ r £N,
$ % & (U?,< < ` B? Q1 2 ( | } 3
3 ` § t 1 L % { A ˇ A j } Y { A ˇ E m V? p? w 4 Q 3 m k l?g h F Z ˇ( ( `,F?
’ ′ ˇ( M ^ 8? ~ / { ( A X 3
3
W (} ; m 4 Q] } m,3? L
//,c08:controller:Event.java
// The common methods for any control event,
package c08.controller;
public abstract class Event {
private long eventTime;
protected final long delayTime;
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() { // Allows restarting
eventTime = System.currentTimeMillis() +
delayTime;
}
public boolean ready() {
return System.currentTimeMillis() >= eventTime;
}
public abstract void action();
} ///:~
| ˙
/! \ } ( M A ( c”? 4 ¥ !1 2 (Y?
u (t 1 4 J? ! Z M {
m v k t c > A v H d 3? { u? E ` currency1,E ′
/! ¥ ! 8 $? ˉ? | { %? 3
˙ A u },
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 40
a4 a5 47 a4
,#? Z M E \ },0 E?; P /! X B ) 4 (,? D % 3? z 4? ( E ! { `
/! ¥ !v F3 v
< (R ¥ !* % g?Z r 0,
< F? % N O,P,0 g
< (b =,#? < m V? ¥!
E ; m < P … ‰ ! Q
!,; m ! P … ‰ <
//,c08:controller:Controller.java
// With Event,the generic framework for control
systems,
package c08.controller;
import java.util.*;
public class Controller {
// An object from java.util to hold Event objects,
private List eventList = new ArrayList();
public void addEvent(Event c) { eventList.add(c); }
public void run() {
while(eventList.size() > 0) {
for(int i = 0; i < eventList.size(); i++) {
Event e = (Event)eventList.get(i);
if(e.ready()) {
System.out.println(e);
e.action();
eventList.remove(i);
}
}
}
}
} ///:~
,? ! < E ˇ 3? (/! ¥ ! 3 A 3 g 3
(¥ ! A,P A] — ! Q Fu ¥ !(
b ` Fd P
/!? g? +? X N O % ˙
/! | Z +,(% 55? Z ;?,X,( Qˇ 3 ( Q$? ‘ (y
ˇ /! ¥ !(X }? (t 1 1 2
/! ( Q X (} F t?,A ; T e { L
1,F3 r `? P ˇ?(D m 3 c 9 Q,9 Q? £N, D (
Thinking in Java 3rd Edition
a3 41
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
2,E ˙? < J X ` ) ^ ‘ A E P Q (D m & % X? ( K,fl gb? v O,3 3 K (
| |? ` (” A Q (
a18 a19 a20 a21? 1? 9 X L 3 ′
` E fl? G H (? X (K ; ˙? F3? 3 B
/! 0 ! V 8 · ¥? } E 8 3 ′ (
/! P K? ] h (v % ` 3?
>
8 Q(L
//,c08:GreenhouseControls.java
// This produces a specific application of the
// control system,all in a single class,Inner
// classes allow you to encapsulate different
// functionality for each type of event,
import com.bruceeckel.simpletest.*;
import c08.controller.*;
public class GreenhouseControls extends Controller {
private static Test monitor = new Test();
private boolean light = false;
public class LightOn extends Event {
public LightOn(long delayTime)
{ super(delayTime); }
public void action() {
// Put hardware control code here to
// physically turn on the light,
light = true;
}
public String toString() { return "Light is
on"; }
}
public class LightOff extends Event {
public LightOff(long delayTime)
{ super(delayTime); }
public void action() {
// Put hardware control code here to
// physically turn off the light,
light = false;
}
public String toString() { return "Light is
off"; }
}
private boolean water = false;
public class WaterOn extends Event {
public WaterOn(long delayTime)
{ super(delayTime); }
public void action() {
// Put hardware control code here,
water = true;
}
public String toString() {
return "Greenhouse water is on";
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 42
a4 a5 47 a4
}
}
public class WaterOff extends Event {
public WaterOff(long delayTime)
{ super(delayTime); }
public void action() {
// Put hardware control code here,
water = false;
}
public String toString() {
return "Greenhouse water is off";
}
}
private String thermostat = "Day";
public class ThermostatNight extends Event {
public ThermostatNight(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here,
thermostat = "Night";
}
public String toString() {
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event {
public ThermostatDay(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here,
thermostat = "Day";
}
public String toString() {
return "Thermostat on day setting";
}
}
// An example of an action() that inserts a
// new one of itself into the event list,
public class Bell extends Event {
public Bell(long delayTime) { super(delayTime); }
public void action() {
addEvent(new Bell(delayTime));
}
public String toString() { return "Bing!"; }
}
public class Restart extends Event {
private Event[] eventList;
public Restart(long delayTime,Event[] eventList)
{
super(delayTime);
this.eventList = eventList;
for(int i = 0; i < eventList.length; i++)
addEvent(eventList[i]);
}
public void action() {
for(int i = 0; i < eventList.length; i++) {
eventList[i].start(); // Rerun each event
addEvent(eventList[i]);
Thinking in Java 3rd Edition
a3 43
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
}
start(); // Rerun this Event
addEvent(this);
}
public String toString() {
return "Restarting system";
}
}
public class Terminate extends Event {
public Terminate(long delayTime)
{ super(delayTime); }
public void action() { System.exit(0); }
public String toString() { return
"Terminating"; }
}
} ///:~
7 ) P Q
> t ) currency1 ( E cm & ‰ K ′ V c
Q g,3 { (
K ′ V c(
/! < J? } - m ) currency1
^ u,o { d ` k 3 ′ ( ¥ ! A,e currency1? 3? ’ ( V 8 (L
-,?,
/! (D m ‰ A < J,?,P Q
> (D m
- ˇ,3 /! ¥ !(c P A t?
ˇ 7 ) - [ 3 /! ¥ !? E F
- ( 3 - ¥ !? ; O,
^ 1 2
> ¥ ! `
/! ¥ !,(? ! ˇ+,’· (3 L
//,c08:GreenhouseController.java
// Configure and execute the greenhouse system,
// {Args,5000}
import c08.controller.*;
public class GreenhouseController {
public static void main(String[] args) {
GreenhouseControls gc = new GreenhouseControls();
// Instead of hard-wiring,you could parse
// configuration information from a text file
here,
gc.addEvent(gc.new Bell(900));
Event[] eventList = {
gc.new ThermostatNight(0),
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 44
a4 a5 47 a4
gc.new LightOn(200),
gc.new LightOff(400),
gc.new WaterOn(600),
gc.new WaterOff(800),
gc.new ThermostatDay(1400)
};
gc.addEvent(gc.new Restart(2000,eventList));
if(args.length == 1)
gc.addEvent(
gc.new Terminate(Integer.parseInt(args[0])));
gc.run();
}
} ///:~
% ¥ ] } f ‘,D m % ( { (,
X? K { *?([, m 3 O?,% %
ˉ? F },c YZ A,F? ^ O u * + %
Q (? v w ; … g (4 g,? ) currency1 A v )
` ( M g #,< g? v V W r ) @ …? (i 1 ( g,Y? Z z j Z,¥ (4 g c? 0
> 8 K ′ V ccurrency1 (
+ +,( a,8 $
E ( a ‰ A £N,V 8 ( & £N (?
(( & fl? U > 8 u ( 9 G H,V
) * p 8 G H ( A,? 7 ) +,(^
,C V * fl? > E?,4 (a q? ` fi g % ¥? 3 e? ( M,3 C,' g `? v w? T U A ( 4,?,? Q V ( )
* b,,c A ( (
% 3? o ;
" # $ (? J? m3,? (
- $ 3 (cm & W (
Thinking in Java 3rd Edition
a3 45
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
* - FA ( 1 2 3 m L ( FM3

,- $ 3 ( (
# - 3 g &? @ ( 0 ! 1 2 3? (? i }? 3 C (
˙ A?
/- 1 2 L % m e `? 3 8 L
(? ` 3 ′ ( 1 2 3 ˙A F? ′ (
( % 8 3 Q % ˇ 3
T c F 12? 3 ¥ ! A?
0- [,/ 1 2 3 ˙ 0 8
- # A ( 0 ! t 3 4 P ( $
q g 4 ˙ 0?
4 ˙ A ˇ 4 X
1 c
1 -?([,0 P -?
2 - 3 g ! ( 0 !? § A (@ · 3? (
3 -? 3 %? t # ( 0 ! ( %
- # ( 0 ! 1 2 3 3 @ m i n (
* - FA (?1 2 3 `? k 3 ( t 3?
( ( F L
8 A ( e f (¥ ! ` Fe f (^ * A J A?
,- 1 2 3 `? m 3 ( F O 4 3? ( ` ˙ A e f
( [,
,% F (1 T?O 4
# - V? [,,
/- ( 0 ! V?
" # %
0- 1 2 3? ( !? 3,e f
!? ( ( P A J A g; X ; ] } A 2 3 X c P A IJ 9 Q,
- 1 2 3? flW c? c( c? m W c
c c ( ` 1 2 3 § * % m 3,e f 3 ( (
( ^ 1 2 8 3 (V ( · 1 2 e f ¥ !
1 - 1 2 3? ! cm & ! ( 1 2 3? m?; P Q cm? ; u P Q ˇ( ( ` M3 P Q 1 2 3 ¥ ! ` u A ( < <
P Q ¥ !m Z
2 - V? [,2
Thinking in Java 3rd Edition
www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
a3 46
a4 a5 47 a4
* 3 - 1 2 3 k l ( F 1 2 3 (?
* - 1 2 3 k l ( 1 23 (?
* * - 1 2 3 k l ( k l 3? 3 [,? 3? 0 (
( { (
*,- 1 2 3? ( FM 3?1 2 3 (?
* # - 1 2 3? flW c % c( c ( ( ` 12 3? ( ˙ 8 3
* /- / ( 0 ! * (?
* 0- 2 ( 0 ! 3 - A 0 3 ′ (
(? % ; y ^ Q % d
* - 1 2 3? L (,1 2 3? m? 1 2 V · e f
,( (ˇ ( ` 1 2 3 k l 3
,(c ( v m 3 ;,( ] c ( E? 3 ; c * (; m ( c + ( ‰ A % m 3 ; c? u
,( (
1 2 3 ¥ !E? 3 ¥ ! 7 ¥ !e f (,(
Qf u ¥ ! 3,(
* 1 - 3 g > ( 0 ! 3? x (
/! > ( 0 ! u ; ′ (
/! ¥ !
* 2 - 8 > ( 0 ! (> 3? x 0 (
/!? 3 ′ (
> ( 0 ! ˙ A ′ (/! ¥ !
,3 - $ 3 E P Q ( ! … ‰ < < ^ Q X [ E
a18 a19 a19 a21
4 5 (?,currency1 6 F
7 8 * 3 3 F *,* ¥? 1,& t (?
a18 a19 a22a10a21
( 9 G F,?
a18 a19 a23 a21 +,
(? ˇu 4 m,$ ! ( currency1
( 3 G H ( I J (" A P Q (¥ ! m Z } [ X F Z W (m
Thinking in Java 3rd Edition
a3 47
a4 a5 47 a4
www.wgqqh.com/shhgs/tij.html
email:shhgs@sohu.com
a18 a19 a24 a21′ /
( > E ( X ; } Y?
( &
a18 a19 a25 a21 %
( 9
a18 a19 a26 a21
M 3? ; %,() ~? ! ( { ( M m,– g? g
" 3 U %,? m C $ X ^,| [ G ; | ^? > G? q (vw K {
a18 a19 a20 a217 ) ˇ? @ (Y
! % %
& ’ ( m,; 3 ˙ ¨ (£N