clone()方法----------(protected native Object clone() throws CloneNotSupportedException)
a.方法功能:对象拷贝
b.要进行"克隆"的对象所属的类必须实现java.lang.Cloneable接口
c.浅度拷贝和深度拷贝
经过克隆得到的对象与被克隆的对象的地址值不一样!!!
a.方法功能:对象拷贝
b.要进行"克隆"的对象所属的类必须实现java.lang.Cloneable接口
c.浅度拷贝和深度拷贝
浅度拷贝就是要拷贝当前对象的内容,自己是另外的地址,而该对象中的引用类型成员也是把地址给克隆对象了。
//要进行"克隆"的对象所属的类必须实现java.lang.Cloneable接口
public class Person1 implements Cloneable {
private String name;
private int age;
public Person1(String name, int age) {
this.name = name;
this.age = age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void display() {
System.out.println("Name:" + name + "\tAge:" + age);
}
// 重写从Object类继承而来的clone()方法
public Object clone() {
Person1 p = null;
try {
p = (Person1) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.getMessage());
}
return p;
}
}
//要进行"克隆"的对象所属的类必须实现java.lang.Cloneable接口
public class Book implements Cloneable {
String bookName;
double price;
Person1 author;// 其成员中对象所属的类也要实现Cloneable接口
public Book(String bn, double price, Person1 author) {
bookName = bn;
this.price = price;
this.author = author;
}
@Override
public Object clone() {
Book b = null;
try {
b = (Book) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return b;
}
public void display() {
System.out.println(bookName + "\t" + price + "\t");
author.display();
}
}
//浅度拷贝
public class TestLowCopy {
public static void main(String[] args) {
Book b1=new Book("Java编程",30.50,new Person1("张三",34));
Book b2=(Book) b1.clone();
b2.price=44.0;
b2.author.setAge(45);
b2.author.setName("李四");
b2.bookName="Java 开发";
b1.display();
b2.display();
/*
* Person1传递的是值(引用的地址),所以Person1对象是同一个
* */
}
}
深度拷贝不仅复制当前对象的内容到不同的地址,并且也复制该对象内中所有引用类型的内容到不同地址。
public class Person2 implements Cloneable{
private String name;
private int age;
public Person2(String name,int age){
this.name = name;
this.age = age;
}
public void setAge(int age){
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void display(){
System.out.println("Name:" + name + "\tAge:" + age);
}
public Object clone(){
Person2 p = null;
try{
p = (Person2)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return p;
}
}
public class Book2 implements Cloneable{
String bookName;
double price;
Person2 author;
public Book2(String bn,double price,Person2 author){
bookName = bn;
this.price = price;
this.author = author;
}
public Object clone(){
Book2 b = null;
try{
b = (Book2)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
//这一行代码成就了深度拷贝。。。。。。
b.author = (Person2)author.clone();
return b;
}
public void display(){
System.out.print(bookName + "\t" + price + "\t") ;
author.display();
}
}
//深度拷贝
public class TestDeepCopy {
public static void main(String[] args) {
Book2 b1 = new Book2("Java编程",30.50,new Person2("张三",34));
Book2 b2 = (Book2)b1.clone();
b2.price = 44.0;
b2.author.setAge(45);
b2.author.setName("李四");
b2.bookName = "Java开发";
b1.display();
b2.display();
}
}
经过克隆得到的对象与被克隆的对象的地址值不一样!!!