java高级编程-反射获取方法、属性、构造(附源码解析)

ava反射编程

准备bean

获取对象对应的类对象 见博客:https://blog.csdn.net/qq_41692766/article/details/105869834

/**
 * description
 *
 * @author yufw
 * date 2020/4/29 19:42
 */
public class People {

    private String name;
    public int age;

    private boolean sex;


    public People() {

    }

    public People(int age, boolean sex, String name) {
        this.age = age;
        this.sex = sex;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Method

getMethod(String name, Class…<?> parameterTypes)

获得该类某个公有的方法

getMethods()

获得该类所有公有的方法

getDeclaredMethod(String name, Class…<?> parameterTypes)

获得该类某个方法

getDeclaredMethods()

获得该类所有方法

@Test
public void testMethod() throws NoSuchMethodException, ClassNotFoundException {

    Class<People> pClass = (Class<People>) Class.forName("com.studynote.utils.People");

    // 获得该类所有公有的方法
    Method[] methods = pClass.getMethods();

    // 获取该类的所有 方法
    Method[] declaredMethods = pClass.getDeclaredMethods();

    // 获取该类 方法名是 getAge 入参类型 null 的 公有方法
    Method method = pClass.getMethod("getAge", null);

    // 获取该类 方法名是 getName 入参类型 null 的 方法
    Method declaredMethod = pClass.getDeclaredMethod("getName", null);

}

Field

get(Object obj)

只能获取 公共属性值

set(Object obj, Object value)

只能设置公共属性值

@Test
public void testField() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Class<?> aClass1 = Class.forName("com.studynote.utils.People");


    People p = (People) aClass1.getDeclaredConstructor().newInstance();
    // 获取所有的 公有属性
    Field[] fields = aClass1.getFields();

    Arrays.stream(fields).
        map(Field::getName).
        forEach(System.out::println);


    // 获取所有的属性
    Field[] declaredFields = aClass1.getDeclaredFields();

    Arrays.stream(declaredFields).
        map(Field::getName).
        forEach(System.out::println);


    // 获取属性名是 age 的公有变量
    // 如果 没有此公有变量 抛异常
    //        Field age = aClass1.getField("age");

    //        System.out.println(age);

    // 获取属性名是 age 的所有变量
    // 如果没有 没有此名称 的变量 抛异常
    Field age1 = aClass1.getDeclaredField("age");

    System.out.println(age1.getName());

    age1.setAccessible(false);
    age1.set(p,2);

    System.out.println(age1.get(p));

    System.out.println(age1);

}

Contructor

getConstructor(Class…<?> parameterTypes)

获取匹配参数的公有构造

getConstructors()

获取所有的 公有构造

getDeclaredConstructor(Class…<?> parameterTypes)

获取匹配参数的所有构造

getDeclaredConstructors()

获取该类所有的构造

@Test
public void testContructor() throws ClassNotFoundException, NoSuchMethodException {

    Class<People> pClass = (Class<People>) Class.forName("com.studynote.utils.People");

    Constructor<?>[] contructors = pClass.getConstructors();

    Constructor<People> contructor = pClass.getConstructor(Integer.TYPE, Boolean.TYPE, String.class);

    Constructor<People> declaredContrctor = pClass.getDeclaredConstructor(null);

    Constructor<?>[] declaredContrctors = pClass.getDeclaredConstructors();

    System.out.println(1);
}

源码地址:https://gitee.com/twelfthLunarMonthFourteen/pub_beauty/tree/hotfix/study-note/src/main/java/com/studynote/reflect

个人水平有限,如有问题,请各路大神指教留言,评论区讨论,虚心接纳**

如果觉得有帮助,请点赞收藏,谢谢

猜你喜欢

转载自blog.csdn.net/qq_41692766/article/details/105930509
今日推荐