现在有一只羊 tom,姓名为: tom, 年龄为:1,颜色为:白色,请编写程序创建和 tom 羊 属性完全相同的 10
只羊。
/**
* @author: LiDeLin [email protected]
* @date: 2019/11/4
* @description :
*/
public class Sheep {
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
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 String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
/**
* @author: LiDeLin [email protected]
* @date: 2019/11/4
* @description :
*/
public class Client {
public static void main(String[] args) {
//传统方法
Sheep sheep = new Sheep("tom", 1, "白色");
Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
System.out.println(sheep);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
System.out.println(sheep5);
}
}
/**
* @author: LiDeLin [email protected]
* @date: 2019/11/4
* @description :
*/
public class Sheep implements Cloneable{
private String name;
private int age;
private String color;
private String address = "羊村";
//对象
private Sheep friend;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Sheep getFriend() {
return friend;
}
public void setFriend(Sheep friend) {
this.friend = friend;
}
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
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 String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
", address='" + address + '\'' +
'}';
}
/**
* 克隆该实例,使用默认的clone方法来完成
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep)super.clone();
}catch (Exception e){
System.out.println(e.getMessage());
}
return sheep;
}
}
/**
* @author: LiDeLin [email protected]
* @date: 2019/11/4
* @description :
*/
public class Client {
public static void main(String[] args) {
System.out.println("使用原型模式完成对象的创建");
Sheep sheep = new Sheep("tom", 1, "白色");
sheep.setFriend(new Sheep("jack",2,"黑色"));
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
Sheep sheep4 = (Sheep) sheep.clone();
Sheep sheep5 = (Sheep) sheep.clone();
System.out.println(sheep+":"+sheep2.getFriend().hashCode());
System.out.println(sheep2+":"+sheep2.getFriend().hashCode());
System.out.println(sheep3+":"+sheep2.getFriend().hashCode());
System.out.println(sheep4+":"+sheep2.getFriend().hashCode());
System.out.println(sheep5+":"+sheep2.getFriend().hashCode());
}
}
public class DeepCloneableTarget implements Serializable, Cloneable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String cloneName;
private String cloneClass;
public DeepCloneableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
/**
* 该类的属性,都是String,使用默认的clone
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
/**
* @author: LiDeLin [email protected]
* @date: 2019/11/4
* @description :
*/
public class DeepProtoType implements Serializable,Cloneable {
private String name;
/**
* 引用类型
*/
private DeepCloneableTarget deepCloneableTarget;
public DeepProtoType(String name, DeepCloneableTarget deepCloneableTarget) {
this.name = name;
this.deepCloneableTarget = deepCloneableTarget;
}
public DeepProtoType() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DeepCloneableTarget getDeepCloneableTarget() {
return deepCloneableTarget;
}
public void setDeepCloneableTarget(DeepCloneableTarget deepCloneableTarget) {
this.deepCloneableTarget = deepCloneableTarget;
}
/**
* 完成深拷贝 方式1 使用clone
*/
@Override
protected Object clone() throws CloneNotSupportedException {
Object deep = null;
//这里完成对基本数据类型和字符串的克隆
deep = super.clone();
//对引用类型的属性进行单独的处理
DeepProtoType deepProtoType = (DeepProtoType)deep;
deepProtoType.deepCloneableTarget = (DeepCloneableTarget)deepCloneableTarget.clone();
return deepProtoType;
}
/**
* 深拷贝 方式2 通过对象的序列化实现(推荐)
*/
public Object deepClone(){
//创建流对象
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
///序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
//当前这个对象以对象流的方式输出
oos.writeObject(this);
//反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
DeepProtoType copyObj = (DeepProtoType) ois.readObject();
return copyObj;
}catch (Exception e){
e.printStackTrace();
return null;
} finally {
//关闭流
try {
ois.close();
bis.close();
oos.close();
bos.close();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
}