Object.clone()被保护的原因是什么? [英] What is the reason behind Object.clone() is protected

查看:2547
本文介绍了Object.clone()被保护的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么在java.lang.Object中保护clone()方法?

这是我的检查克隆方法工作的测试代码,

Here is my test code for checking the clone method working,

class Test{
  int a;
  public void setA(int value){
a = value;
  }
  public int getA(){
   return a;
  }
}
class TestClass{   
   public static void main(String args[]){
   Test obj1 = new Test();
   obj1.setA(100);
   Test obj2 = obj1.clone();
   System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());
   obj2.setA(9999);
   System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());
 }
}

抛出编译错误:clone()具有受保护的访问权限java.lang.Object obj1.clone()

Throws compilation error: clone() has protected access in java.lang.Object at obj1.clone()


  1. 我在这里做错什么?

  2. 克隆()被保护的原因是什么?

谢谢

与答案一起编辑最后,当我实现Cloneable界面并覆盖克隆方法时,我看到我的测试工具正在工作。它只是从Object类覆盖clone()方法不起作用。这是修改的代码,

Edit along with Answer: Well at last I see my test harness is working when I implemented the Cloneable interface and overriden the clone method. It's not working with just overriding the clone() method from Object class. Here is the modified code,

class Test implements Cloneable{
 int a;
 public void setA(int value){
a = value;
 }
 public int getA(){
return a;
 }
@Override
protected Test clone() throws CloneNotSupportedException{    
  return(Test) super.clone();  
  }   
}
class TestClass{   
  public static void main(String args[]){
     Test obj1 = new Test();
   obj1.setA(100);
   try{
     Test obj2 = (Test)obj1.clone();
     System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());
     obj2.setA(9999);
     System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());       
   }catch(Exception e){
     System.out.println("ERror"+e);
     }         
    }
  }

2。克隆()方法的原因是保护:我从书籍Core Java中找到这个,

2. Reason for clone() method being protect: I found this from the book Core Java,

克隆方法是Object的受保护方法,这意味着您的代码不能简单叫它。只有Employee类可以克隆Employee对象。

The clone method is a protected method of Object, which means that your code cannot simply call it. Only the Employee class can clone Employee objects.

有一个这个限制的原因。想想Object类可以实现克隆的方式。它完全不知道对象,所以它只能做一个字段拷贝。如果对象中的所有数据字段都是数字或其他基本类型,则复制字段很好。

There is a reason for this restriction. Think about the way in which the Object class can implement clone. It knows nothing about the object at all, so it can make only a field-byfield copy. If all data fields in the object are numbers or other basic types, copying the fields is just fine.

但是,如果对象包含对子对象的引用,则复制该字段你再次引用子对象,所以原始和克隆的对象仍然有一些信息。

But if the object contains references to subobjects, then copying the field gives you another reference to the subobject, so the original and the cloned objects still share some information.

希望对他人有帮助

推荐答案

您应该覆盖Test类中的克隆方法。

You should override the clone method in the Test class.

为什么保护被讨论这里虽然似乎没有一致意见。

Why it is protected is discussed here although there doesn't seem to be a consensus.

这篇关于Object.clone()被保护的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆