31day
获取运行时类的完整结构
通过反射获取运行时类的完整结构
Filed、Method、Constructor、Superclass、Interface、Annotation
-
全部的字段Filed
-
全部的方法Method
-
全部的构造器Constructor
-
所继承的父类Superclass
-
-
注解Annotation
package com.wang.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.wang.reflection.User");
User user = new User();
c1 = user.getClass();
//获得类的名字
System.out.println(c1.getName());//获得包名+类名
System.out.println(c1.getSimpleName());//获得类名
//获得属性
Field[] fields = c1.getFields();//只能找到public属性
fields = c1.getDeclaredFields();//找到全部的属性
for (Field field : fields) {
System.out.println(field);
}
//获得指定属性的值
Field name = c1.getDeclaredField("name");
System.out.println(name);
//获得类的方法
Method[] methods = c1.getMethods();//获得本类及其父类的全部public方法
for (Method method : methods) {
System.out.println("正常的:"+method);
}
methods = c1.getDeclaredMethods();//获得本类的全部方法
for (Method method : methods) {
System.out.println("getDeclaredMethods:"+method);
}
//获得指定方法
Method getName = c1.getMethod("getName", null);
System.out.println(getName);
Method setName = c1.getMethod("setName", String.class);
System.out.println(setName);
//获得构造器
System.out.println("================");
Constructor[] constructors = c1.getConstructors();//获得本类的全部public方法
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
Constructor[] declaredConstructors = c1.getDeclaredConstructors();//获得本类的全部方法
for (Constructor declaredConstructor : declaredConstructors) {
System.out.println("#"+declaredConstructor);
}
