Learn Programming Bar programming It tutorial Java Construction method overload tutorial released, Welcome to visit through Xuebiancheng8.com.
In the previous analysis of what is the construction method, the constructor is to instantiate the object when the code is the construction method to complete the initialization of the object, then you think is not in a class can only have a construction method, the answer is no, there can be more than one constructor in a class, These construction methods form the overloads of the method, and let's look at what the constructor method overloads are. Let's look at an example:
public class person{
private int age;
private String name;
Public person () {
SYSTEM.OUT.PRINTLN ("Non-parametric construction method");
}
public person (int age) {
This.age = age;
}
public person (int age,string name) {
This.age= age;
THIS.name = name;
}
}
Okay, so we've written three constructs, one for each parameter, and two for the argument.
So when does the three construction methods actually execute?
Person p = new person ();
Call the parameterless construction method above
Person p = new person (19);
The method of constructing a parameter is called above
Person p = new person ("Zhangsan");
The specific invocation of the constructor method is based on the parameters passed in and the constructor method matches, then the method is executed.
Please visit xuebiancheng8.com for details. The URL is:
Http://xuebiancheng8.com/play/goodgoodstudy_95_daydayup.html
How to construct the Java tutorial overloaded