好久没有写博客了感觉自己好多的知识点都有点模糊了哎!
最近几天在看alibaba的cloud,也不知道怎么的就想起了Java中的GC又联想到Java的传递方式(值传递,引用传递),在后来就到了对象的copy,对象的copy一般通过Object类的clone()方法来实现,而想要实现这个方法有两个必要条件。
1.要继承Object类
我们的所有类默认继承Object类。
2.实现Cloneable接口
Cloneable接口实际上相当与一个标记,用来告诉编译器这个类可以调用Object的clone()方法,如果不实现Cloneable接口就会出现Unhandled exception:java.lang.CloneNotSupportedException异常。
我的实现
public class Person implements Cloneable {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person(20, "xioahua");
Person person2 = (Person) person1.clone();
person1.setAge(22);
System.out.println(person1.toString());
System.out.println(person2.toString());
}
}
结果
Person{age=22, name='xioahua'}
Person{age=20, name='xioahua'}
这里的结果可以看出person1和person2是两个不同的对象但这样也仅仅是浅拷贝并且只能在该类中使用因为clone()只会拷贝对象中的属性,而当类中的属性是一个对象引用时拷贝的也仅仅是这个对象的引用而非拷贝的对象,并且clone()方法时protected修饰的方法,只能在同一包内或继承关系中使用,而想要在其他的类中调用就需要通过重写来扩大访问权限,从而使得clone()方法可以在其他类中使用。
以上结论中可以得到两个关键问题
1.如何实现深度拷贝
2.如何扩大权限
想要实现深度拷贝我们就需要重写clone()方法在方法中做一些处理来实现对象的深度拷贝,而扩大权限只需要将clone()方法的权限修改为public就可以实现在其他类中使用,如果你只想在同一个包下使用就不需要修改权限了。
示例
class Student implements Cloneable {
private String name;
private int age;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// @Override
// public Student clone() throws CloneNotSupportedException {
// return (Student) super.clone();
// }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student(10,"hah");
Student student2 = (Student) student1.clone();
student1.setAge(20);
System.out.println(student1);
System.out.println(student2);
}
}
这里我没有重写Clone()方法,在本类中可以调用clone()。
下面我在另一个类中调用clone()方法就会报错。
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person(20, "xioahua");
Person person2 = (Person) person1.clone();
person1.setAge(22);
//这里的clone方法会报错。
person1.setStudent(person1.getStudent().clone());
System.out.println(person1.toString());
System.out.println(person2.toString());
}
clone()' has protected access in 'java.lang.Object'
因为我在Student中没有重写clone()方法,所以要想在其他的类中调用clone()方法就必须重写clone()方法。
深拷贝的实现需要在重写的clone方法中进行处理。
public class Person implements Cloneable {
private int age;
private String name;
private Student student;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
", student=" + student +
'}';
}
@Override
public Person clone() throws CloneNotSupportedException {
Person person = (Person) super.clone();
// person.setStudent(this.student.clone());
return person;
}
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person(20, "xioahua");
person1.setStudent(new Student(10,"xiaotian"));
Person person2 = (Person) person1.clone();
person1.setAge(22);
person1.getStudent().setAge(20);
System.out.println(person1.toString());
System.out.println(person2.toString());
}
}
结果
Person{age=22, name='xioahua', student=Student{name='xiaotian', age=20}}
Person{age=20, name='xioahua', student=Student{name='xiaotian', age=20}}
结果中可以看出如果不再clone()方法中做处理只能实现浅拷贝。
如果在clone()方法中加入处理语句,结果如下:
Person{age=22, name='xioahua', student=Student{name='xiaotian', age=20}}
Person{age=20, name='xioahua', student=Student{name='xiaotian', age=10}}
没事还是要多写写,既可以分享给大家,还能加深记忆。