[英]Any way to call the default constructor from a parameterized constructor?
Suppose, I have the following code 假设我有以下代码
class C {
int i;
String s;
C(){
System.out.println("In main constructor");
// Other processing
}
C(int i){
this(i,"Blank");
System.out.println("In parameterized constructor 1");
}
C(int i, String s){
System.out.println("In parameterized constructor 2");
this.i = i;
this.s = s;
// Other processing
// Should this be a copy-paste from the main contructor?
// or is there any way to call it?
}
public void show(){
System.out.println("Show Method : " + i + ", "+ s);
}
}
I would like to know, Is there any way, I can call the main(default) constructor from the parametrized constructor (ie C(int i, String s)
in this case) ? 我想知道,有什么办法,我可以从参数化的构造函数(在这种情况下为
C(int i, String s)
调用main(default)构造函数?
Or I have just copy-paste the entire contents from the main(default) constructor to the parametrized one, as shown in comments in the above code? 或者我只是将整个内容从main(默认)构造函数复制粘贴到参数化的结构,如上面代码中的注释所示?
I need to call the default constructor after the variables i
and s
are set in the parametrized constructor, as the processing involves these variables. 在参数化构造函数中设置了变量
i
和s
之后,我需要调用默认构造函数,因为处理涉及这些变量。
I see this post , which says placing this()
as the first line would call the default constructor. 我看到这篇文章 ,说将
this()
作为第一行将调用默认构造函数。 But I need to call it after the setting of values. 但是我需要在设置值后调用它。
Calling this()
would work, but note this must be the first statement in constructor. 调用
this()
会起作用,但是请注意,它必须是构造函数中的第一条语句。 For eg: Below code would be illegal and won't compile: 例如:下面的代码将是非法的,将无法编译:
class Test {
void Test() { }
void Test(int i) {
i = 9;
this();
}
}
An option could be to call your parameterized constructor from your default constructor. 一种选择是从默认构造函数调用参数化构造函数。
C(){
this(0, "Blank");
}
C(int i){
this(i,"Blank");
}
C(int i, String s){
this.i = i;
this.s = s;
}
This pattern will let you supply defaults for constructors with less arguments to the more specific constructors. 通过该模式,您可以为构造函数提供默认值,而为更具体的构造函数提供较少的参数。
Also, note chaining constructors must be done as the first call in another constructor - you cannot call another constructor after initializing variables: 另外,注释链构造器必须作为另一个构造器中的第一个调用完成-初始化变量后不能调用另一个构造器:
C(int i, String s){
this.i = i;
this.s = s;
this(); // invalid!
}
If you really want to do something like this, consider an init method: 如果您确实想做这样的事情,请考虑使用init方法:
C() {
init();
}
C(int i, String s) {
this.i = i;
this.s = s;
init();
}
Calling this()
as the first statement in other constructors is enough. 调用
this()
作为其他构造函数中的第一条语句就足够了。
C(int i, String s)
{
this();
// other stuff.
}
Quoting the Docs: 引用文档:
the invocation of another constructor must be the first line in the constructor.
另一个构造函数的调用必须是该构造函数的第一行 。
So no its not possible. 所以没有不可能。 Use this() in first line.
在第一行中使用this()。
You can move all the code from main constructor to some method, say, mainProcessing(). 您可以将所有代码从主构造函数移至某个方法,例如mainProcessing()。
C()
{
System.out.println("In main constructor");
mainProcessing();
}
private void mainProcessing()
{
// Move your code from main constructor to here.
}
Now in your parameterized constructor 2, you can call this method, mainProcessing(), at desired location. 现在,在参数化的构造函数2中,可以在所需位置调用此方法mainProcessing()。
C(int i, String s)
{
System.out.println("In parameterized constructor 2");
this.i = i;
this.s = s;
mainProcessing();
}
Just call the constructor ny this(); 只需调用构造函数ny this();即可。 statment as a first line in your default constructor....
声明作为默认构造函数的第一行。
You can use this();
您可以使用
this();
to call default constructor 调用默认构造函数
C(int i, String s){
this(); // call to default constructor
// .....
}
Use this() in first line of parametrized constructor 在参数化构造函数的第一行中使用this()
C(int i, String s){
this();
System.out.println("In parameterized constructor 2");
this.i = i;
this.s = s;
// Other processing
// Should this be a copy-paste from the main contructor?
// or is there any way to call it?
}
You can call this()
in the first statement to call default constructor in any parameterized constructor. 您可以在第一条语句中调用
this()
以在任何参数化的构造函数中调用默认构造函数。
Note this()
should mandatorily be the first line in the constructor definition 请注意,
this()
必须强制为构造函数定义的第一行
C(int i, String s){
this();
. . . .
}
But I need to call it after the setting of values.
但是我需要在设置值后调用它。
It is not possible. 这不可能。 constructor invocation has to be the first statement.
构造函数调用必须是第一个语句。
You can go through this link : constructor invocation must be the first line 您可以通过以下链接: 构造函数调用必须是第一行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.