如何将现有类对象复制到其他类新类对象,而不在java中创建原始对象(内存中)的副本

l7wslrjt  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(289)

请告知。我认为aobj只有一个副本,除非我们在新对象中添加任何更改或更新最初创建的对象。不确定,请帮忙。

public class AObject {
    private String a;
    Private String b;

    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }
    public void setB(String b) {
        this.b = b;
    }
}
}
public class BObject{
    private AObject aObj;
    public AObject getAObj() {
        return aObj;
    }
    public void setAObj(AObject aObj) {
        this.aObj = aObj;
    }
}
public static void main(String[] args){
    AObject aObj = new AObject();
    aObj.setA("testA");
    aObj.setB("testB");

    BObject bObj = new BObject();
    bObj.setAObj(aObj) //here I would like to avoid creating the deep copy of objects.
}
yyhrrdl8

yyhrrdl81#

不复制对象。你的代码做你想做的。
(在java中意外地复制某些内容是相当困难的。)

相关问题