import java.util.*;
import java.lang.reflect.*;
public class ReflectionTest{
public static void main(String[] args){
String name;
Scanner in = new Scanner(System.in);
System.out.println("请输入类名(例如:java.util.Date)");
name = in.next();
try{
Class cl = Class.forName(name);
printFields(cl);
System.out.println();
}catch(ClassNotFoundException cn){
cn.printStackTrace();
}
System.exit(0);
}
public static void printFields(Class cl){
Field[] fields = cl.getFields();
for(Field f:fields){
Class type = f.getType();//类型名
String name = f.getName();//成员变量名
System.out.println(" ");
String modifiers = Modifier.toString(f.getModifiers());//修饰符
System.out.print(modifiers+" "+type+" "+name+";");
}
}
}
|