62,620
社区成员
发帖
与我相关
我的任务
分享
import javax.swing.*;
import java.util.*;
import java.math.*;
import java.util.Date;
public class CloneTest extends Object
{
public static void main(String[] args)
{
Employee e1 = new Employee();
Employee e2 = e1;
e1.SetName("ff");
e1.SetName("gg");
System.out.println( e2.GetName() );
}
}
class Employee implements Cloneable
{
private String name;
public void SetName(String a)
{
this.name = a;
}
public String GetName()
{
return this.name;
}
public Employee clone() throws CloneNotSupportedException
{
Employee cloned = (Employee)super.clone();
cloned.name = (String) name.clone();//Eclipse提示这个clone函数不可视
return cloned;
}
}
public Employee clone() throws CloneNotSupportedException
{
Employee cloned = (Employee)super.clone();
cloned.name = new String(name);//这样就构成了一个新串,String没有实现Cloneable不能被克隆
return cloned;
}
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
String abc = "abc";
String xyz = new String(abc);
System.out.println(xyz);