提交 31d21788 编写于 作者: N nicky

原型模式测试类

上级 3147acaa
package com.muses.taoshop;
import java.io.*;
public class Attachment
public class Attachment implements Serializable
{
public void download()
{
System.out.println("下载附件");
System.out.println("下载附件");
}
}
\ No newline at end of file
package com.muses.taoshop;
public class Client
{
public static void main(String a[])
{
Email email,copyEmail;
Email email,copyEmail=null;
email=new Email();
copyEmail=(Email)email.clone();
try{
copyEmail=(Email)email.deepClone();
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("email==copyEmail?");
System.out.println(email==copyEmail);
System.out.println("email.getAttachment==copyEmail.getAttachment?");
System.out.println(email.getAttachment()==copyEmail.getAttachment());
System.out.println("email.getAttachment==copyEmail.getAttachment?");
System.out.println(email.getAttachment()==copyEmail.getAttachment());
}
}
\ No newline at end of file
package com.muses.taoshop;
import java.io.*;
public class Email implements Cloneable
public class Email implements Serializable
{
private Attachment attachment=null;
public Email()
{
this.attachment=new Attachment();
}
public Object clone()
public Object deepClone() throws IOException, ClassNotFoundException, OptionalDataException
{
Email clone=null;
try
{
clone=(Email)super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println("Clone failure!");
}
return clone;
//将对象写入流中
ByteArrayOutputStream bao=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bao);
oos.writeObject(this);
//将对象从流中取出
ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bis);
return(ois.readObject());
}
public Attachment getAttachment()
{
return this.attachment;
}
public void display()
{
System.out.println("查看邮件");
System.out.println("查看邮件");
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册