栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

反射对象的获取和方法的调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

反射对象的获取和方法的调用

测试类
package jess.day11;


public class User {
    private static final int MAN =1;
    private static final int WOMAN =1;
    private String no;
    public String name;
    private Integer age;
    private int sex ;

    public User() {
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public User(String no, String name, Integer age, int sex) {
        this.no = no;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public int getSex() {
        return sex;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "no='" + no + ''' +
                ", name='" + name + ''' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}

测试reflect
package jess.day11;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class GetTest {
    Class clazz = User.class;

    
    @Test
    public void getUser() throws ClassNotFoundException {
        // 使用类名获取
        Class clazz = User.class;
        // 使用实例对象获取
        User user = new User();
        Class clazz1 = user.getClass();
        // 使用全限定名称
        Class clazz2 = Class.forName("jess.day11.User");
    }

    @Test
    
    public void getFieldTest() throws NoSuchFieldException, IllegalAccessException {
        // 输出类名
        System.out.println(clazz.getName());
        // 获取名字为 name 属性值得属性
        Field name1 = clazz.getField("name");
        // new 一个对象
        User jess = new User("jess", 20);
        // 通过反射获得的name1属性来获取jess 的name 属性值
        System.out.println(name1.get(jess));
        // 获取 User的全部属性值
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
        }
    }

    @Test
    

    public void getMethodTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 传递方法名获取方法
        Method getAgeClazz = clazz.getMethod("getAge");
        //  新建测试对象
        User jess = new User("jess", 20);
        // 调用反射获取的getAge方法
        System.out.println(getAgeClazz.invoke(jess));
        // 获取public的全部方法 包括父类的
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
        // 获取本类的所用方法 包括private修饰的
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod.getName());
        }
        Method setAge1 = clazz.getMethod("setAge", Integer.class);
        // 设置访问权限
        setAge1.setAccessible(true);
        setAge1.invoke(jess, 11);
        System.out.println(jess.toString());
    }

    @Test
    
    public void getConstructorTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 获取所用构造器
        Constructor[] constructors = clazz.getConstructors();

        for (Constructor constructor : constructors) {
            System.out.println(constructor.getName());
        }
        // 获取有两个参数的构造器
        Constructor constructor1 = clazz.getConstructor(String.class, Integer.class);
        // 通过反射获取的构造器 构造实例对象
        User name = constructor1.newInstance("name", 20);
        System.out.println(name.toString());
    }

    @Test
    
    public void getAnnotationTest() {
        // 获取所有注解
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("annotation = " + annotation);
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/288487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号