English 中文(简体)
深克隆的克隆体?
原标题:Clone() body for deep cloning?
  • 时间:2012-05-24 00:29:00
  •  标签:
  • java

我受了很久的苦

告诉我下面的代码是否克隆了?

class A
{  
 int i;
    int j;  
    String str;  
    A()  
    {  
      i=10;  
      j=30;  
      str="Hello";  
    }  
    A(A a)  
   {  
      this.i=a.i;  
      this.j=a.j;  
      this.str=a.str;  
   }  
}  
class B
{  
   public static void main(String args[])  
   {
      A a  = new A();  
      A a1 = new A(a);
      /* I want to make clone like this. */  
      } 
}

when I run this code and when I print hashcode of a and a1, they are different. But some of my friends say that this is not the correct way to make a clone. You have to implement the Cloneable interface, is that really necessary? In my opinion, it can be a good approach if I want to make a deep copy even in case of derived reference variable. Thank you.

最佳回答

您需要安装可克隆界面才能克隆对象。 您所执行的是一个复制构建器。 复制构建器比执行可克隆更可取 。

您复制对象与被复制对象有不同的散列码/等值, 原因是您没有翻过散列码或等同 A类中的函数, 所以它检查身份, 而不是只检查平等( exact 相同对象, 而不是具有相同值的对象) 。 通过压过散列码/ 等, 您可以使您的类比较其属性的值 。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签