What does “this()” method mean?

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

提交回复
热议问题