预备姿势
- 能看懂java代码 能接受我的蹩脚单词
- 不是基础详细讲解,更多的是一个笔记和分享。
- 对深拷贝 浅拷贝有基本认识
- 基本都采用代码分析 保证可以运行 大家时间不多 我也把输出给一起带上了
目标 学习clone函数
简单使用
package cloneDemo;
//公共对象 下面的demo都会使用
//必须继承这个接口 object并没有继承这个接口 如果直接调用Object的Clone会报错
public class Person implements Cloneable {
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;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
//使用简单的clone
private static void cloneTest1() throws Exception{
Person p = new Person();
p.setName("小陈");
p.setAge(21);
Person cloneP = (Person)p.clone();
System.out.println(cloneP.getAge() +" year" + cloneP.getName());
}
//ouput: 21 year小陈
了解系统默认clone
关注重点深拷贝 浅拷贝
//验证默认使用那种拷贝方式
//output: 21cm
//默认:浅拷贝
private static void cloneTest2() throws Exception{
Person p = new Person();
p.setHeight(new MyInteger(20));
Person cloneP = (Person)p.clone();
p.getHeight().setNumber(21);
System.out.println(cloneP.getHeight().getNumber()+"cm");
}
package cloneDemo;
//必须继承这个接口 object并没有继承这个接口 如果直接调用Object的Clone产生异常
public class Person implements Cloneable {
private String name ;
private int age;
private MyInteger height;
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 MyInteger getHeight() {
return height;
}
public void setHeight(MyInteger height) {
this.height = height;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package cloneDemo;
public class MyInteger {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public MyInteger(int n) {this.number = n;}
public MyInteger() {}
}
学习自定义clone方法
网上逛一圈 就发现两种实现深拷贝的方法
- 一种自己new一个出来,设置值
- 一种类自己的的clone方法,类自身必须实现 . 这里对于我们的目的来说没有区别,从设计的角度在使用的时候还是需要有所考量
//深拷贝clone方法实现
package cloneDemo;
//必须继承这个接口 object并没有继承这个接口 如果直接调用Object的Clone产生异常
public class Person implements Cloneable {
private String name ;
private int age;
private MyInteger height;
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 MyInteger getHeight() {
return height;
}
public void setHeight(MyInteger height) {
this.height = height;
}
//实现深拷贝
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
Person person = (Person)super.clone();
// person.hight = (MyInteger)this.height.clone();
person.height = new MyInteger(this.height.getNumber());
return person;
}
}
package cloneDemo;
public class MyInteger implements Cloneable {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public MyInteger(int n) {this.number = n;}
public MyInteger() {}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
//自定义深拷贝
//output: 20cm 浅拷贝
private static void cloneTest3() throws Exception{
Person p = new Person();
p.setHeight(new MyInteger(20));
Person cloneP = (Person)p.clone();
p.getHeight().setNumber(21);
System.out.println(cloneP.getHeight().getNumber()+"cm");
}
对clone的源码理解
这里就很坑了 jvm的clone .没有好的阅读工具,我不知道怎么关联阅读。看到jvm.clone那附近就找不到路了。 javajdk的源码和glibc比起来麻烦了好多,自己封装了好多东西。这个坑以后填