问题
解答
- 克隆是创建对象的方式之一,通过克隆可以将原有的对象完全克隆出一个新的对象且该对象的所有属性被复制过来。在对象比较复杂的时候,克隆为一种效率高的方式。
- 对象克隆的方法
- 被克隆的类要覆写Object的clone,并改为public
- 被克隆的类要实现Cloneable接口
例子:
class Student implements Cloneable{
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
//此处将clone改为public类型
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneApp {
public static void main(String[] args) throws CloneNotSupportedException {
Student s1 = new Student("Tom",20);
Student s2 = (Student)s1.clone();
System.out.println(s2.getName());
System.out.println(s2.getAge());
}
}
- 浅拷贝
默认被克隆的对象中的属性中有其他对象的引用的时候,不会把该引用的对象也克隆,导致了只会克隆该对象,而不会克隆该对象的属性的引用。
class Score{
private int english ;
private int math;
public Score(int english, int math) {
this.english = english;
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
}
class Student implements Cloneable{
private String name;
private int age;
private Score score;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
public Student(String name, int age, Score score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class CloneApp {
public static void main(String[] args) throws CloneNotSupportedException {;
Score score = new Score(100,80);
Student s1 = new Student("Tom",20,score);
Student s2 = (Student)s1.clone();
System.out.println(s2.getName());
System.out.println(s2.getAge());
//s1的score的属性被修改了,其实s1和s2持有的是同一个对象。
s2.getScore().setEnglish(50);
s2.getScore().setMath(70);
System.out.println("===s1的score===");
System.out.println("english="+s1.getScore().getEnglish());
System.out.println("math="+s1.getScore().getMath());
}
}
- 深拷贝
深拷贝就是讲被克隆的对象引用的其他对象也克隆了。实现方法如下
class Score implements Cloneable{
private int english ;
private int math;
public Score(int english, int math) {
this.english = english;
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
@Override
public Object clone () throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable{
private String name;
private int age;
private Score score;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
public Student(String name, int age, Score score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public Object clone() throws CloneNotSupportedException {
Student s = (Student)super.clone();
//把Score克隆了
s.score = (Score) score.clone();
return s;
}
}
public class CloneApp {
public static void main(String[] args) throws CloneNotSupportedException {;
Score score = new Score(100,80);
Student s1 = new Student("Tom",20,score);
Student s2 = (Student)s1.clone();
System.out.println(s2.getName());
System.out.println(s2.getAge());
s2.getScore().setEnglish(50);
s2.getScore().setMath(70);
System.out.println("===s1的score===");
System.out.println("english="+s1.getScore().getEnglish());
System.out.println("math="+s1.getScore().getMath());
System.out.println("===s2的score===");
System.out.println("english="+s2.getScore().getEnglish());
System.out.println("math="+s2.getScore().getMath());
}
}