【问题标题】:Any way to call the default constructor from a parameterized constructor?有什么方法可以从参数化构造函数调用默认构造函数?
【发布时间】:2013-01-03 16:46:16
【问题描述】:

假设,我有以下代码

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) 在这种情况下)调用主(默认)构造函数吗?

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

注意

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

编辑

我看到this post,它说将this() 作为第一行将调用默认构造函数。但是我需要在设置值之后调用它。

【问题讨论】:

    标签: java default-constructor parameterized-constructor


    【解决方案1】:

    调用this() 会起作用,但请注意这必须是构造函数中的第一条语句。例如:下面的代码是非法的,不会编译:

    class Test {
        void Test() { }
        void Test(int i) {
            i = 9;
            this();
        }
    }
    

    【讨论】:

    【解决方案2】:

    一个选项可以是从默认构造函数调用参数化构造函数。

    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();
    }
    

    【讨论】:

      【解决方案3】:

      在其他构造函数中调用this()作为第一条语句就足够了。

      C(int i, String s)
      {
         this();
         // other stuff.
      }
      

      【讨论】:

        【解决方案4】:

        引用文档:

        另一个构造函数的调用必须第一行在 构造函数。

        所以不,这是不可能的。在第一行使用 this()。

        【讨论】:

          【解决方案5】:

          您可以将所有代码从主构造函数移动到某个方法,例如 mainProcessing()。

          C()
          {
              System.out.println("In main constructor");
              mainProcessing();
          }
          
          private void mainProcessing()
          {
              // Move your code from main constructor to here.
          }
          

          现在在参数化构造函数 2 中,您可以在所需位置调用此方法 mainProcessing()。

          C(int i, String s)
          {
              System.out.println("In parameterized constructor 2");
              this.i = i;
              this.s = s;
              mainProcessing();
          }
          

          【讨论】:

            【解决方案6】:

            只需调用构造函数 ny this();声明作为默认构造函数的第一行....

            【讨论】:

              【解决方案7】:

              可以使用this();调用默认构造函数

              C(int i, String s){
                 this(); // call to default constructor
                 // .....       
              }
              

              【讨论】:

                【解决方案8】:

                使用 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?
                }
                

                【讨论】:

                  【解决方案9】:

                  您可以在第一条语句中调用this(),在任何参数化构造函数中调用默认构造函数。

                  注意this() 必须是构造函数定义的第一行

                  C(int i, String s){
                     this();
                      . . . . 
                  }
                  

                  但我需要在设置值之后调用它。

                  这是不可能的。构造函数调用必须是第一条语句。

                  你可以通过这个链接:constructor invocation must be the first line

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2020-10-10
                    • 1970-01-01
                    • 2013-03-20
                    • 2012-07-27
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多