我受了很久的苦
告诉我下面的代码是否克隆了?
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.