java 将对象复制到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2536849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy object into another
提问by EugeneP
Is there a generic way to achieve copying an existing object into another?
有没有通用的方法来实现将现有对象复制到另一个对象?
Assume MyObjhas an idand namefields. Like this:
假设MyObj有一个id和name字段。像这样:
MyObj myObj_1 = new MyObj(1, "Name 1");
MyObj myObj_2 = new MyObj(2, "Name 2");
Instead of
代替
myObj_2.setName(myObj_1.getName()) // etc for each field
do something as following:
执行以下操作:
myObj_2.copyFrom(myObj_1)
so that they are different instances, but have equal properties.
所以它们是不同的实例,但具有相同的属性。
回答by Jim Blackler
The convention is to do this at construction time with a constructor that takes one parameter of its own type.
约定是在构造时使用一个接受其自身类型参数的构造函数来执行此操作。
MyObj myObj_2 = new MyObj(myObj_1);
MyObj myObj_2 = new MyObj(myObj_1);
There is no Java convention to overwrite the existing properties of an object from another. This tends to go against the preference for immutable objects in Java (where properties are set at construction time unless there is a good reason not to).
Java 没有约定从另一个对象覆盖一个对象的现有属性。这往往与 Java 中对不可变对象的偏好背道而驰(除非有充分的理由,否则在构造时设置属性)。
Edit: regarding clone(), many engineers discourage this in modern Java because it has outdated syntax and other drawbacks. http://www.javapractices.com/topic/TopicAction.do?Id=71
编辑:关于 clone(),许多工程师在现代 Java 中不鼓励这样做,因为它具有过时的语法和其他缺点。http://www.javapractices.com/topic/TopicAction.do?Id=71
回答by Boris Pavlovi?
Use copy constructor:
使用复制构造函数:
public class YourObject {
private String name;
private int age;
public YourObject(YourObject other) {
this.name = other.name;
this.age = other.age;
}
}
回答by Maxime ARNSTAMM
回答by Dishayloo
The clone()-method is for exactly this job.
clone() 方法正是为了这个工作。
回答by codymanix
You can use introspection to automate the implementation of your clone routines, so you can be safe you do not forget to copy some fields.
您可以使用自省来自动执行您的克隆例程,因此您不会忘记复制某些字段,从而确保安全。
回答by MANTU KUMAR
The clone()method is best suited for these requirements. Whenever the clone()method is called on an object, the JVM will actually create a new object and copy all the content of previous object into the newly created object. Before using the clone()method you have to implement the Cloneableinterface and override the clone()method.
该clone()方法最适合这些要求。每当在clone()对象上调用该方法时,JVM 实际上会创建一个新对象,并将之前对象的所有内容复制到新创建的对象中。在使用该clone()方法之前,您必须实现该Cloneable接口并覆盖该clone()方法。
public class CloneExample implements Cloneable
{
int id;
String name;
CloneExample(int id, String name) {
this.id = id;
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) {
CloneExample obj1 = new CloneExample(1,"Name_1");
try {
CloneExample obj2 = (CloneExample) obj1.clone();
System.out.println(obj2.id);
System.out.println(obj2.name);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}

