一个字段包括基本数据类型和引用数据类型.基本数据类型:boolean,byte,short,int long,char,float,double. 一个引用型的类型可以是java.lang.Object直接,间接的子类,以及接口,数组,枚举类型.
以下示例了如何来获取字段类型.
以下示例了如何来获取字段类型.
import java.lang.reflect.Field;
import java.util.List;
import static java.lang.System.out;
public class FieldSpy<T> {
public boolean[][] b = {{false,false},{true,true}};
public String name = "Alice";
public List<Integer> List;
public T val;
public static void main(String... args) {
try{
Class<?> c = Class.forName("FieldSpy");
Field f = c.getField(args[1]);
out.println(f.getType());
out.format("Type: %s%n", f.getType());
out.format("GenericType: %s%n",f.getGenericType());
} catch(ClassNotFoundException x) {
x.printStackTrace();
}catch(NoSuchFieldException x) {
x.printStackTrace();
}
}
}