[英]Java: Copy attributes from one object instance to another?
Say you have 说你有
public class Car{
private Engine m_eng;
public Car(){
}
public Engine getEngine(){
return m_eng;
}
public void setEngine(Engine engine){
m_eng = engine;
}
}
public class Engine{
private String m_name;
public Engine(){};
public Engine(String name){ m_name = name;}
public String getName(){
return m_name;
}
public void setName(String name){
m_name = name;
}
}
public static void main(String[] args){
Engine eng1 = new Engine("abc");
Car car1 = new Car();
car1.setEngine(eng1);
Car car2 = new Car();
car2.setEngine(car1.getEngine());
}
Question: are the engine of car1 and car2 referring to the same instance of Engine, or when I do car2.setEngine(car1.getEngine())
, it automatically make a deep copy of car1.getEnginer() and set to car2 ? 问题:car1和car2的引擎是引用相同的Engine实例,还是当我执行
car2.setEngine(car1.getEngine())
,它会自动生成car1.getEnginer()的深层副本并设置为car2?
car1--------------->eng1 CAR 1 ---------------> ENG1
car2.setEngine(car1.getEngine());
results in 结果是
car1--------------->eng1 <------------------car2 car1 ---------------> eng1 <------------------ car2
thereby pointing to same engine instance 从而指向相同的引擎实例
是的,确定这些将是同一个实例。
There is no deep copy. 没有深刻的副本。 Both
Car
instances reference the same instance of Engine
. 两个
Car
实例都引用相同的Engine
实例。
As noted by other people, when you do 正如其他人所说,当你这样做时
car2.setEngine(car1.getEngine())
The engine
in car2
will be the same object reference as in car1
. 该
engine
在car2
将是相同的对象的引用作为car1
。 This is easily tested by using ==
: 使用
==
可以轻松测试
System.out.println(car2.getEngine() == car1.getEngine()); //prints "true"
when I do
car2.setEngine(car1.getEngine())
it automatically make a deep copy ofcar1.getEngine()
and set tocar2
?当我执行
car2.setEngine(car1.getEngine())
它会自动生成car1.getEngine()
的深层副本并设置为car2
?
Be careful here, since when executing that statement there's no copy of the object reference , it is not a deep copy nor a shallow copy , it is the same object reference . 这里要小心,因为在执行该语句时没有对象引用的副本 ,它不是深拷贝也不是浅拷贝 ,它是相同的对象引用 。 This means, if you modify the state of the engine in one of the cars, then the engine in the other car gets modified (since is the same object reference):
这意味着,如果您修改其中一辆车的引擎状态,那么另一辆车中的引擎会被修改(因为它是相同的对象引用):
public static void main(String[] args){
Engine eng1 = new Engine("abc");
Car car1 = new Car();
car1.setEngine(eng1);
Car car2 = new Car();
car2.setEngine(car1.getEngine());
//additional code to show the last statement
car2.getEngine().setName("foo");
System.out.println(car2.getEngine().getName()); //prints "foo"
System.out.println(car1.getEngine().getName()); //prints "foo" too
System.out.println(eng1.getName()); //prints "foo" since it is the same object reference used from the beginning
}
Check here to know about how to make copies of object references: Java: recommended solution for deep cloning/copying an instance 请在此处了解如何制作对象引用的副本: Java:深度克隆/复制实例的推荐解决方案
There is no deep copy. 没有深刻的副本。 both reference are refering to same object try to use == operator to compare two objects.
两个引用都引用同一个对象尝试使用==运算符来比较两个对象。
Engine eng1 = new Engine("abc");
Car car1 = new Car();
car1.setEngine(eng1); //here you have set the reference eng1 which is pointing to the object abc in heap
Car car2 = new Car();
car2.setEngine(car1.getEngine());// here you are getting the reference of the object which is in the heap and setting it in car2 Object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.