简体   繁体   English

嵌套类的公共构造函数

[英]Public constructor for nested class

I'm parsing a JSON and bumped into an exception saying I don't have default constructor for one of my entities. 我正在解析一个JSON并遇到一个异常,说我的某个实体没有默认构造函数。 Here is the code: 这是代码:

public class MyPromosResponse extends BaseResponse {

    public MyPromosResponseData response;

    public MyPromosResponse() {
    }

    public TreeSet<Promo> getMyPromosResponseData() {
        return new TreeSet<Promo>(response.getEvents());
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    class MyPromosResponseData {
        public ArrayList<Promo> events;
        public ArrayList<Table1PromoData> Table1;

        public MyPromosResponseData() {
        }

        public ArrayList<Promo> getEvents() {
            return events;
        }

        ArrayList<Table1PromoData> getTable1() {
            return Table1;
        }

    }

    class Table1PromoData {
        public int id;
        public int eventid;
        public int cardholderid;

        public Table1PromoData() { // Here is the constructor.
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getEventid() {
            return eventid;
        }

        public void setEventid(int eventid) {
            this.eventid = eventid;
        }

        public int getCardholderid() {
            return cardholderid;
        }

        public void setCardholderid(int cardholderid) {
            this.cardholderid = cardholderid;
        }
    }

}

When I have Table1PromoData as a nested class I have the error the default constructor can't be found, but there is a public constructor defined. 当我将Table1PromoData作为嵌套类时,我有错误,无法找到默认构造函数,但是定义了一个公共构造函数。 If I move it to a separate file everything is fine - the lib finds the constructor. 如果我将它移动到一个单独的文件一切都很好 - lib找到构造函数。

Why the default constructor become visible after I moved it to a separate file? 将默认构造函数移动到单独的文件后,为什么默认构造函数可见?

Because inner classes can also be public, private, or protected. 因为内部类也可以是公共的,私有的或受保护的。 If you don't specify, its private and can't be seen outside the class. 如果你没有指定,它的私有和在课外都看不到。 You need to declare the class as public. 您需要将该类声明为public。

如果我的内部类不是静态的,我遇到了JSON序列化程序(特别是Jackson)的问题 - 显然如果你的内部类不是静态的,那么默认构造函数会被破坏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM