-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFactoryConstraint.java
More file actions
54 lines (39 loc) · 1.07 KB
/
FactoryConstraint.java
File metadata and controls
54 lines (39 loc) · 1.07 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package generics;
import java.util.*;
/**
* RUN:
* javac generics/FactoryConstraint.java && java generics.FactoryConstraint
* OUTPUT:
*
*/
public class FactoryConstraint {
public static void main(String[] args) {
new Foo2<Integer>(new IntegerFactory());
new Foo2<Widget>(new Widget.Factory());
}
}
// --------------------------------------------------------------------
interface FactoryI<T> {
T create();
}
// --------------------------------------------------------------------
class Foo2<T> {
private T x;
public <F extends FactoryI<T>> Foo2(F factory) {
x = factory.create();
}
}
// --------------------------------------------------------------------
class IntegerFactory implements FactoryI<Integer> {
public Integer create() {
return new Integer(0);
}
}
// --------------------------------------------------------------------
class Widget {
public static class Factory implements FactoryI<Widget> {
public Widget create() {
return new Widget();
}
}
}