?? clone.java
字號(hào):
//演示clone()方法。
class CloneTest
{
public static void main(String[] args)
{
Professor p = new Professor("wangwu", 50);
Student s1 = new Student("zhangshan", 18, p);
Student s2;
s2 = (Student)s1.clone();
s2.name = "lisi";
s2.age = 20;
s2.p.name = "kkk";
s2.p.age = 30;
System.out.println(s1);
System.out.println(s2);
}
}
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 ex)
{
ex.printStackTrace();
}
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 String toString()
{
return "Student' Informatioin "+ name + ", " + age
+"\nProfessor's Information " + p.name + "," + p.age;
}
public Object clone()
{
Student o = null;
try
{
o = (Student)super.clone();
}
catch(CloneNotSupportedException ex)
{
ex.printStackTrace();
}
o.p = (Professor)p.clone();
return o;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -