【问题标题】:What does "this()" method mean?“this()”方法是什么意思?
【发布时间】:2013-03-29 20:43:10
【问题描述】:

我遇到了这段代码,有这一行我不明白它的含义或它在做什么。

public Digraph(In in) {
    this(in.readInt()); 
    int E = in.readInt();
    for (int i = 0; i < E; i++) {
        int v = in.readInt();
        int w = in.readInt();
        addEdge(v, w); 
    }
}

我知道this.method()this.variable 是什么,但this() 是什么?

【问题讨论】:

  • @Avi 我只是觉得这听起来很熟悉。

标签: java constructor this constructor-overloading


【解决方案1】:

这是构造函数重载:

public class Diagraph {

    public Diagraph(int n) {
       // Constructor code
    }


    public Digraph(In in) {
      this(in.readInt()); // Calls the constructor above. 
      int E = in.readInt();
      for (int i = 0; i < E; i++) {
         int v = in.readInt();
         int w = in.readInt();
         addEdge(v, w); 
      }
   }
}

您可以通过缺少返回类型来判断此代码​​是构造函数而不是方法。 这与在构造函数的第一行调用super() 以初始化扩展类非常相似。您应该在构造函数的第一行调用this()(或this() 的任何其他重载),从而避免构造函数代码重复。

你也可以看看这个帖子:Constructor overloading in Java - best practice

【讨论】:

  • Digraph 还是Diagraph?是否可以为构造函数重载使用不同的名称?
【解决方案2】:

使用 this() 作为这样的函数,本质上是调用类的构造函数。这允许您在一个构造函数中进行所有通用初始化,并在其他构造函数中进行特化。因此,例如在这段代码中,对this(in.readInt()) 的调用正在调用具有一个 int 参数的 Digraph 构造函数。

【讨论】:

    【解决方案3】:

    这段代码sn-p是一个构造函数。

    这个对this的调用调用了同一个类的另一个构造函数

    public App(int input) {
    }
    
    public App(String input) {
        this(Integer.parseInt(input));
    }
    

    在上面的例子中,我们有一个使用int 的构造函数和一个使用String 的构造函数。采用String 的构造函数将String 转换为int,然后委托给int 构造函数。

    请注意,对另一个构造函数或超类构造函数 (super()) 的调用必须是构造函数的第一行。

    也许可以查看this 以获得关于构造函数重载的更详细说明。

    【讨论】:

      【解决方案4】:

      几乎一样

      public class Test {
          public Test(int i) { /*construct*/ }
      
          public Test(int i, String s){ this(i);  /*construct*/ }
      
      }
      

      【讨论】:

      • 你的 cmets 弄乱了你的右括号
      • @Alex 那是因为为了清晰起见,我从 OP 中复制了它们。
      【解决方案5】:

      调用this 本质上是调用构造函数类。 例如,如果您要扩展某些东西,而不是与add(JComponent) 一起,您可以这样做:this.add(JComponent).

      【讨论】:

        【解决方案6】:

        Digraph 类的另一个构造函数,带有 int 参数。

        Digraph(int param) { /*  */ }
        

        【讨论】:

          【解决方案7】:

          构造函数重载:

          例如:

          public class Test{
          
              Test(){
                  this(10);  // calling constructor with one parameter 
                  System.out.println("This is Default Constructor");
              }
          
              Test(int number1){
                  this(10,20);   // calling constructor with two parameter
                  System.out.println("This is Parametrized Constructor with one argument "+number1);
              }
          
              Test(int number1,int number2){
                  System.out.println("This is Parametrized  Constructor  with two argument"+number1+" , "+number2);
              }
          
          
              public static void main(String args[]){
                  Test t = new Test();
                  // first default constructor,then constructor with 1 parameter , then constructor with 2 parameters will be called 
              }
          
          }
          

          【讨论】:

            【解决方案8】:

            this(); 是构造函数,用于调用类中的另一个构造函数, 例如:-

            class A{
              public A(int,int)
               { this(1.3,2.7);-->this will call default constructor
                //code
               }
             public A()
               {
                 //code
               }
             public A(float,float)
               { this();-->this will call default type constructor
                //code
               }
            }
            

            注意: 我没有在默认构造函数中使用this()构造函数,因为它会导致死锁状态。

            希望对你有帮助:)

            【讨论】:

            • 死锁...?这段代码与多线程有关吗?不是递归吗?
            猜你喜欢
            • 1970-01-01
            • 2011-08-22
            • 1970-01-01
            • 2018-10-29
            • 2011-06-20
            • 2012-05-04
            • 1970-01-01
            • 1970-01-01
            • 2012-02-06
            相关资源
            最近更新 更多