Skip to content

Commit bce2bf3

Browse files
committed
1.接口 2.内部类
1 parent 179e12f commit bce2bf3

39 files changed

+1265
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//: innerclasses/AnonymousConstructor.java
2+
// Creating a constructor for an anonymous inner class.
3+
4+
package com.innerclasses10;
5+
6+
abstract class Base {
7+
public Base(int i) {
8+
System.out.println("Baase constructor. i = " + i);
9+
}
10+
public abstract void f();
11+
}
12+
13+
public class AnonymousConstructor {
14+
public static Base getBase(int i) {
15+
return new Base(i) {
16+
{ System.out.println("Inside instance initializer"); }
17+
public void f() {System.out.println("In anonymous f()"); }
18+
};
19+
}
20+
public static void main(String[] args) {
21+
Base base = getBase(47);
22+
base.f();
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//: innerclasses/BinEgg.java
2+
// An inner class cannot be overriden like a method.
3+
4+
package com.innerclasses10;
5+
6+
class Egg {
7+
private Yolk y;
8+
protected class Yolk {
9+
public Yolk() { System.out.println("Egg.Yolk()"); }
10+
}
11+
public Egg() {
12+
System.out.println("New Egg()");
13+
y = new Yolk();
14+
}
15+
}
16+
17+
public class BigEgg extends Egg {
18+
public class Yolk {
19+
public Yolk() { System.out.println("BigEgg.Yolk();"); }
20+
21+
}
22+
public static void main(String[] args) {
23+
new BigEgg();
24+
}
25+
}
26+
27+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//: innerclasses/BigEgg.java
2+
// Proper inheritance of an inner class:
3+
4+
package com.innerclasses10;
5+
6+
class Egg2 {
7+
protected class Yolk {
8+
public Yolk() { System.out.println("Egg2.Yolk()"); } //3
9+
public void f() { System.out.println("Egg2.Yolk.f()"); }
10+
}
11+
private Yolk y = new Yolk(); //1
12+
public Egg2() { System.out.println("New Egg2"); } //2
13+
public void insertYolk(Yolk yy) { y = yy; }
14+
public void g() { y.f(); }
15+
}
16+
17+
public class BigEgg2 extends Egg2 {
18+
public class Yolk extends Egg2.Yolk {
19+
public Yolk() { System.out.println("BigEgg2.Yolk()"); } //4
20+
public void f() { System.out.println("BigEgg2.Yolk.f()"); } //5
21+
}
22+
public BigEgg2() { insertYolk(new Yolk()); }
23+
public static void main(String[] args) {
24+
Egg2 e2 = new BigEgg2();
25+
e2.g();
26+
}
27+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//: innerclasses/Callbacks.java
2+
// Using inner classes for callbacks
3+
4+
package com.innerclasses10;
5+
6+
interface Incrementable {
7+
void increment();
8+
}
9+
10+
// Very simple to just implement the interface:
11+
class Callee1 implements Incrementable {
12+
private int i = 0;
13+
public void increment() {
14+
i++;
15+
System.out.println("Callee1 " + i);
16+
}
17+
}
18+
19+
class MyIncrement {
20+
public void Increment() { System.out.println("Other operation"); }
21+
static void f(MyIncrement mi) { mi.Increment(); }
22+
}
23+
24+
// If your class muse implement increment() in
25+
// some other way, you must use an inner class.
26+
class Callee2 extends MyIncrement {
27+
private int i = 0;
28+
public void Increment() {
29+
super.Increment();
30+
i++;
31+
System.out.println("Callee2 " + i);
32+
}
33+
private class Closure implements Incrementable {
34+
public void increment() {
35+
// Specify outer-class method, otherwise
36+
// You'd get an infinite recursion
37+
Callee2.this.Increment();
38+
}
39+
}
40+
Incrementable getCallbackReference() {
41+
return new Closure();
42+
}
43+
}
44+
45+
class Caller {
46+
private Incrementable callbackReference;
47+
Caller(Incrementable cbh) { callbackReference = cbh; }
48+
void go() { callbackReference.increment(); }
49+
}
50+
51+
52+
public class CallBacks {
53+
54+
public static void main(String[] args) {
55+
// TODO Auto-generated method stub
56+
Callee1 c1 = new Callee1();
57+
Callee2 c2 = new Callee2();
58+
MyIncrement.f(c2);
59+
Caller caller1 = new Caller(c1);
60+
Caller caller2 = new Caller(c2.getCallbackReference());
61+
caller1.go();
62+
caller1.go();
63+
caller2.go();
64+
caller2.go();
65+
caller2.go();
66+
}
67+
68+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//: innerclasses/ClassInInterface.java
2+
// {main: ClassInInterface$Test}
3+
4+
package com.innerclasses10;
5+
6+
public interface ClassInInterface {
7+
void howdy();
8+
class Test implements ClassInInterface {
9+
public void howdy() {
10+
System.out.println("Howdy!");
11+
}
12+
public static void main(String[] args) {
13+
new Test().howdy();
14+
}
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//: innerclasses/Contents.java
2+
3+
package com.innerclasses10;
4+
5+
public interface Contents {
6+
int value();
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//: innerclasses/Destination.java
2+
3+
package com.innerclasses10;
4+
5+
public interface Destination {
6+
String readLabel();
7+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//: innerclasses/DotThis.java
2+
// Qualifying access to the outer-class object
3+
4+
package com.innerclasses10;
5+
6+
public class DotThis {
7+
void f() {
8+
System.out.println("DotThis.f()");
9+
}
10+
public class Inner {
11+
public DotThis outer() {
12+
return DotThis.this;
13+
// A plain "this" would be Inner's "this"
14+
}
15+
}
16+
public Inner inner() { return new Inner(); }
17+
public static void main(String[] args) {
18+
DotThis dt = new DotThis();
19+
DotThis.Inner dti = dt.inner();
20+
dti.outer().f();
21+
22+
DotThis dt1 = new DotThis();
23+
DotThis.Inner dti1 = dt1.new Inner();
24+
}
25+
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//: innerclasses/Factories.java
2+
3+
package com.innerclasses10;
4+
5+
interface Service {
6+
void method1();
7+
void method2();
8+
}
9+
10+
interface ServiceFactory {
11+
Service getService();
12+
}
13+
14+
class Implementation1 implements Service {
15+
private Implementation1() {}
16+
public void method1() { System.out.println("Implementation1 method1"); }
17+
public void method2() { System.out.println("Implementation2 method2"); }
18+
public static ServiceFactory factory =
19+
new ServiceFactory() {
20+
public Service getService() {
21+
return new Implementation1();
22+
}
23+
};
24+
}
25+
26+
class Implementation2 implements Service {
27+
private Implementation2() {}
28+
public void method1() { System.out.println("Implementation2 method1"); }
29+
public void method2() { System.out.println("Implementation2 method2"); }
30+
public static ServiceFactory factory =
31+
new ServiceFactory() {
32+
public Service getService() {
33+
return new Implementation2();
34+
}
35+
};
36+
}
37+
38+
public class Factories {
39+
40+
public static void serviceConsumer(ServiceFactory fact) {
41+
Service s = fact.getService();
42+
s.method1();
43+
s.method2();
44+
}
45+
46+
public static void main(String[] args) {
47+
// TODO Auto-generated method stub
48+
49+
serviceConsumer(Implementation1.factory);
50+
serviceConsumer(Implementation2.factory);
51+
}
52+
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//: innerclasses/Games.java
2+
// Using the anonymous inner classes with the Game Framework
3+
4+
package com.innerclasses10;
5+
6+
interface Game { boolean move(); }
7+
interface GameFactory { Game getGame(); }
8+
9+
class Checker implements Game {
10+
private Checker() {}
11+
private int moves = 0;
12+
private static final int MOVE = 3;
13+
public boolean move() {
14+
System.out.println("Checker move: " + moves);
15+
return ++moves != MOVE;
16+
}
17+
public static GameFactory gamefactory =
18+
new GameFactory() {
19+
public Game getGame() {
20+
return new Checker();
21+
}
22+
};
23+
}
24+
25+
class Chess implements Game {
26+
private Chess() {}
27+
private int moves = 0;
28+
private static final int MOVE = 4;
29+
public boolean move() {
30+
System.out.println("Chess move: " + moves);
31+
return ++moves != MOVE;
32+
}
33+
public static GameFactory gamefactory =
34+
new GameFactory() {
35+
public Game getGame() {
36+
return new Chess();
37+
}
38+
};
39+
}
40+
41+
public class Games {
42+
public static void playGame(GameFactory fact) {
43+
Game game = fact.getGame();
44+
while(game.move()) {
45+
46+
}
47+
}
48+
49+
public static void main(String[] args) {
50+
playGame(Checker.gamefactory);
51+
playGame(Chess.gamefactory);
52+
}
53+
54+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//: innerclasses/GreenhouseController.java
2+
// Configure and execute the greenhouse system.
3+
4+
package com.innerclasses10;
5+
import com.innerclasses10.controller.*;
6+
7+
public class GreenHouseController {
8+
public static void main(String[] args) {
9+
GreenHouseControls gc = new GreenHouseControls();
10+
gc.addEvent(gc.new Bell(100));
11+
Event[] eventList = {
12+
gc.new ThermostatNight(0),
13+
gc.new LightOn(200),
14+
gc.new LightOff(400),
15+
gc.new WaterOn(600),
16+
gc.new WaterOff(800),
17+
gc.new ThermostatDay(1400)
18+
};
19+
gc.addEvent(gc.new Restart(2000, eventList));
20+
gc.addEvent(new GreenHouseControls.Terminate(5000));
21+
gc.run();
22+
}
23+
}

0 commit comments

Comments
 (0)