What does “this()” method mean?

后端 未结 8 2011
眼角桃花
眼角桃花 2020-12-13 03:33

I ran into this block of code, and there is this one line I don\'t quit understand the meaning or what it is doing.

public Digraph(In in) {
    this(in.readI         


        
相关标签:
8条回答
  • 2020-12-13 04:09

    Using this() as a function like that, essentially calls the Constructor of the class. This allows you to all the generic initializations in one constructor and have specializations in others. So in this piece of code for example, the call to this(in.readInt()) is calling the Digraph constructor that has one int argument.

    0 讨论(0)
  • 2020-12-13 04:12

    This code snippet is a constructor.

    This call to this calls another constructor of the same class

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

    In the above example we have a constructor that takes an int and one that takes a String. The constructor that takes a String converts the String to an int and then delegates to the int constructor.

    Note that a call to another constructor or a superclass constructor (super()) must be the first line in a constructor.

    Maybe take a look at this for a more detailed explanation of constructor overloading.

    0 讨论(0)
  • 2020-12-13 04:18

    An other constructor of the class Digraph with an int parameter.

    Digraph(int param) { /*  */ }
    
    0 讨论(0)
  • 2020-12-13 04:22

    Calling this essentially calls the class Constructor. For example, if you're extending something, than along with add(JComponent), you could do: this.add(JComponent).

    0 讨论(0)
  • 2020-12-13 04:22

    this(); is constructor which is used to call another constructor in a class, for example:-

    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
       }
    }
    

    Note: i did not use this() constructor in default constructor because it will lead to deadlock state.

    Hope this will help you:)

    0 讨论(0)
  • 2020-12-13 04:27

    This is constructor overloading:

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

    You can tell this code is a constructor and not a method by the lack of a return type. This is pretty similar to calling super() in the first line of the constructor in order to initialize the extended class. You should call this() (or any other overloading of this()) in the first line of your constructor and thus avoid constructor code duplications.

    You can also have a look at this post: Constructor overloading in Java - best practice

    0 讨论(0)
提交回复
热议问题