JAVA浅克隆与深克隆

package oop;

import java.io.Serializable;

public class Parent implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;


@Override
public String toString() {
return "Parent [age=" + age + ", name=" + name + "]";
}

protected Object clone() throws CloneNotSupportedException {
return super.clone();
}

public Parent() {
}

public Parent(String name, int age) {
super();
this.name = name;
this.age = 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;
}

}



package oop;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Student implements Serializable,Cloneable {//实现 序列化接口   和克隆接口

private static final long serialVersionUID = 1L;
private /*transient  可以不被序列化*/ int id;
private int age;
private char sex;
private String name;
private Parent parent; // 浅克隆 不能克隆parent
public Student() {
}

public Student(int id, int age, char sex, String name, Parent parent) {
super();
this.id = id;
this.age = age;
this.sex = sex;
this.name = name;
this.parent = parent;
}

public Student clone() throws CloneNotSupportedException {
return (Student)super.clone();
}

public Object clone2() {  //用流实现深克隆
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
Object object = null;
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);

bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
object = ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

return object;
}

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((parent == null) ? 0 : parent.hashCode());
result = prime * result + sex;
return result;
}

@Override
public String toString() {
return "Student [age=" + age + ", id=" + id + ", name=" + name
+ ", parent=" + parent + ", sex=" + sex + "]";
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (parent == null) {
if (other.parent != null)
return false;
} else if (!parent.equals(other.parent))
return false;
if (sex != other.sex)
return false;
return true;
}

public Parent getParent() {
return parent;
}

public void setParent(Parent parent) {
this.parent = parent;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public char getSex() {
return sex;
}

public void setSex(char sex) {
this.sex = sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}



package oop;

public class Test {
public static void main(String[] args) {
Parent p = new Parent("刘老",50);
Student stu= new Student(15005,25,'男',"刘备备",p);
Student stuclone=(Student)stu.clone2(); //深克隆
try {
Student  copy=stu.clone(); //浅克隆
stu.getParent().setAge(55);
System.out.println(copy);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}

System.out.println(stuclone);

}



}

你可能感兴趣的:(clone)