一.java实现对象的克隆有两种方式
1.继承object类重写clone方法;
假如我现在要克隆一个学生对象,该对象拥有一些属性,例如:姓名;首先创建学生类
package com.onekeyop.sop.bingfa;
public class Student {
private String xuehao;
private String name;
private String zhiye;
private String xingbie;
private String nianling;
private String aihao;
public String getXuehao() {
return xuehao;
}
public void setXuehao(String xuehao) {
this.xuehao = xuehao;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getZhiye() {
return zhiye;
}
public void setZhiye(String zhiye) {
this.zhiye = zhiye;
}
public String getXingbie() {
return xingbie;
}
public void setXingbie(String xingbie) {
this.xingbie = xingbie;
}
public String getNianling() {
return nianling;
}
public void setNianling(String nianling) {
this.nianling = nianling;
}
public String getAihao() {
return aihao;
}
public void setAihao(String aihao) {
this.aihao = aihao;
}
//通过覆盖超类的clone方法来实现克隆
@Override
public Object clone() {
Student stu = null;
try{
stu = (Student)super.clone();
}catch(CloneNotSupportedException e) {
e.printStackTrace();
}
return stu;
}
}
该学生类继承了所有类的超类Object;
关键地方在于对超类中的clone方法的重写;
下面我们新建一个测试类ADDThread
package com.onekeyop.sop.bingfa;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class AddThread extends Thread{
public static void main(String[] args){
Map<String,String> xuesheng = new HashMap<>();
Map<String,String> xuesheng2 = new HashMap<>();
//首先创建一个学生对象
Student zhangqingjiao = new Student();
//java中只能传递对值的引用,而不能传递值
zhangqingjiao.setAihao("钓鱼");
zhangqingjiao.setName("张发菜");
zhangqingjiao.setNianling("22");
zhangqingjiao.setXingbie("男");
zhangqingjiao.setZhiye("程序员");
zhangqingjiao.setXuehao("2014551522");
//将对象的属性放入键值对中
xuesheng.put("爱好",zhangqingjiao.getAihao());
xuesheng.put("姓名",zhangqingjiao.getName());
xuesheng.put("年龄",zhangqingjiao.getNianling());
xuesheng.put("性别",zhangqingjiao.getXingbie());
xuesheng.put("职业",zhangqingjiao.getZhiye());
xuesheng.put("学号",zhangqingjiao.getZhiye());
System.out.println(xuesheng);
try {
Student zhangqingjiao2 = (Student) zhangqingjiao.clone();
}catch (Exception e){
e.printStackTrace();
}
//***********
xuesheng2.put("爱好",zhangqingjiao.getAihao());
xuesheng2.put("姓名",zhangqingjiao.getName());
xuesheng2.put("年龄",zhangqingjiao.getNianling());
xuesheng2.put("性别",zhangqingjiao.getXingbie());
xuesheng2.put("职业",zhangqingjiao.getZhiye());
xuesheng2.put("学号",zhangqingjiao.getZhiye());
//接下来就要克隆一个对象
System.out.println(xuesheng);
//克隆开始
//Student zhangqingjiao2=zhangqingjiao;
System.out.println(xuesheng2);
Thread thread1 = new Thread();
Thread thread2 = new Thread();
System.out.println("see two see");
System.out.println(xuesheng.equals(xuesheng2));
System.out.println(xuesheng==xuesheng2);
System.out.println(xuesheng.hashCode());
String s="A";
System.out.println(s.hashCode());
}
}
其中,我们运用了map工具类,来存放学生对象拥有的一些属性
输出结果如下:
{学号=程序员, 性别=男, 姓名=张发菜, 职业=程序员, 年龄=22, 爱好=钓鱼}
{学号=程序员, 性别=男, 姓名=张发菜, 职业=程序员, 年龄=22, 爱好=钓鱼}
{学号=程序员, 性别=男, 姓名=张发菜, 职业=程序员, 年龄=22, 爱好=钓鱼}
结果能输出正确,并且编译器还报了异常
java.lang.CloneNotSupportedException: com.onekeyop.sop.bingfa.Student
at java.lang.Object.clone(Native Method)
at com.onekeyop.sop.bingfa.Student.clone(Student.java:63)
at com.onekeyop.sop.bingfa.AddThread.main(AddThread.java:35)
这个异常说是克隆不支持异常;
由于克隆,是对值的赋值,而不是对引用的赋值,查看jdk源码,你就会发现
protected native Object clone() throws CloneNotSupportedException;
克隆方法是通过java本地方法实现,因为此方法要和cpu底层打交道