问一个对象clone()的问题
代码如下:
class StringTest { /** * @param args */ public static void change(int x,int y) { Professor p = new Professor("wangwu",50); Student s1 = new Student("zhangsan",18,p); Student s2 = (Student)s1.clone(); s2.p.name = "lisi"; s2.p.age = 30; System.out.print("name = "+s1.p.name+",age = "+s1.p.age); }}class Professor implements Cloneable{ String name; int age; Professor(String name,int age) { this.name = name; this.age = age; } public Object clone() { Object o = null; try { o = super.clone(); } catch (CloneNotSupportedException ce) { System.out.println(ce.toString()); } return o; }}class Student implements Cloneable{ String name; int age; Professor p; Student(String name,int age,Professor p) { this.name = name; this.age = age; this.p = p; } public Object clone() { Student o = null; try { o = (Student)super.clone(); } catch (CloneNotSupportedException ce) { System.out.println(ce.toString()); } o.p = (Professor)p.clone(); return o; }}