根据已知类获得其内部结构
- getDeclared表示获得本类的全部
- 后面不带参数,表示获得全部,带参数表示获得指定的属性、方法、构造器。
package com.dingha.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test08 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class.forName("com.dingha.reflection.User");
System.out.println(c1.getName());
System.out.println(c1.getSimpleName());
System.out.println("===================");
Field[] fields = c1.getFields();
fields = c1.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
Field name = c1.getDeclaredField("name");
System.out.println(name);
System.out.println("===================");
Method[] methods = c1.getMethods();
for (Method method : methods) {
System.out.println("正常的"+method);
}
Method[] declaredMethods = c1.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod);
}
Method getName = c1.getMethod("getName",null);
Method setName = c1.getMethod("setName", String.class);
System.out.println(getName);
System.out.println(setName);
System.out.println("===================");
Constructor[] constructors = c1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
Constructor[] declaredConstructors = c1.getDeclaredConstructors();
for (Constructor declaredConstructor : declaredConstructors) {
System.out.println("##"+declaredConstructor);
}
Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
System.out.println("指定"+declaredConstructor);
}
}