Java,构造函数未定义

问题描述 投票:0回答:1

我手动添加了构造函数,但仍然得到 The constructor HttpResponseExtended() is undefined

public class HttpResponseExtended {
    HttpResponse<String> response;

    public HttpResponseExtended(HttpResponse<String> res) {
        this.response = res;
    }

    public JSONObject json() {
        return new JSONObject(response);
    }
}

我正在创建新对象:

new HttpResponseExtended()
java class constructor
1个回答
0
投票

如果添加任何类型的构造函数,那么您将不再获得默认的空构造函数。但你可以添加一个。喜欢,

public class HttpResponseExtended {
    HttpResponse<String> response;

    public HttpResponseExtended(HttpResponse<String> res) {
        this.response = res;
    }

    public HttpResponseExtended() {
        // Of course, now you need a default response...
    }

    public JSONObject json() {
        return new JSONObject(response);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.