通过反射将俩个类对象交换

news/2024/5/19 5:19:50 标签: java, 反射, 编程语言

通过反射将俩个类对象交换

现在需求如下,需要封装一个工具类,该工具通过反射和注解将一个类的特定内容取出,赋值给另一个类。实现类的交换。


提示:通过此练习可以对反射的理解更上一层楼。

文章目录

  • 通过反射将俩个类对象交换
  • 一、思考
    • 1. 反射的来龙去脉
    • 2. 如何解决问题
  • 二、过程的实现。
    • 1.首先编写两个注解,用来筛选需要的值和不需要的值。
    • 2. 封装工具类
    • 3. 测试类,新建两个对象类和测试类,验证结果。
  • 三、测试结果


一、思考

1. 反射的来龙去脉

什么是反射反射可以做什么?
在这里插入图片描述

高级工程师都会亲睐反射,因为反射可以做到很多事情,甚至大幅度简化程序。
在这里插入图片描述
反射是Java语言的特征之一。它允许在运行时动态加载类、获取类信息、生成对象、操作对象的属性或方法等。
在这里插入图片描述
反射可以做到很多事情,比如

  • 在运行时判断任意一个对象所属的类;
  • 在运行时构造任意一个类的对象;
  • 在运行时判断任意一个类所具有的成员变量和方法;
  • 在运行时调用任意一个对象的方法。甚至是private方法。
  • 生成动态代理等
    在这里插入图片描述

理解反射对一个程序员来讲是至关重要的。
在这里插入图片描述

2. 如何解决问题

然后思考:如何通过发射拿到一个类的所有方法,并且把一个对象中需要的值赋值给另一个对象?
在这里插入图片描述

这个算法核心是字段的赋值,字段哪里来,当然通过反射类调用的方法拿到了。
在这里插入图片描述

如果我们拿到了资源对象的字段,然后利用字段的get方法,是不是就可以把资源对象的值赋值给目标对象上。围绕此核心,我们首先要拿到资源对象的Class类对象,然后利用该类对象获取所有的描述字段的值,然后遍历所有资源对象字段的值,在其中利用注解拿到我们想拿的值,剔除我们不想要的值。然后再拿到目标对象的对象类,获取目标对象的对应资源对象字段名称的值,然后通过目标对象的get方法将值取到。

二、过程的实现。

1.首先编写两个注解,用来筛选需要的值和不需要的值。

TargetField.java

java">package com.lqz.test.annotation;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD})
public @interface TargetField {

    String value();
}

IgnoreField.java

java">package com.lqz.test.annotation;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD})
public @interface IgnoreField {
}

2. 封装工具类

CopyExcutor.java

java">package com.lqz.test.util;

import com.lqz.test.annotation.IgnoreField;
import com.lqz.test.annotation.TargetField;

import java.lang.reflect.Field;

public class CopyExcutor {
    private Object source;
    private Object target;

    public CopyExcutor(Object source, Object target) {
        this.source = source;
        this.target = target;
    }
    public void executor() throws IllegalAccessException {
        Class<?> sourceClass = source.getClass();
        Field[] sourcedeclaredFields = sourceClass.getDeclaredFields();
        for (int i = 0; i < sourcedeclaredFields.length; i++) {
            IgnoreField ignoreField = sourcedeclaredFields[i].getAnnotation(IgnoreField.class);
            if (ignoreField!=null){
                continue;
            }

            String targetName=null;


            TargetField targetField = sourcedeclaredFields[i].getAnnotation(TargetField.class);
            if (targetField==null){
                targetName=  sourcedeclaredFields[i].getName();//对象字段名
            }else {
                targetName=targetField.value();
            }
            Class<?> targetClass = target.getClass();
            Field declaredField=null;
            try {
                declaredField = targetClass.getDeclaredField(targetName);
            } catch (NoSuchFieldException e) {
                System.out.println("没有拿到字段");
                System.exit(0);
            }
            sourcedeclaredFields[i].setAccessible(true);
            Object o = sourcedeclaredFields[i].get(source);
            if (o==null){
                return;
            }

            declaredField.setAccessible(true);
            declaredField.set(target,o);
        }
    }

}

3. 测试类,新建两个对象类和测试类,验证结果。

Source.java

java">package com.lqz.test.entity;

import com.lqz.test.annotation.IgnoreField;
import com.lqz.test.annotation.TargetField;

import java.util.Date;

public class Source {
    @TargetField("name")
    private String Sname;
    @IgnoreField
    private String Snumber;
    @TargetField("age")
    private int age;
    @TargetField("score")
    private double score;
    @TargetField("birthday")
    private Date Sbrithday;
    @IgnoreField
    private String selse;

    @Override
    public String toString() {
        return "Source{" +
                "Sname='" + Sname + '\'' +
                ", Snumber='" + Snumber + '\'' +
                ", age=" + age +
                ", score=" + score +
                ", Sbrithday=" + Sbrithday +
                ", selse='" + selse + '\'' +
                '}';
    }

    public String getSname() {
        return Sname;
    }

    public void setSname(String sname) {
        Sname = sname;
    }

    public String getSnumber() {
        return Snumber;
    }

    public void setSnumber(String snumber) {
        Snumber = snumber;
    }

    public int getAge() {
        return age;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public Date getSbrithday() {
        return Sbrithday;
    }

    public void setSbrithday(Date sbrithday) {
        Sbrithday = sbrithday;
    }

    public String getSelse() {
        return selse;
    }

    public void setSelse(String selse) {
        this.selse = selse;
    }

    public Source() {
    }

    public Source(String sname, String snumber, int age, double score, Date sbrithday, String selse) {
        Sname = sname;
        Snumber = snumber;
        this.age = age;
        this.score = score;
        Sbrithday = sbrithday;
        this.selse = selse;
    }
}

Target.java

java">package com.lqz.test.entity;

import java.util.Date;

public class Target {
    private String name;
    private int age;
    private double score;
    private Date birthday;

    public Target() {
    }

    @Override
    public String toString() {
        return "Target{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                ", birthday=" + birthday +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Target(String name, int age, double score, Date birthday) {
        this.name = name;
        this.age = age;
        this.score = score;
        this.birthday = birthday;
    }
}

Test.java

java">package com.lqz.test;

import com.lqz.test.entity.Source;
import com.lqz.test.entity.Target;
import com.lqz.test.util.CopyExcutor;

import java.util.Date;

public class test {
    public static void main(String[] args) {
        Source source = new Source("娄黔子", "15634917785", 21, 99.9, new Date(), "其他");
        Target target = new Target();
        System.out.println("资源对象的内容:"+source);
        System.out.println("目标对象的内容:"+target);
        CopyExcutor copyExcutor = new CopyExcutor(source, target);
        try {
            copyExcutor.executor();
        } catch (IllegalAccessException e) {
            System.out.println("运行出错,请检查程序");
        }
        System.out.println("=====================工具方法结束============================");
        System.out.println("资源对象的内容:"+source);
        System.out.println("目标对象的内容:"+target);
    }
}

三、测试结果

在这里插入图片描述

在这里插入图片描述
从图中我们可以看到,
资源对象中拥有的属性,“Sname,Snumber,age,score,brithday,selse”
目标对象拥有的属性,
“name,age,score,birthday”
初始给资源对象赋值:
new Source("娄黔子", "15634917785", 21, 99.9, new Date(), "其他")
目标对象为,运行方法后
我们得到了目标对象:
Target{name='娄黔子', age=21, score=99.9, birthday=Tue Apr 27 14:52:05 CST 2021}


http://www.niftyadmin.cn/n/1780308.html

相关文章

前端知识汇总

前端知识入门 还在为前途渺茫而苦恼吗&#xff0c;还在为工资低而感到担忧吗&#xff0c;本章节将带你入门前端有关知识&#xff0c;认识一下html&#xff0c;js&#xff0c;服务框架等如何撑起一个互联网时代&#xff0c;并逐渐取代现在的安卓应用的。 提示&#xff1a;安卓端…

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line

2019独角兽企业重金招聘Python工程师标准>>> json数据解析问题 gson解析时一定要注意要解析的数据是{}&#xff0c;还是[]&#xff0c;另外gson对应的实体类&#xff0c;只能比要解析的gson数据的属性少&#xff0c;不能多 转载于:https://my.oschina.net/u/2483853…

jquery源码解析:attr,prop,attrHooks,propHooks详解

我们先来看一下jQuery中有多少个方法是用来操作元素属性的。 首先&#xff0c;看一下实例方法&#xff1a; 然后&#xff0c;看下静态方法&#xff08;工具方法&#xff09;&#xff1a; 静态方法是内部使用的&#xff0c;我们外面使用的很少&#xff0c;实例方法才是对外的。 …

如何写自定义的AlertView

如何写自定义的AlertView 效果 说明 考虑到后面的可定制性以及可维护性,本人用AbstractAlertView定义了AlertView抽象的基类,实现通过子类来完成. 注:这只是粗略的写了一个实现,并没有考虑到代码的合理性以及精确性. 源码 https://github.com/YouXianMing/UI-Component-Collect…

曲轴位置传感器

http://www.gzweix.com/article/sort0253/sort0487/info-287275.html

JavaScript 显示弹出窗口

window . showModalDialog ( sURL,vArguments , sFeatures )参数说明&#xff1a; sURL--必选参数,用来指定对话框要显示的文档的URL。 //要显示页面的路径vArguments--可选参数&#xff0c;用来向对话框传递参数。传递的参数类型不限&#xff0c;包括数组等。对话框通过window…

Spring中间层

系列文章目录 保姆级别的介绍Spring实现JDBC原理的封装 文章目录系列文章目录一、Spring配置数据源连接池由Spring来管理数据源使用阿里的Druid连接池二、Spring内置JDBC模板引入Spring-jdbc的包Spring引入两种JDBC操作模板模板使用JDBCTemple常用方法修改方法查询方法执行储存…

Nginx学习—反向代理

2019独角兽企业重金招聘Python工程师标准>>> 一、代理分类 代理一般分为正向代理和反向代理。正向代理简单点说就是内部网络通过代理服务器访问外部网络&#xff0c;反向代理就是外部网络通过代理服务器反问内部网络。 nginx就是这种反向代理服务器。 二、nginx的反…