gpt4 book ai didi

Java:具有可交换字段的类?

转载 作者:太空宇宙 更新时间:2023-11-04 10:54:14 25 4
gpt4 key购买 nike

我有一个简单的类(class):

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
}

如何实现这一点,以便发送方法和检索方法使用相同的类型,即“MailAttachment”?我考虑了继承,也考虑了这种类型的“MailAttachment”具有所有这些字段。做到这一点的最佳聪明方法是什么?

最佳答案

在这两种情况下,您都可以将原始的 MailAttachment 类与 InputStream 一起使用来传回文件信息。

inputStream 设置为使用 File 参数构造的 FileInputStream 实例 ( link )。

关于Java:具有可交换字段的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47464109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com