java why sometimes a class has multiple constructors

Construction method is generated when the object is automatically invoked java system, we can not call other methods like, like to call the constructor (which must be called by keyword new automatic) in the program. But we can call a constructor in other overloaded constructor, not by the constructor name, but the form this (argument list), according to the parameter list of them, select the appropriate constructor. E.g:     
public class Person{
        String name;
        int age;
        public Person(String name){
            this.name = name;
        }
        public Person(String name,int age){
            this(name);
            this.age = age;
        }
    }

Not a class has multiple constructors , but the overloaded constructor this many times. You can read about method overloading this concept.

Construction method overloading specific benefits is this: I have thrown in is what kind of parameters, the program will automatically find the corresponding construction method of execution. Such constructors can achieve a lot of features. Otherwise, a function to write a class, and more trouble ah.

 

https://blog.csdn.net/qq_14989227/article/details/79550607

Guess you like

Origin www.cnblogs.com/baxianhua/p/12167980.html
Recommended