当前位置: 首页 > 知识库问答 >
问题:

对象数组实例的深度复制,Java

姬凡
2023-03-14
public class Recipe implements Cloneable{

String Name;

final int INGREDIENT_ARRAY_MAX = 10;

Ingredient Recipe[] = new Ingredient[INGREDIENT_ARRAY_MAX];

public Recipe(String name){

    Name = name;

}
public Object clone(){

    Recipe cloneRec = new Recipe(Name);

    return cloneRec;

}

编辑:@Rohit Jain

我的Recipe类和Ingreatent类(Recipe数组保存的对象)都有toString方法和recipes对Ingreats的调用,以便以一个漂亮的小格式将其全部打印出来。当我在我的“recipeone”对象(即意大利辣香肠披萨)上调用它时,我得到的是“意大利辣香肠披萨:1.0磅面团,8.0盎司酱汁,10.0盎司奶酪”。

然后我继续制作对象ressippi并将其设置为recipeOne的克隆,所以从这里开始一切都很好...然后我将ressippi的名字改为“菠萝披萨”,打印出来很好,但它没有打印recipeOne存储的3个成分对象,这是它应该做的!

共有1个答案

杨景山
2023-03-14

向recipe类添加一个copy构造函数,它创建一个新的recipe实例,并从原始recipe复制所有字段。

Recipe.java

public class Recipe implements Cloneable {

    String name;

    final int INGREDIENT_ARRAY_MAX = 10;

    Ingredient[] ingredients = new Ingredient[INGREDIENT_ARRAY_MAX];

    public Recipe(String name) {
        this.name = name;
    }

    //Copy Constructor
    private Recipe(Recipe recipe){
        this.name = recipe.name;
        for(int x = 0; x < recipe.ingredients.length; x++){
            this.ingredients[x] = recipe.ingredients[x];
        }
    }

    public static Recipe newInstance(Recipe recipe){
        return new Recipe(recipe);
    }

    //Debug Method
    public static void printRecipe(Recipe recipe){
        System.out.println("Recipe: " + recipe.name);
        for(Ingredient i:recipe.ingredients){
          if(i != null && i.getName() != null){
              System.out.println("Ingredient: " + i.getName());           
          }
        }
    }

    //Test Method
    public static void main(String[] args) {
        Recipe recipe = new Recipe("Chicken Soup");
        recipe.ingredients[0] = new Ingredient("Chicken");
        recipe.ingredients[1] = new Ingredient("Broth");

        Recipe copy = new Recipe(recipe);
        copy.ingredients[2] = new Ingredient("Rice");
        copy.name = "Chicken Rice Soup";

        printRecipe(recipe);
        printRecipe(copy);
        System.out.println(recipe == copy);
        System.out.println(recipe.ingredients == copy.ingredients);
    }
}

成分.java

public class Ingredient {

    private String name;

    public Ingredient(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }
}
 类似资料:
  • 本文向大家介绍Java数组集合的深度复制代码实例,包括了Java数组集合的深度复制代码实例的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了Java数组集合的深度复制代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Java当我们想要对一个数组进行一些操作,同时又不希望对原来的数组数据有影响的时候,使用引用是不能满足我们的需求的,

  • 问题 你想复制一个对象,包含其所有子对象。 解决方案 clone = (obj) -> if not obj? or typeof obj isnt 'object' return obj if obj instanceof Date return new Date(obj.getTime()) if obj instanceof RegExp flags

  • 问题内容: 我想知道是否需要避免在创建一个带有嵌入式对象数组的简单对象时复制对对象的引用。情况如下:我有一个接受JSON并应用一些逻辑然后将对象存储在其中的服务器D B。可以说,我的表单用于保存DB中的团队。服务器接受team作为json。团队有一个TeamMember对象数组,我的表单有一个简单的字段来输入团队成员信息并将其添加到团队teamMembers数组中。现在这是一个问题,当我向数组列表

  • 问题内容: 现在并包含相同的日期- 从现在起三年。我想创建两个单独的日期时间,其中一个是从字符串中解析出来的,另一个是添加了三年的时间。目前,我已经将其修改为: 但这似乎是一个可怕的骇客。有没有“正确”的方法来深度复制DateTime对象? 问题答案: 更新: 如果要复制而不是引用现有的DT对象,请使用,而不是。

  • 本文向大家介绍JavaScript 数组的深度复制解析,包括了JavaScript 数组的深度复制解析的使用技巧和注意事项,需要的朋友参考一下 对于javascript而言,数组是引用类型,如果要想复制一个数组就要动脑袋想想了,因为包括concat、slice在内的函数,都是浅层复制。也就是说,对于一个二维数组来说,用concat来做复制,第二维的数组还是引用,修改了新数组同样会使旧数组发生改变。

  • 本文向大家介绍php5对象复制、clone、浅复制与深复制实例详解,包括了php5对象复制、clone、浅复制与深复制实例详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了php5对象复制、clone、浅复制与深复制。分享给大家供大家参考,具体如下: 对象复制的由来 为什么对象会有“复制”这个概念,这与PHP5中对象的传值方式是密切相关的,让我们看看下面这段简单的代码 PHP代码 这段代