java内部类并实例化问题

/ Java / 0 条评论 / 92 浏览
@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

解决办法:若要创建内部类的实例,需要有外部类的实例才行,或者是将内部类设置为静态的。 方法一: ```java mailAttachmentInfo = new MailOperationContent().new MailAttachmentInfo();

方法二:将内部类设置为静态的


```java
 @Data
    public static class MailAttachmentInfo {
        private String attachmentName;
        private String attachmentSecretLevel;
    }