📄 clone.java
字号:
//演示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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -