Skip to content

Commit f94f1e3

Browse files
committed
泛型
1 parent 643264c commit f94f1e3

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/GenericsDemo.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 泛型
3+
*/
4+
public class GenericsDemo {
5+
public static void main(String[] args) {
6+
Pair<Integer, String> pair = new Pair<>(1, "apple");
7+
Pair<Integer, String> pair1 = new Pair<>(2, "Pear");
8+
boolean isSame;
9+
if (compare(pair, pair1)) isSame = true;
10+
else isSame = false;
11+
System.out.println(isSame);
12+
}
13+
14+
public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
15+
return p1.getKey().equals(p2.getKey()) &&
16+
p1.getValue().equals(p2.getValue());
17+
}
18+
}
19+
20+
class Pair<K, V> {
21+
private K key;
22+
private V value;
23+
24+
public Pair(K key, V value) {
25+
this.key = key;
26+
this.value = value;
27+
}
28+
29+
public void setKey(K key) {
30+
this.key = key;
31+
}
32+
33+
public void setValue(V value) {
34+
this.value = value;
35+
}
36+
37+
public K getKey() {
38+
return key;
39+
}
40+
41+
public V getValue() {
42+
return value;
43+
}
44+
}

0 commit comments

Comments
 (0)