[英]call to super() must be the first statement in constructor body
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) 我正在编写一个LoginRequest类的构造函数,该类扩展了一个名为JsobObjectRequest的类(来自Android中的Volley框架,但这与问题完全无关)
With this code : 使用此代码:
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;
}
I get the error: call to super() must be the first statement in constructor body 我得到错误:调用super()必须是构造函数体中的第一个语句
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? call-super-constructor-must-first-statement语句规范是否比它需要的更简单,或者我错过了什么?
EDIT : this has been wrongly marked as duplicate of Why do this() and super() have to be the first statement in a constructor? 编辑 :这被错误地标记为重复为什么this()和super()必须是构造函数中的第一个语句? .
。 That question is much more generic and asks why super() have to be the first statement at all.
这个问题更通用,并询问为什么super()必须是第一个声明。 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 enforces that the call to super
(explicit or not) must be the first statement in the constructor. Java强制要求对
super
(显式或非显式)的调用必须是构造函数中的第一个语句。 This is to prevent the subclass part of the object being initialized prior to the superclass part of the object being initialized. 这是为了防止在初始化对象的超类部分之前初始化对象的子类部分。
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. 但是,Java的编译器没有达到确定调用
super
之前的语句是否实际进行任何初始化的程度。 It just disallows all statements before super()
. 它只是在
super()
之前不允许所有语句。
This statement is true.super() must always be the first statement executed inside a subclass constructor. 此语句为true.super()必须始终是子类构造函数内执行的第一个语句。 .
。 for details see Java the complete reference by Herbert Schildt...
有关详细信息,请参阅Herbert Schildt的完整参考资料...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.