01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package state.two;
interface I {
public int f();
public int g();
public void changeDirection();
}
class C implements I {
private CState[] states = new CState[] { new CStateMinus(), new CStatePlus() };
private int index;
int i;
int j;
public int f() {
return states[index].f(this);
}
public int g() {
return states[index].g(this);
}
public void changeDirection() {
index = (index+1) % 2;
}
}
interface CState {
public int f(C x);
public int g(C x);
}
class CStateMinus implements CState {
public int f(C x) {
x.i -= 32;
return x.i;
}
public int g(C x) {
x.j -= 27;
return x.j;
}
}
class CStatePlus implements CState {
public int f(C x) {
x.i += 26;
return x.i;
}
public int g(C x) {
x.j += 42;
return x.j;
}
}
|