import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Main {
public static void main(String[] args) throws Exception {
Person p1 = new Senior("xxx", 24);
Class c = p1.getClass();
//获取接口
System.out.println("----获取接口----");
Class[] interfaces = c.getInterfaces();
for (Class in : interfaces) {
System.out.println("接口名字:" + in.getName());
}
//获取父类
System.out.println("----获取父类----");
Class superName = c.getSuperclass();
System.out.println("父类名字:" + superName.getName());
//获取构造函数
Constructor[] constructors;
constructors = c.getDeclaredConstructors();
System.out.println("----获取构造函数以及参数----");
for (int i = 0; i < constructors.length; i++) {
System.out.print(Modifier.toString(constructors[i].getModifiers()) + "参数:");
Class[] parametertypes = constructors[i].getParameterTypes();
for (int j = 0; j < parametertypes.length; j++) {
System.out.print(parametertypes[j].getName() + " ");
}
System.out.println("");
}
//获取方法
System.out.println("----获取方法----");
Method[] ms = c.getDeclaredMethods();
for (Method m :ms){
System.out.println("方法名字:"+m.getName());
}
//获取成员变量
System.out.println("----获取成员变量----");
Field[] fields = c.getDeclaredFields();
for (Field field : fields){
System.out.println("成员变量名字:"+field.getName());
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)