我有一堂简单的课:
public class MailAttachment {
InputStream inputStream; //file to send as an attachment
String fileName; //name given to the sent file as an attachment
}
我使用此类来发送电子邮件,例如:
public boolean sendEmailWithAttachments(String[] recipients, MailAttachment[] attachments, String subject, String body) {
...
}
我也有从服务器上任何消息中检索附件(文件)的方法:
public File retrieveFile(String filename, boolean shouldDeleteFromServer) {
/* returns retrieved file */
}
但是我想使'retrieveFile'方法也返回类型为'MailAttachment'的对象,但需要使用不同的字段:
public class MailAttachment {
File file; //file retrived from mail server, existing on disk
String mailSubject; //The name of the message (subject) that holds the attachment
}
如何实现这一点,以使send方法和检索方法使用相同的类型,即MailAttachment?我考虑了继承问题,还考虑了这种类型的“ MailAttachment”具有所有这些字段。最好的聪明方法是什么?
在这两种情况下,都可以使用原始MailAttachment
类InputStream
来传递文件信息。
设置inputStream
为FileInputStream
使用File
参数(link)构造的实例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句