有什么方法可以从参数化构造函数调用默认构造函数吗?

mtk*_*mtk 1 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");
    }

    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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道,有什么方法可以从参数化构造函数(即C(int i, String s)在本例中)调用主(默认)构造函数?

或者我只是将主(默认)构造函数中的全部内容复制粘贴到参数化构造函数中,如上面代码中的注释所示?

笔记

我需要在变量之后调用默认构造函数i,并s在参数化构造函数中设置,因为处理涉及这些变量。

编辑

我看到这篇文章,其中说放置this()为第一行将调用默认构造函数。但我需要在设置值后调用它。

Pra*_*mha 5

调用this()可以,但请注意这必须是构造函数中的第一个语句。例如:下面的代码是非法的并且无法编译:

class Test {
    void Test() { }
    void Test(int i) {
        i = 9;
        this();
    }
}
Run Code Online (Sandbox Code Playgroud)