调用super()必须是构造函数体中的第一个语句 [英] call to super() must be the first statement in constructor body

查看:3848
本文介绍了调用super()必须是构造函数体中的第一个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing the constructor of a LoginRequest class which extends a class called JsobObjectRequest (from the Volley framework in Android, but that's completely irrelevant to the question)

使用此代码:

 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;

    }

我得到错误:调用super在构造函数体中的第一个语句

I get the error: call to super() must be the first statement in constructor body

而是,这个代码编译得很好:

Instead, this code compiles just fine:

 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;

    }

但是不是有效的一样吗?
在这两种情况下,在调用超级构造函数之前,基于传递给子类构造函数的参数的值,进行一些平凡的计算。为什么编译器不能编译第一个例子给定它可以编译第二个?

But isn't it effectively the exact same thing? In both cases, a couple of trivial computation are made prior to calling the super constructor, based on the values of the parameters passed to the subclass constructor. Why shouldn't the compiler be able to compile the first example given that it can compile the second?

是调用超级构造函数必须是第一语句规范比它需要的更简单,或者我缺少某些东西?

Is the calling-super-constructor-must-be-first-statement specification more simplistic than it would need to be, or am I missing something?

EDIT :这被错误地标记为< a href =http://stackoverflow.com/questions/1168345/why-does-this-and-super-have-to-be-the-first-statement-in-a-constructor>为什么这个()和super()必须是构造函数中的第一个语句?这个问题是更通用的,并问为什么super()必须是第一个语句。

EDIT: this has been wrongly marked as duplicate of Why does this() and super() have to be the first statement in a constructor?. That question is much more generic and asks why super() have to be the first statement at all. The question here is why a case like the one I've posted would defeat those requirements (and it has been satisfactorily answered in this very question)

推荐答案

Java强制调用 super (显式或非显式)必须是构造函数中的第一个语句。这是为了防止对象的子类部分在被初始化的对象的超类部分之前被初始化。

Java enforces that the call to super (explicit or not) must be the first statement in the constructor. This is to prevent the subclass part of the object being initialized prior to the superclass part of the object being initialized.

在你的情况下,除了local 琐碎的计算,所以所有的事情,考虑,这将是好的。但是,Java的编译器没有达到在调用 super 之前语句是否做任何初始化的级别。它只是禁止 super()之前的所有语句。

In your case, you don't do anything but local "trivial computation", so all things considered, it would be okay. However, Java's compiler doesn't go to the level of determining whether statements before a call to super actually do any initialization. It just disallows all statements before super().

这篇关于调用super()必须是构造函数体中的第一个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆