-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPair.java
More file actions
59 lines (48 loc) · 1.35 KB
/
Pair.java
File metadata and controls
59 lines (48 loc) · 1.35 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
package example.GenericExample;
/**
* 泛型<T> 不能用在靜態方法 (static method), 除非使用 static <T> Pair<T>才可以。
*
* Reference:
* - https://www.liaoxuefeng.com/wiki/1252599548343744/1265105853480864
*
* @param <T>
*/
public class Pair<T> {
// same type for parameters.
private T fist;
private T last;
public Pair(T first, T last) {
this.fist = first;
this.last = last;
}
public T getFirst() {
return fist;
}
public T getLast() {
return last;
}
public static <K> Pair<K> create(K first, K last) {
//! 注意如果使用 public static Pair<T> create() 則會有錯誤訊息,需要使用 <T> Pair<T> create().s
return new Pair<K>(first, last);
}
public static void main(String[] args) {
SecondPair<String, Integer> p = new SecondPair<> ("test", 123);
System.out.println("first: " + p.getFirst());
System.out.println("last: " + p.getLast());
}
}
class SecondPair<T, K> {
// two different type for parameters.
private T first;
private K last;
public SecondPair(T first, K last) {
this.first = first;
this.last = last;
}
public T getFirst() {
return first;
}
public K getLast() {
return last;
}
}