gpt4 book ai didi

java - 使用可变字段进行对象克隆

转载 作者:行者123 更新时间:2023-12-01 22:39:07 24 4
gpt4 key购买 nike

我正在研究Object类的clone()方法。我尝试过以下示例。

public class Orange {
private Double price;

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}
}

public class Fruit implements Cloneable {
private String colour;
private String fruitCode;
private Orange orange;

public String getColour() {
return colour;
}

public void setColour(String colour) {
this.colour = colour;
}

public String getFruitCode() {
return fruitCode;
}

public void setFruitCode(String fruitCode) {
this.fruitCode = fruitCode;
}

public Orange getOrange() {
return orange;
}

public void setOrange(Orange orange) {
this.orange = orange;
}

@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}

@Override
public boolean equals(Object obj) {
Fruit other = (Fruit)obj;
return new EqualsBuilder().append(this.getFruitCode(), other.getFruitCode()).append(this.colour, other.colour).isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(this.getFruitCode()).append(this.colour).toHashCode();
}
}
public class CloneTest {
public static void main(String[] args) {
Fruit fruit = new Fruit();
try {
fruit.setOrange(new Orange());
fruit.getOrange().setPrice(12.45);
fruit.setFruitCode("X1");

Fruit clonedFruit = (Fruit) fruit.clone();

fruit.setFruitCode("X2");

fruit.getOrange().setPrice(15.5);

System.out.println("fruit's orange price is : "+fruit.getOrange().getPrice());
System.out.println("clonedFruit's orange price is : "+clonedFruit.getOrange().getPrice());

System.out.println("fruit's code is : "+fruit.getFruitCode());
System.out.println("fruit's code is : "+clonedFruit.getFruitCode());

} catch (CloneNotSupportedException ex) {
Logger.getLogger(CloneTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

**out put is**
fruit's orange price is : 15.5
clonedFruit's orange price is : 15.5
fruit's code is : X2
fruit's code is : X1

请注意,在克隆对象后,我更改了 orange 对象的价格字段和水果的 fruitCode 字段。但我注意到一个问题,即我对橙色对象的价格字段所做的更改已影响到两个对象,即原始对象和克隆对象。 但是水果的fruitCode字段并没有发生这种情况

而且当我输入以下行时,它打印为 true

System.out.println(fruit.getOrange()== clonedFruit.getOrange()); 

我有点困惑,觉得克隆的水果和水果引用都指向同一个橙色对象。当我搜索在线文章时,我注意到可变对象的克隆问题。 但这对我来说还不够清楚。任何人都可以清楚地向我解释这是如何发生的以及我可以采取什么措施来防止这种情况

最佳答案

遗憾的是,默认的 object.clone() 方法并不总是产生实际的克隆。当克隆基本变量时,您会复制一个值,但是,如果您克隆一个对象,它会复制该对象的地址而不是其值。

这就是为什么您通常应该为您的类定义自己的克隆方法(可能有一些语法错误,但逻辑就在那里。

  @Override
protected Object clone() throws CloneNotSupportedException {
Orange orange = new Orange();
//copy every basic variable
orange.setPrice(price);

//clone any Objects that have clone() method defined;
orange.setImage(this.Image.clone()); //just an example because you don't have 1 in your code
// for any objects that do not have clone() defined you have to copy each field manually
GeneticStructure gn = new GeneticStructure();
gn.setGeneticCode( this.geneticStructure.getGeneticCode());
gn.setName( this.geneticStructure.getGeneticStructire());
orange.setGeneticStructure( gn )

return orange;
}

关于java - 使用可变字段进行对象克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26448345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com