What does “this()” method mean?

后端 未结 8 2012
眼角桃花
眼角桃花 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:31

    It's nearly the same

    public class Test {
        public Test(int i) { /*construct*/ }
    
        public Test(int i, String s){ this(i);  /*construct*/ }
    
    }
    
    0 讨论(0)
  • 2020-12-13 04:31

    Constructor Overloading:

    ex:

    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 
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题