[Java] + 1 + reflection to get the name and type attributes / member variables

Extended link: [Java] 2+ + set the property value of the reflection / member variables

1, the target class

 

 

2, the type of the target acquisition class group of attributes and

 

 

3, Code

 1     @Test
 2     public void testA() {
 3         Class clazz = ZGXRequest.class;
 4         // step1 获取类中所有的属性
 5         Field[] fields = clazz.getDeclaredFields();
 6         for (Field field : fields) {
 7             // step2 获取每个属性的类型(以点分割 需加双反斜杆转义)(获取到的为 java.lang.String 所以分割一下存到数组里)
 8             String[] fieldType = field.getType().getName().split("\\.");
 9             // step3 直接获取数组的最后一个元素即可(即直接获取到String)
10             System.out.println(String.format("属性类型:%s    属性名称:%s", fieldType[fieldType.length-1], field.getName()));
11         }
12     }

Guess you like

Origin www.cnblogs.com/danhuai/p/12041832.html