java 如何将一个对象转换成另一个对象

本文介绍了在Java中通过构造函数、类型转换、方法调用、工厂方法以及使用Dozer等工具进行对象转换的五种方式,包括基于继承的关系转换和处理复杂转换场景的策略。
摘要由CSDN通过智能技术生成

1. 使用构造函数

如果目标类有从源对象构造新实例的构造函数,可以直接使用构造函数进行转换。

// 源对象
public class Source {
    private String name;
    private String code;

    public String getName() {
        return name;
    }

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

// 目标类
public class Target {
    private String name;
    private Integer num;

    public Target(Source source) {
        // 转换逻辑
        this.name = source.getName();
    }

    public String getName() {
        return name;
    }

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

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}

// 测试类
public class Test1 {
    public static void main(String[] args) {
        constructionMethod();
    }

    public static void constructionMethod(){
        Source source = new Source();
        source.setName("11");
        source.setCode("22");
        Target target = new Target(source);
        System.out.println(target.getName());
    }
}

2. 类型转换

如果两个类具有继承关系,可以直接进行类型转换。

// 父类
public class Source {
    private String name;
    private String code;

    public String getName() {
        return name;
    }

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

// 子类
public class Source1 extends Source{
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

// 测试类
public class Test1 {
    public static void main(String[] args) {
        typeChange();
    }

    public static void typeChange(){
        Source source = new Source1();
        source.setName("11");
        source.setCode("22");
        Source1 source1 = (Source1) source;
        System.out.println(source1.getName());
        System.out.println(source1.getCode());
        System.out.println(source1.getType());
    }
}

3. 使用方法

有些时候可以调用目标类的方法将源对象转换为目标对象。

// 源对象
public class Source {
    private String name;
    private String code;

    public String getName() {
        return name;
    }

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

// 目标对象
public class Target {
    private String name;
    private Integer num;
    
    public static Target convert(Source source) {
        Target target = new Target();
        // 转换逻辑
        target.setName(source.getName());
        return target;
    }

    public String getName() {
        return name;
    }

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

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}



// 测试类
public class Test1 {
    public static void main(String[] args) {
        convert();
    }
    
    public static void convert(){
        Source source = new Source();
        source.setName("11");
        source.setCode("22");
        Target target = Target.convert(source);
        System.out.println(target.getName());
    }
}

4. 使用工厂方法或构建器

如果转换过程复杂,可以使用专门的转换类或方法来封装转换逻辑。

// 源对象
public class Source {
    private String name;
    private String code;

    public String getName() {
        return name;
    }

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

// 目标类
public class Target {
    private String name;
    private Integer num;

    public String getName() {
        return name;
    }

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

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}

// 转换类
public class Converter {
    public static Target convert(Source source) {
        Target target = new Target();
        // 转换逻辑
        target.setName(source.getName());
        return target;
    }
}

// 测试类
public class Test1 {
    public static void main(String[] args) {
        convert();
    }

    public static void convert(){
        Source source = new Source();
        source.setName("11");
        source.setCode("22");
        Target target = Converter.convert(source);
        System.out.println(target.getName());
    }


}

5. 使用自定义转换器

对于复杂的对象转换,可以使用像 Dozer 或 ModelMapper 这样的转换框架。

  • 导入Dozer的依赖和配置
<dependency>
    <groupId>net.sf.dozer</groupId>
    <artifactId>dozer</artifactId>
    <version>5.5.1</version>
</dependency>
// 假设已有Dozer的依赖和配置
Mapper mapper = DozerBeanMapperBuilder.buildDefault();
 
class Source {
    // ...
}
 
class Target {
    // ...
}
 
Source source = new Source();
Target target = mapper.map(source, Target.class);  //第一个参数是源对象,第二个参数是目标对象
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值