2、然后创建一个具体原型类
public class ConcretePrototype extends ProtoType {
private String name; //成员变量
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public ProtoType clone() {
ProtoType protoType = new ConcretePrototype(); //创建新对象
((ConcretePrototype) protoType).setName(this.name);
return protoType;
}
}
3、然后创建一个客户类
public class Client {
public static void main(String[] args) {
ConcretePrototype prototype = new ConcretePrototype();
prototype.setName(“孙悟空”);
ConcretePrototype clone = (ConcretePrototype)prototype.clone();
System.out.println(clone.getName());
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/fb0ed4bf083f45eeb9d7bbdb46a3dd94.png)
**面试官:你这个是自己写的克隆方法,可以使用java中自带的克隆方法吗?**
我:在java中Object类提供了一个clone()方法,可以将一个java对象复制一份,因此可以使用clone()方法来实现对象的克隆,不过这里要注意,这个方法只能实现浅克隆。而且要使用这个方法就需要实现Cloneable接口。
1、具体克隆类实现Cloneable接口
public class ConcretePrototype implements Cloneable{
@Override
public Object clone() {
Object object = null;
try {
object = super.clone(); //浅克隆
}catch (CloneNotSupportedException exception){
System.err.println(“Not support cloneable”);
}
return object;
}
}
2、客户类用来测试是否能够克隆
public class Client {
public static void main(String[] args) {
ConcretePrototype prototype = new ConcretePrototype();
ConcretePrototype copy = (ConcretePrototype)prototype.clone();
System.out.println(prototype == copy);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/608b3823bddd403985ecf4a08921ecb4.png)
**面试官:你上面说的是浅克隆的方法,那么我想要深克隆怎么办呢?**
我:深克隆需要实现Serialization序列化方式来实现深克隆。序列化就是将对象写到流的过程,写到流中的对象是原有对象的一个复制,而原对象仍然存在于内存中。通过序列化的方式不仅可以复制基础类型,还可以复制引用类型。
**面试官:可以简单写一个深克隆的例子吗?**
1、首先定义一个学生实体。
public class Student implements Serializable {
private String name;
private int 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;
}
}
2、然后定义一个老师实体,其中有自己写的深克隆方法
public class Teacher implements Serializable {
private Student student;
private String name;
private String add;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
//在对象中自定义克隆的方法
public Teacher deepClone() throws IOException,ClassNotFoundException, OptionalDataException {
//将对象写入流中
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(this);
//将对象从流中取出
ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (Teacher) ois.readObject();
}
}
3、编写测试类
public class ClientDeep {
public static void main(String[] args) {
Teacher t_proto,t_new = null;
t_proto = new Teacher(); //创建原型对象
Student student = new Student(); //创建学生对象
t_proto.setStudent(student); //将学生添加到老师中
try {
t_new = t_proto.deepClone(); //调用深克隆方法创建克隆对象
}catch (Exception e){
System.err.println(“克隆失败!”);
}
//比较两个老师对象
System.out.println(“老师对象是否相同:”+ (t_proto == t_new));
//比较老师中的两个学生是否相同
System.out.println(“学生对象是否相同:”+ (t_proto.getStudent() == t_new.getStudent()));
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/77cd9e3f374e42fd8a55ec98a461933b.png)
![img](https://img-blog.csdnimg.cn/img_convert/2c64c2e26ff39bd68dc9e011435a9113.png)
![img](https://img-blog.csdnimg.cn/img_convert/0abb12cc0505ae3240dbecb72e53b4a6.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**