无法理解克隆

发布于 2024-11-18 23:50:43 字数 384 浏览 8 评论 0原文

我有一个简单的程序来克隆一个对象,我用谷歌搜索了错误“线程“main”java.lang.CloneNotSupportedException中的异常:”,但需要您的帮助来理解该错误,为什么我无法克隆 obj1?

public class Test{
int a;
int b;
public Test(int a , int b){
    this.a=a;
    this.b=b;
    }
public static void main(String[]args) throws CloneNotSupportedException{
    Test obj1=new Test(2, 4);
    Test obj2=(Test) obj1.clone();
       }
 }

I have a simple program to clone a object , I googled the error "Exception in thread "main" java.lang.CloneNotSupportedException:" but need your help to understand the error, why am I not able to get clone of obj1?

public class Test{
int a;
int b;
public Test(int a , int b){
    this.a=a;
    this.b=b;
    }
public static void main(String[]args) throws CloneNotSupportedException{
    Test obj1=new Test(2, 4);
    Test obj2=(Test) obj1.clone();
       }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-11-25 23:50:43

出现此问题的原因是 Test 类未实现 Cloneable 接口。如 API 规范<中所述/a>,

如果类[..]没有实现Cloneable接口,则会抛出CloneNotSupportedException。

要修复此问题,请尝试以下操作:

public class Test implements Cloneable { 
  ...
}

由于 Cloneable 接口未声明任何方法(它称为 marker 接口,就像 Serialized 一样),因此仅此而已去做。现在可以克隆 Test 类的实例。

但是,默认的克隆机制(即 Object 的克隆机制)可能并不完全是您正在寻找的,并且您可能想要重写 clone() 方法。默认情况下是进行浅拷贝,也就是说,您将获得类的一个新的、不同的实例,但两个实例的字段将引用相同的对象!例如:

class C1 {
  Object o;
}
class C2 implements Cloneable {
  C1 c1;
}

... main ... {
  C2 c2 = new C2();
  c2.c1 = new C1();
  c2.c1.o = new Object();
  C2 c2clone = c2.clone();
  System.out.println(c2 == c2clone); // prints false
  System.out.println(c2.c1 == c2clone.c1); // prints true
  c2.c1.o = new Object(); // modified both c2 and c2clone!!!

最后一行将修改 c2 和 c2clone,因为它们都指向 c1 的同一个实例。如果您希望最后一行仅修改 c2,那么您必须进行我们所说的深复制。

The problem occurs because the class Test doesn't implement the Cloneable interface. As stated in the API specs,

if the class [..] does not implement the interface Cloneable, then a CloneNotSupportedException is thrown.

To fix, try something like:

public class Test implements Cloneable { 
  ...
}

Since the Cloneable interface declares no methods (it's called a marker interface, just like Serializable), there's nothing more to do. The instances of your Test class can now be cloned.

However, the default cloning mechanism (ie, that of Object) might not be exactly what you are looking for, and you might want to override the clone() method. The default is to make a shallow copy, that is, you will get a new, distinct instance of your class, but the fields of both instances will refer to the same objects! For example:

class C1 {
  Object o;
}
class C2 implements Cloneable {
  C1 c1;
}

... main ... {
  C2 c2 = new C2();
  c2.c1 = new C1();
  c2.c1.o = new Object();
  C2 c2clone = c2.clone();
  System.out.println(c2 == c2clone); // prints false
  System.out.println(c2.c1 == c2clone.c1); // prints true
  c2.c1.o = new Object(); // modified both c2 and c2clone!!!

The last line will modify both c2 and c2clone because both point to the same instance of c1. If you want the last line to modify only c2, then you have to make what we call a deep copy.

梦太阳 2024-11-25 23:50:43

你必须实现 Cloneable。这是一个标记界面。

你的程序应该是

    public class Test implements Cloneable{
         //rest of the program
    }

You have to implement Cloneable. It's a marker interface.

Your program should be

    public class Test implements Cloneable{
         //rest of the program
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文