-
-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy path1-3-0.java
More file actions
71 lines (58 loc) · 1.7 KB
/
1-3-0.java
File metadata and controls
71 lines (58 loc) · 1.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package genericsdemo;
public class Box<T> {
private T value;
public Box(T value) { this.value = value; }
public T get() { return value; }
}
public class Main {
public static void main(String[] args) {
Box<String> b = new Box<>("hello");
NumberBox<Integer> nb = new NumberBox<>(42);
Utils.print("Generic method!");
Wildcards.printBox(b);
Wildcards.sumNumbers(nb);
Pair<String, Integer> p = new Pair<>("age", 30);
SelfComparable<MyCmp> a = new MyCmp(10);
SelfComparable<MyCmp> c = new MyCmp(20);
System.out.println(a.compareTo(c));
}
}
class MyCmp extends SelfComparable<MyCmp> {
public MyCmp(int data) {
super(data);
}
}
public class NumberBox<T extends Number> {
private T num;
public NumberBox(T num) { this.num = num; }
public double doubleValue() { return num.doubleValue(); }
}
public class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
}
public class SelfComparable<T extends SelfComparable<T>> implements Comparable<T> {
private int data;
public SelfComparable(int data) { this.data = data; }
@Override
public int compareTo(T other) {
return Integer.compare(this.data, other.data);
}
}
public class Wildcards {
public static void printBox(Box<?> box) {
System.out.println("Box contains: " + box.get());
}
public static void sumNumbers(NumberBox<? extends Number> nb) {
System.out.println("Double value: " + nb.doubleValue());
}
}
public class Utils {
public static <T> void print(T value) {
System.out.println(value);
}
}