简体   繁体   English

java中浅层和深层复制演示的示例

[英]examples of shallow and deep copy demonstration in java

// shallow copy example
public class a
{
    public static void main(String[] args) throws CloneNotSupportedException
    {
        b x = new b(2);
        System.out.println(x.i[0]); // previous value for object x
        System.out.println(x.k);
        b y =(b)x.clone(); // y shallow clone of object x
        y.i[0] = 10; y.k = 999; // changed values in object y
        System.out.println(y.i[0]); // values of y after change
        System.out.println(y.k);

        System.out.println(x.i[0]); // values of x after change     
        System.out.println(x.k);
        System.out.println(x.getClass() == y.getClass()); // both objects belong to same class
        System.out.println(x == y); // both objects are different, they are not the same
    }
}

class b implements Cloneable
{
    public int i[] = new int[1];
    int k;

    public Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }

    b(int j)
    {
        super();
        i[0] = j;
        k = j + 2;
    }
}

/*output

2
4
10
999
10
4
true 
false
*/
//*********************************************************************
//deep copy example

public class a
{     
    public static void main(String[] args) throws CloneNotSupportedException 
    {  
        b x = new b(2);
        System.out.println(x.i[0]); // object x values before change
        System.out.println(x.k);

        b y = (b)x.clone(); // deep clone y of object x

        System.out.println(y.i[0]); // values of object y
        System.out.println(y.k);

        System.out.println(x.i[0]); // values of object x after changing the values of the members in object y in clone method  
        System.out.println(x.k);
        System.out.println(x.getClass() == y.getClass());
        System.out.println(x==y);
    }
}

class b implements Cloneable
{
    public int i[] = new int[1];
    int k;

    public Object clone()throws CloneNotSupportedException
    {
        b t = new b(6);
        return t;
    }

    b(int j)
    {
        i[0] = j;
        k = j+2;
    }
}

/*

2
4
6
8
2
4
true 
false
*/

I have already written the examples please see if I left out anything.I have to give a demo regarding it and wanted it to be as simple as possible.Do let me know if I can make it even simpler. 我已经写了这些例子,请看我是否遗漏了任何东西。我必须给出一个关于它的演示,并希望它尽可能简单。如果我能让它变得更简单,请告诉我。 I have provided quotes wherever they changed values or object was being cloned. 我提供了引号,只要它们改变了值或克隆了对象。

A golden tip for demos: use meaningful class names and variable names, and make the examples more concrete. 演示的黄金提示:使用有意义的类名和变量名,并使示例更具体。 Use for example a Book class and an Author class 例如,使用Book类和Author

public class Book{
  private String title;
  private Author author;
...
}
public class Author{
  private String name;
  ...
}

With a getter/setter (or public fields even) you can do a deep/shallow clone of a Book instance, and illustrate what changes when you change the name of the Author . 使用getter / setter(或公共字段),您可以对Book实例进行深度/浅层克隆,并说明更改Author名称时的更改。 Exactly the same as what you did, but easier to tell to an audience, and easier for an audience to follow since everybody knows what a book and an author is and does not need to look at the code to follow your explanation 与您所做的完全相同,但更容易告诉观众,并且观众更容易遵循,因为每个人都知道书籍和作者是什么,并且不需要查看代码来遵循您的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM