相关疑难解决方法(0)

对super()的调用必须是构造函数体中的第一个语句

我正在编写一个LoginRequest类的构造函数,它扩展了一个名为JsobObjectRequest的类(来自Android中的Volley框架,但这与问题完全无关)

使用此代码:

 public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
        Boolean hasCredentials=(username!=null && password!=null);
        int method=hasCredentials? Method.POST:Request.Method.GET;
        super(method, API_URL_LOGIN, null, responseListener, errorListener);

        this.username=username;
        this.password=password;

    }
Run Code Online (Sandbox Code Playgroud)

我得到错误:调用super()必须是构造函数体中的第一个语句

相反,这段代码编译得很好:

 public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
        super((username!=null && password!=null)? Method.POST:Request.Method.GET, API_URL_LOGIN, null, responseListener, errorListener);

        this.username=username;
        this.password=password;

    }
Run Code Online (Sandbox Code Playgroud)

但这不是有效的完全相同的事情吗?在这两种情况下,在调用超级构造函数之前,根据传递给子类构造函数的参数值,进行一些简单的计算.为什么编译器不能编译第一个例子,因为它可以编译第二个例子?

call-super-constructor-must-first-statement语句规范是否比它需要的更简单,或者我错过了什么?

编辑:这被错误地标记为重复为什么this()和super()必须是构造函数中的第一个语句?.这个问题更通用,并询问为什么super()必须是第一个声明.这里的问题是为什么像我发布的那样的案例会破坏这些要求(并且在这个问题中得到了令人满意的回答)

java constructor super

7
推荐指数
1
解决办法
8640
查看次数

标签 统计

constructor ×1

java ×1

super ×1