java内部类并实例化问题
warning:
这篇文章距离上次修改已过607天,其中的内容可能已经有所变动。
@Data
public class MailOperationContent {
private String theme;
private List<String> toMail;
private List<String> ccMail;
private List<String> bccMail;
private List<String> fromMail;
private BTMailSecretlevelEnum secretLevel;
private Long mailSize;
private Integer attachmentNum;
private List<MailAttachmentInfo> mailAttachmentInfos = new ArrayList<>();
@Data
public class MailAttachmentInfo {
private String attachmentName;
private String attachmentSecretLevel;
}
但是我想实例化 MailAttachmentInfo 这个类
采用:
mailAttachmentInfo = new MailOperationContent.MailAttachmentInfo();
但是编译报错
这个时候就会出现编译错误:"xxx.OuterClass" is not an enclosing class
解决办法:若要创建内部类的实例,需要有外部类的实例才行,或者是将内部类设置为静态的。
方法一:
mailAttachmentInfo = new MailOperationContent().new MailAttachmentInfo();
方法二:将内部类设置为静态的
@Data
public static class MailAttachmentInfo {
private String attachmentName;
private String attachmentSecretLevel;
}