这是替换 Java 对象的最佳方法吗?
有时我想用同一类的另一个对象“替换”一个对象。 通常我按以下方式执行此操作。
具有构造函数的对象的类:
public class Type {
private int field;
private double anotherField;
public Type(Type anotherTypeInstance) {
this.field = anotherTypeInstance.getField();
this.anotherField=anotherTypeInstance.getAnotherField();
}
}
因此,每当我想要替换对象时,我只需执行此操作。
Type oldInstance = new Type(newInstance)
有时这样做很容易且方便,而其他人则不然。 还有其他选择吗?
编辑:
我需要优化算法中的这种“替换”。 我必须用成本较小的另一个解决方案替换当前的解决方案(对象)
Sometimes i want to "replace" an object with another object of the same class.
Usually i do this in the following way.
The object's class with a constructor:
public class Type {
private int field;
private double anotherField;
public Type(Type anotherTypeInstance) {
this.field = anotherTypeInstance.getField();
this.anotherField=anotherTypeInstance.getAnotherField();
}
}
So whenever i want to replace the object I simply do this
Type oldInstance = new Type(newInstance)
Sometimes it is easy and convenient to do, while others not.
Is there an alternative?
EDIT:
I need this kind of "replacement" in optimization algorithms.
Where i have to replace the current Solution (object) whith another Solution that has a smaller cost
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
对象.clone()
这是创建现有对象副本的标准方法。但是我不记得最后一次使用
clone()
是什么时候。为什么需要具有完全相同字段的现有对象的副本?您想在不影响“原始”对象的情况下对对象进行更改吗?考虑使用不可变对象。You can use
Object.clone()
which is a standard way of creating a copy of existing object.However I can't remember when was the last time I had to use
clone()
. Why do you need a copy of existing object that has exactly the same fields? Do you want to make changes to object without affecting the "original" one? Consider using immutable objects.您可以实现
Cloneable
接口和clone()
这样的方法如果你确实使用
clone()
来处理更复杂的类,你需要确保所有可变的类的字段引用的对象也会被递归克隆,即进行深复制而不是浅复制。由于浅复制而可能发生的意外状态共享可能会导致难以调试的问题。使用
clone()
有很多问题。它绕过了构造函数,构造函数通常是强制执行类的初始不变量的地方。对于不仅仅包含基本类型的类来说,这是非常容易出错的。此外,使用clone()
与非原始最终字段不兼容,因为在克隆对象后您无法修复它们,以防它们需要修改以确保深度复制。例如,考虑这个类:
像问题中发布的那样的复制构造函数是一种更好的方法。
You can implement
Cloneable
interface and theclone()
method like thisIf you do use
clone()
for more complex classes, you need to make sure that all mutable objects referred to by fields of the class are recursively cloned as well, i.e. that you make a deep copy instead of a shallow copy. Accidental state sharing that can occur as a result of shallow copy can lead to problems which are hard to debug.The use of
clone()
has a number of problems. It circumvents constructors which are normally a place where class's initial invariants are enforced. It is very error prone for classes containing more than just primitive types. Also, the use ofclone()
is incompatible with non-primitive final fields, since you cannot fix them after cloning an object in case they require modification to ensure deep copying.Consider for example this class:
Copy constructors like the one posted in the question are a much better approach.
是的,那很好。更好的方法是:
Yes that is fine. A better way would be: