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

原型模式测试类

上级 3147acaa
package com.muses.taoshop; package com.muses.taoshop;
import java.io.*;
public class Attachment public class Attachment implements Serializable
{ {
public void download() public void download()
{ {
System.out.println("下载附件"); System.out.println("下载附件");
} }
} }
\ No newline at end of file
package com.muses.taoshop; package com.muses.taoshop;
public class Client public class Client
{ {
public static void main(String a[]) public static void main(String a[])
{ {
Email email,copyEmail; Email email,copyEmail=null;
email=new Email(); 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==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; package com.muses.taoshop;
import java.io.*;
public class Email implements Cloneable public class Email implements Serializable
{ {
private Attachment attachment=null; private Attachment attachment=null;
public Email() public Email()
{ {
this.attachment=new Attachment(); this.attachment=new Attachment();
} }
public Object clone() public Object deepClone() throws IOException, ClassNotFoundException, OptionalDataException
{ {
Email clone=null; //将对象写入流中
try ByteArrayOutputStream bao=new ByteArrayOutputStream();
{ ObjectOutputStream oos=new ObjectOutputStream(bao);
clone=(Email)super.clone(); oos.writeObject(this);
}
catch(CloneNotSupportedException e) //将对象从流中取出
{ ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
System.out.println("Clone failure!"); ObjectInputStream ois=new ObjectInputStream(bis);
} return(ois.readObject());
return clone;
} }
public Attachment getAttachment() public Attachment getAttachment()
{ {
return this.attachment; return this.attachment;
} }
public void display() 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.
先完成此消息的编辑!
想要评论请 注册