Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cbd33b8

Browse files
committed
泛型类
1 parent 4c577e0 commit cbd33b8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

generic/GenericClass.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package generic;
2+
3+
/**
4+
* 泛型类,典型的就是各种容器类如 List、Set、Map
5+
* @param <T>
6+
*/
7+
class GenericClass<T> {
8+
private T value;
9+
10+
GenericClass(T value) {
11+
this.value = value;
12+
}
13+
14+
public T getValue() {
15+
return this.value;
16+
}
17+
}
18+
19+
class Test {
20+
public static void main(String[] args) {
21+
// 实例化时指定类型
22+
GenericClass<String> genericString = new GenericClass<>("Generic Class");
23+
System.out.println(genericString.getValue());
24+
25+
GenericClass<Integer> genericInteger = new GenericClass<>(666);
26+
System.out.println(genericInteger.getValue());
27+
}
28+
}

0 commit comments

Comments
 (0)