简体   繁体   English

Java:将属性从一个对象实例复制到另一个对象实例

[英]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 . enginecar2将是相同的对象的引用作为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 of car1.getEngine() and set to car2 ? 当我执行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.

相关问题 如果所有属性的值不为 null 或为空,如何将所有属性的值从对象的一个​​实例复制到另一个实例 - How to copy values of all attributes from one instance of an object to another if they are not null or empty 从一个实例到另一个线程的非零/非空属性的另一个副本,嵌套方法/Java - Yet another copy of non-zero/non-null attributes from one instance to another thread, nested approach / Java 将大多数属性从一个 class object 复制到另一个 class - Copy most attributes from one class object to another class Java中如何将文件属性从一个文件复制到另一个文件? - How to copy file attributes from one file to another in Java? JAVA - 如何将对象的属性复制到具有相同属性的另一个对象? - JAVA - How to copy attributes of an object to another object having the same attributes? 将一个 Java 对象复制到另一个对象 - Copy one Java object to another 将 java 对象/类从一个类加载器复制到另一个类加载器 - Copy java object/class from one classloader to another classloader 如何在Java中将对象从一种类型复制到另一种类型? - How to copy an object from one type to another in Java? 将属性白名单从一个类的实例复制到另一个实例 - Copy whitelist of attributes from class one instance to the other 从一个 object 复制 not null and not empty fields 到 java 中的另一个 object 相同类型(对象是相同类型) - Copy not null and not empty fields from one object to another object of the same type(Objects are same type) in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM