-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise23.java
More file actions
40 lines (36 loc) · 800 Bytes
/
Copy pathExercise23.java
File metadata and controls
40 lines (36 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
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
interface FactoryI<T> {
T create(Integer i);
}
class Foo2<T> {
private T x;
public void get() { System.out.println(x); }
public <F extends FactoryI<T>> Foo2(F factory, Integer i) {
x = factory.create(i);
}
}
class IntegerFactory implements FactoryI<Integer> {
public Integer create(Integer i) {
return new Integer(i);
}
}
class Widget {
int i;
public static class Factory implements FactoryI<Widget> {
public Widget create(Integer i) {
Widget w = new Widget();
w.i = i;
return w;
}
}
public String toString() {
return "Widget " + i;
}
}
class FactoryConstraint23 {
public static void main(String[] args) {
Foo2<Integer> f1 = new Foo2<Integer>(new IntegerFactory(), 1);
Foo2<Widget> f2 = new Foo2<Widget>(new Widget.Factory(), 2);
f1.get();
f2.get();
}
}