public class Test02 {
public static void main(String[] args) {
Student student = new Student("238");
try {
Student student1 = student.clone();
System.out.println(student.getName());
System.out.println(student1.getName());
System.out.println("是否一样" + (student == student1 ? true : false));
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
//实现Cloneable,不写会抛异常
class Student implements Cloneable {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//方法公开
@Override
public Student clone() throws CloneNotSupportedException {
return (Student) super.clone();
}
}
java 深拷贝实现 重写Object的clone方法
最新推荐文章于 2022-08-10 22:54:55 发布