Java 反射机制示例

Java反射的用法

主要反射API,用来生成在当前JAVA虚拟机中的类、接口或者对象的信息。

1.Class类:反射的核心类,可以获取类的属性,方法等内容信息。

2.Field类:Java.lang.reflect.表示类的属性,可以获取和设置类的中属性值。

3.Method类:Java.lang.reflect,表示类的方法,它可以用来获取类中方法的信息或者执行方法

4.Construcor类:Java.lang.reflect,表示类的构造方法。

下面是示例代码:

实体类:

public class UserEntity {

    private Integer userId;

    private String userName;

    private String userSex;

    private Integer userAge;

    public UserEntity() {}

    public UserEntity(Integer userId, String userName, String userSex, Integer userAge) {
        this.userId = userId;
        this.userName = userName;
        this.userSex = userSex;
        this.userAge = userAge;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }

    public Integer getUserAge() {
        return userAge;
    }

    public void setUserAge(Integer userAge) {
        this.userAge = userAge;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", userSex='" + userSex + '\'' +
                ", userAge=" + userAge +
                '}';
    }
}

util类:




import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public class ReflectUtil<T> {

    /**
     * 方法用途: 给实体属性赋值<br/>
     * 操作步骤: TODO<br/>
     * ${tags}
     */
    public static <T> Object getObject(Class<T> clazz, Map<String, Object> map) {
        Object obj = null;
        try {
            // 构造方法;如果包含参数,并给属性赋值clazz.getConstructor(Integer.class,String.class,String.class,Integer.class)
            Constructor constructor = clazz.getConstructor();
            // 有参构造方法constructor.newInstance(110,"张三丰","man",99),等价new UserEntity(110,"张三丰","man",99)
            obj = constructor.newInstance();
            // 无构造函数,给属性赋值
            Method method = null;
            // getFields只能拿到public属性,想要拿到private属性就用getDeclaredFields
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                // 将field可见性设置为true,默认为false(如果没有设为true报错)
                field.setAccessible(true);
                // 属性类别:Integer、String、Boolean等
                String fieldType = field.getType().getName();
                // 属性名称
                String fieldName = field.getName();
                if (fieldType.equals("java.lang.Boolean")) {
                    method = clazz.getDeclaredMethod("is" + getMethodName(fieldName), field.getType());
                } else {
                    method = clazz.getDeclaredMethod("set" + getMethodName(fieldName), field.getType());
                }
                /*if (fieldType.equals("java.lang.Integer")) {
                    method = clazz.getDeclaredMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), Integer.class);
                } else if (fieldType.equals("java.lang.String")) {
                    method = clazz.getDeclaredMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), String.class);
                }*/
                for (String key : map.keySet()) {
                    if (fieldName.equals(key)) {
                        // 给属性赋值,等价于set方法(xxx.set())
                        method.invoke(obj, map.get(key));
                        break;
                    }
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static void getMethod(Class clazz) {
        try {
            Method method = clazz.getDeclaredMethod("getUserId");
            System.out.println(method.getName());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    /**
     * 方法用途: 获取实体所有属性值<br/>
     * 操作步骤: TODO<br/>
     * ${tags}
     */
    public static void getBeanValues(Object object) {
        Class clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // 同样需要先将field可见性设为可见
            field.setAccessible(true);
            try {
                // field.get() 获取属性值
                System.out.println(field.get(object));;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 方法用途: 把一个字符串的第一个字母大写、效率是最高的<br/>
     * 操作步骤: TODO<br/>
     * ${tags}
     */
    private static String getMethodName(String fildeName) {
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", 110);
        map.put("userName", "张三丰");
        map.put("userSex", "man");
        map.put("userAge", 99);
        UserEntity userEntity = (UserEntity) ReflectUtil.getObject(UserEntity.class, map);
        ReflectUtil.getMethod(UserEntity.class);
        ReflectUtil.getBeanValues(userEntity);
    }
内容概要:本文档是一份关于“超声谐波成像中幅超声谐波成像中幅度调制聚焦超声引起的全场位移和应变的分析模型(Matlab代码实现)度调制聚焦超声引起的全场位移和应变的分析模型”的Matlab代码实现研究资料,重点构建了一个用于分析在超声谐波成像过程中,由幅度调制聚焦超声所引发的生物组织全场位移与应变的数学模型。该模型通过Matlab仿真手段实现了对声场激励下组织力学响应的精确计算与可视化,有助于深入理解超声激励与组织变形之间的物理机制,提升超声弹性成像的精度与可靠性。文档还附带多个相关科研领域的Matlab/Simulink代码实例,涵盖无人机控制、路径规划、电力系统仿真、信号处理、机器学习等多个方向,展示了强大的技术支撑与应用拓展能力。; 适合人群:具备Matlab编程基础,从事医学超声成像、生物力学建模、信号与图像处理等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于超声弹性成像中组织力学响应的仿真与分析;②为开发新型超声诊断技术提供理论模型与算法支持;③作为多物理场耦合仿真的教学与研究案例,促进跨学科技术融合。; 阅读建议:建议读者结合Matlab代码逐行理解模型实现细节,重点关注声场建模、组织力学方程求解及位移应变后处理部分。同时可参考文档中提供的其他仿真案例,拓宽研究思路,提升综合科研能力。
标题基于SpringBoot的高校餐饮档口管理系统设计与实现AI更换标题第1章引言介绍高校餐饮档口管理系统的研究背景、意义、国内外现状及论文方法与创新点。1.1研究背景与意义阐述高校餐饮档口管理现状及系统开发的重要性。1.2国内外研究现状分析国内外高校餐饮管理系统的研究与应用进展。1.3研究方法及创新点概述本文采用的研究方法及系统设计的创新之处。第2章相关理论总结与高校餐饮档口管理系统相关的现有理论。2.1SpringBoot框架理论阐述SpringBoot框架的原理、优势及其在Web开发中的应用。2.2数据库设计理论介绍数据库设计的基本原则、方法和步骤。2.3系统安全理论讨论系统安全设计的重要性及常见安全措施。第3章系统需求分析对高校餐饮档口管理系统的功能需求、性能需求等进行详细分析。3.1功能需求分析列举系统需实现的主要功能,如档口管理、订单处理等。3.2性能需求分析分析系统对响应时间、并发处理能力等性能指标的要求。3.3非功能需求分析阐述系统对易用性、可维护性等非功能方面的需求。第4章系统设计详细描述高校餐饮档口管理系统的设计过程。4.1系统架构设计给出系统的整体架构,包括前端、后端和数据库的设计。4.2模块设计详细介绍各个功能模块的设计,如用户管理、档口信息管理等。4.3数据库设计阐述数据库表结构的设计、数据关系及索引优化等。第5章系统实现与测试介绍高校餐饮档口管理系统的实现过程及测试方法。5.1系统实现系统各模块的具体实现过程,包括代码编写和调试。5.2系统测试方法介绍系统测试的方法、测试用例设计及测试环境搭建。5.3系统测试结果与分析从功能、性能等方面对系统测试结果进行详细分析。第6章结论与展望总结本文的研究成果,并展望未来的研究方向。6.1研究结论概括高校餐饮档口管理系统的设计与实现成果。6.2展望指出系统存在的不足及未来改进和扩展的方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值