[英]How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java
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. }
You do not copy the object.您不要复制 object。 Your code does what you want.你的代码做你想做的事。
(It is pretty difficult to accidentally copy something in Java.) (在 Java 中意外复制某些内容非常困难。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.