Overloaded constructors

帅比萌擦擦* 提交于 2019-12-25 16:24:27

问题


After reading overloaded constructors from a book, i tired the following code:

public class Employee {

    String name;
    int idNumber;

    public Employee(){
        this("JJ", 0);
        System.out.println(name +" "+ idNumber);
    }

    public Employee(String name, int num){

        this.name = name;
        idNumber = num;
        System.out.println(name +" 2nd "+ idNumber);
    }
}


public class Object1 {

    public static void main(String[] args) {

        Employee emp = new Employee();
    }

}

OUTPUT:

JJ 2nd 0

JJ 0

I am really confused. Why "JJ 2nd 0" printed out first then "JJ 0"?? I created an employee object emp and did not pass any args in the parameter, isn't suppose to call the first constructor first?


回答1:


new Employee(); is invoking

public Employee(){
    this("JJ", 0);
    System.out.println(name +" "+ idNumber);
}

In this constructor

this("JJ", 0);

is invoking

public Employee(String name, int num)

constructor, which ends with call

System.out.println(name +" 2nd "+ idNumber);.

which is responsible for printing

JJ 2nd 0

When this("JJ", 0); will finish System.out.println(name +" "+ idNumber); will be invoked and you should see another line

JJ 0




回答2:


When you wrote:

... new Employee();

You end up calling the default (no-argument) constructor. The first line of code in that constructor is:

this("JJ", 0);

Which calls the 2-parameter constructor, in which you write

System.out.println(name +" 2nd "+ idNumber);

This is the first of the two output statements your program encounters, and thus is the first thing you see in the console.

After that output statement, the program returns to the default no-argument constructor, and proceeds to execute your other output statement

System.out.println(name +" "+ idNumber);

Which is the "second" output statement you see.

If you step through your code with a debugger, go line by line, you will see exactly how the program is executing, and should demonstrate what I am referring to.




回答3:


The no-argument constructor is calling the other constructor with arguments with the following line:

this("JJ", 0);



回答4:


You will have to read about the this keyword for that , Just for starters its like and object of the class your'e using,

                       See in the first constructor this("JJ","0") means that a constructor having two arguments is being invoked so the first line redirects the control into the second constructor that's is why the other line is printed first.



回答5:


It do call the first constructor first, but the first line in it, is a call to the second constructor. When you create the Employee object from the main method, you call the constructor Employee(), with no arguments (the first one). That constructor first calls the second constructor, which prints JJ 2nd 0, and then, the first constructor prints the line JJ 0. This is how it gets executed:

  1. When the main method is being called, the first constructor, new Employee(), is called.
  2. The constructor calls the second constructor.
  3. The second constructor prints JJ 2nd 0, and then it's done.
  4. The first constructor continues with the next line, which is System.out.println(name +" "+ idNumber);, and prints JJ 0.


来源:https://stackoverflow.com/questions/27610274/overloaded-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!