画类图
浅克隆:
深克隆:
具体代码实现
浅克隆具体代码:
//Vector.java
package org.example.design004;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Vector implements Cloneable{
public int[] getArray() {
return array;
}
public void setArray(int[] array) {
this.array = array;
}
public Attachment getAttachment() {
return attachment;
}
public void setAttachment(Attachment attachment) {
this.attachment = attachment;
}
private int[] array;
private Attachment attachment=null;
public Vector(){
this.attachment=new Attachment();
}
public Object clone() throws CloneNotSupportedException {
Vector clone=null;
clone=(Vector) super.clone();
return clone;
}
public void display(){
System.out.println(Arrays.toString(array));
}
}
//Attachment.java
package org.example.design004;
public class Attachment {
public void download(){
System.out.println("复制向量");
}
}
//Client.java
package org.example.design004;
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Vector vector,copyVector;
vector=new Vector();
int[] arr={1,2,3,4};
vector.setArray(arr);
System.out.println("浅克隆复制:");
copyVector=(Vector) vector.clone();
System.out.print("vector:");
vector.display();
System.out.print("copyVector:");
copyVector.display();
System.out.println("vector==copyVector?");
System.out.println(vector.getAttachment()==copyVector.getAttachment());
}
}
深克隆具体代码:
//Vector.java
package org.example.design004;
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
public class Vector implements Serializable{
public int[] getArray() {
return array;
}
public void setArray(int[] array) {
this.array = array;
}
public Attachment getAttachment() {
return attachment;
}
public void setAttachment(Attachment attachment) {
this.attachment = attachment;
}
private int[] array;
private Attachment attachment=null;
public Vector(){
this.attachment=new Attachment();
}
public Object deepClone() throws IOException, ClassNotFoundException {
ByteArrayOutputStream bao=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bao);
oos.writeObject(this);
ByteArrayInputStream bai=new ByteArrayInputStream(bao.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bai);
return (ois.readObject());
}
public void display(){
System.out.println(Arrays.toString(array));
}
}
//Attachment.java
package org.example.design004;
import java.io.Serializable;
public class Attachment implements Serializable {
public void download(){
System.out.println("复制向量");
}
}
//Client.java
package org.example.design004;
import java.io.IOException;
public class Client {
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Vector vector,copyVector;
vector=new Vector();
int[] arr={1,2,3,4};
vector.setArray(arr);
System.out.println("深克隆复制:");
copyVector=(Vector) vector.deepClone();
System.out.print("vector:");
vector.display();
System.out.print("copyVector:");
copyVector.display();
System.out.println("vector==copyVector?");
System.out.println(vector.getAttachment()==copyVector.getAttachment());
}
}
标签:Java,Vector,--,System,vector,原型,java,public,out
From: https://www.cnblogs.com/liuzijin/p/17763817.html