拷贝分为两种:
public class QuoteCopy {
public static void main(String[] args) {
Teacher teacher = new Teacher("riemann", 28);
Teacher otherTeacher = teacher;
System.out.println(teacher);
System.out.println(otherTeacher);
}
}
class Teacher {
private String name;
private int age;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7dd59c05f027b9384604737649f2fa0d.png)
public class ObjectCopy {
public static void main(String[] args) throws CloneNotSupportedException {
Teacher teacher = new Teacher("riemann", 28);
Teacher otherTeacher = (Teacher) teacher.clone();
System.out.println(teacher);
System.out.println(otherTeacher);
}
}
class Teacher implements Cloneable {
private String name;
private int age;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
public Object clone() throws CloneNotSupportedException {
Object object = super.clone();
return object;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b069c1bbef72a180c8451ce8e13f002f.png)