java内部类并实例化问题

2023-07-17 19:49:54 浏览数 (1)

代码语言:javascript复制
@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 这个类 采用:

代码语言:javascript复制
  mailAttachmentInfo = new MailOperationContent.MailAttachmentInfo();

但是编译报错

这个时候就会出现编译错误:"xxx.OuterClass" is not an enclosing class

解决办法:若要创建内部类的实例,需要有外部类的实例才行,或者是将内部类设置为静态的。 方法一:

代码语言:javascript复制
   mailAttachmentInfo = new MailOperationContent().new MailAttachmentInfo();

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

代码语言:javascript复制
 @Data
    public static class MailAttachmentInfo {
        private String attachmentName;
        private String attachmentSecretLevel;
    }

0 人点赞