Java 有没有办法从参数化构造函数调用默认构造函数?
假设我有以下代码Java 有没有办法从参数化构造函数调用默认构造函数?,java,default-constructor,parameterized-constructor,Java,Default Constructor,Parameterized Constructor,假设我有以下代码 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"); }
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);
}
}
我想知道,有没有办法,我可以从参数化构造函数调用主(默认)构造函数(即在这种情况下C(int I,String s)
或者我只是将整个内容从主(默认)构造函数复制粘贴到参数化的构造函数,如上面代码中的注释所示
注
我需要在参数化构造函数中设置变量I
和s
后调用默认构造函数,因为处理涉及这些变量
编辑
我明白了,这表示将
this()
作为第一行调用默认构造函数。但是我需要在设置值之后调用它。只需调用构造函数ny this();station作为默认构造函数的第一行…只需调用构造函数ny this();station作为默认构造函数中的第一行….调用this()
作为其他构造函数中的第一条语句就足够了
C(int i, String s)
{
this();
// other stuff.
}
调用this()
作为其他构造函数中的第一条语句就足够了
C(int i, String s)
{
this();
// other stuff.
}
您可以使用this()代码>调用默认构造函数
C(int i, String s){
this(); // call to default constructor
// .....
}
您可以使用this()代码>调用默认构造函数
C(int i, String s){
this(); // call to default 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?
}
在参数化构造函数的第一行中使用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?
}
引用文件:
调用另一个构造函数必须是
构造器
所以不,这是不可能的。在第一行中使用此()。引用文档:
调用另一个构造函数必须是
构造器
所以不,这是不可能的。在第一行中使用this()。调用this()
会起作用,但请注意,这必须是构造函数中的第一条语句。例如:下面的代码是非法的,无法编译:
class Test {
void Test() { }
void Test(int i) {
i = 9;
this();
}
}
调用this()
可以工作,但请注意,这必须是构造函数中的第一条语句。例如:下面的代码是非法的,无法编译:
class Test {
void Test() { }
void Test(int i) {
i = 9;
this();
}
}
您可以在第一条语句中调用this()
,以调用任何参数化构造函数中的默认构造函数
注意this()
必须是构造函数定义的第一行
C(int i, String s){
this();
. . . .
}
但我需要在设置值之后调用它
这是不可能的。构造函数调用必须是第一条语句
您可以通过此链接:您可以在第一条语句中调用this()
,以调用任何参数化构造函数中的默认构造函数
注意this()
必须是构造函数定义的第一行
C(int i, String s){
this();
. . . .
}
但我需要在设置值之后调用它
这是不可能的。构造函数调用必须是第一条语句
您可以通过以下链接:一个选项是从默认构造函数调用参数化构造函数
C(){
this(0, "Blank");
}
C(int i){
this(i,"Blank");
}
C(int i, String s){
this.i = i;
this.s = s;
}
此模式将允许您向更具体的构造函数提供参数较少的构造函数的默认值
另外,请注意,链接构造函数必须作为另一个构造函数中的第一个调用来完成-初始化变量后不能调用另一个构造函数:
C(int i, String s){
this.i = i;
this.s = s;
this(); // invalid!
}
如果你真的想做这样的事情,考虑一个init方法:
C() {
init();
}
C(int i, String s) {
this.i = i;
this.s = s;
init();
}
一个选项是从默认构造函数调用参数化构造函数
C(){
this(0, "Blank");
}
C(int i){
this(i,"Blank");
}
C(int i, String s){
this.i = i;
this.s = s;
}
此模式将允许您向更具体的构造函数提供参数较少的构造函数的默认值
另外,请注意,链接构造函数必须作为另一个构造函数中的第一个调用来完成-初始化变量后不能调用另一个构造函数:
C(int i, String s){
this.i = i;
this.s = s;
this(); // invalid!
}
如果你真的想做这样的事情,考虑一个init方法:
C() {
init();
}
C(int i, String s) {
this.i = i;
this.s = s;
init();
}
您可以将所有代码从主构造函数移动到某个方法,例如mainProcessing()
现在,在参数化构造函数2中,可以在所需位置调用此方法mainProcessing()
C(int i, String s)
{
System.out.println("In parameterized constructor 2");
this.i = i;
this.s = s;
mainProcessing();
}
您可以将所有代码从主构造函数移动到某个方法,例如mainProcessing()
现在,在参数化构造函数2中,可以在所需位置调用此方法mainProcessing()
C(int i, String s)
{
System.out.println("In parameterized constructor 2");
this.i = i;
this.s = s;
mainProcessing();
}
因此,我应该从默认构造函数中复制/粘贴代码,以实现我所需的功能?是的,如果您想在完成当前构造函数中的工作后调用另一个构造函数,因为这是用this()无法实现的。尽可能避免复制/粘贴-这不是一个好习惯,以后会给您带来麻烦。您可以一直这样做。@mtk,在这种情况下,您需要重新考虑逻辑,复制粘贴不好。因此,我应该从默认构造函数复制/粘贴代码,以实现我需要的功能?是的,如果您想在完成当前构造函数中的工作后调用另一个构造函数,因为使用此()无法实现.尽可能避免复制/粘贴-这不是一个好习惯,以后会让你头疼。你可以一直这么做。@mtk,在这种情况下,你需要重新考虑逻辑,复制粘贴不好