Skip to content

Commit 4474515

Browse files
committed
泛型参数化
1 parent c1fd88d commit 4474515

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

java-reflect/src/com/zyj/reflect/Demo03.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public static void main(String[] args) throws IllegalAccessException, Instantiat
4141
field.set(u4,"张勇杰3");
4242
System.out.println(u4.getName());
4343
//通过反射API操作属性
44-
SimpleDateFormat
4544
} catch (ClassNotFoundException e) {
4645
e.printStackTrace();
4746
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.zyj.reflect;
2+
3+
import com.zyj.bean.User;
4+
5+
import java.lang.reflect.Method;
6+
import java.lang.reflect.ParameterizedType;
7+
import java.lang.reflect.Type;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
/**
12+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
13+
*
14+
* @author : 张勇杰
15+
* @date : 2018/11/25 12:34
16+
* @Version : v1.0
17+
* @description
18+
**/
19+
public class Demo04 {
20+
public void test01(Map<String,User> map, List<User> list){
21+
System.out.println("Demo04.test01");
22+
}
23+
24+
public Map<Integer,User> test02(){
25+
System.out.println("Demo04.test02");
26+
return null;
27+
}
28+
29+
public static void main(String[] args) throws NoSuchMethodException {
30+
Method m1 = Demo04.class.getMethod("test01",Map.class,List.class);
31+
Type []types = m1.getGenericParameterTypes();
32+
for(Type paramType:types){
33+
System.out.println("#"+paramType);
34+
if(paramType instanceof ParameterizedType){
35+
Type[] genericTypes = ((ParameterizedType) paramType).getActualTypeArguments();
36+
for(Type genericType:genericTypes){
37+
System.out.println("泛型类型"+genericType);
38+
}
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)