1.對象的淺克隆
1.一個對象如果要調用clone()方法,那麼該對象所屬的類必須實現Cloneable接口。
2.Cloneable接口是一個標識接口,沒有任何方法。
3.對象的淺克隆:在克隆一個對象的時候,如果被克隆的對象中維護了另外一個類的對象,這時候,只是克隆了另外一個對象的地址,而沒有把另外一個對象也克隆一份
4.對象的克隆沒有調用構造方法。
代碼:
//City類
package day07;
public class City {
private String name;
public City(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//Worker類
package day07;
public class Worker implements Cloneable{
//Cloneable是一個標識接口
private String name;
private double salary;
private City city;
public Worker() {
}
public Worker(String name,double salary,City city)
{
System.out.println("構造方法運行中");
this.name=name;
this.salary=salary;
this.city=city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getCityName() {
return city.getName();
}
public void setCityName(String cityName) {
this.city.setName(cityName);
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString()
{
return "姓名:"+this.name+"薪水:"+this.salary+"城市:"+this.city.getName();
}
}
//Test
package day07;
public class CloneDemo {
public static void main(String args[]) throws CloneNotSupportedException
{
City city=new City("長沙");
Worker w1=new Worker("老林",5530.1,city);
Worker w2=(Worker)w1.clone();
w2.setCityName("北京");
System.out.println(w1);
System.out.println("-------------");
System.out.println(w2);
}
}
運行結果如下
2.對象的深克隆
對象的深克隆就是利用對象的輸入輸出流將對象先寫到文件上,然後再讀取對象的信息,這個過程就稱爲對象的深克隆。
代碼:
package day07;
import java.io.Serializable;
public class City implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4395851692262429808L;
private String name;
public City(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package day07;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Worker implements Cloneable,Serializable{
/**
*
*/
private static final long serialVersionUID = -425984482155520827L;
//Cloneable是一個標識接口
private String name;
private double salary;
private City city;
public Worker() {
}
public Worker(String name,double salary,City city)
{
System.out.println("構造方法運行中");
this.name=name;
this.salary=salary;
this.city=city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getCityName() {
return city.getName();
}
public void setCityName(String cityName) {
this.city.setName(cityName);
}
public void writeObject() throws IOException
{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\\obj.txt"));
oos.writeObject(this);
oos.close();
}
public Object readObject() throws FileNotFoundException, IOException, ClassNotFoundException
{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:\\obj.txt"));
Object w=ois.readObject();
ois.close();
return w;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString()
{
return "姓名:"+this.name+"薪水:"+this.salary+"城市:"+this.city.getName();
}
}
package day07;
import java.io.IOException;
public class CloneDemo {
public static void main(String args[]) throws CloneNotSupportedException, IOException, ClassNotFoundException
{
City city=new City("長沙");
Worker w1=new Worker("老林",5530.1,city);
Worker w2=(Worker)w1.clone();
w1.writeObject();
Worker w3=(Worker)w1.readObject();
w3.setCityName("北京");
System.out.println(w1);
System.out.println("-------------");
System.out.println(w2);
System.out.println(w3);
}
}
運行結果: