[Javase Study Notes]-7.5 constructor details
In this section, we will briefly introduce several details that need to be paid attention to when using constructors.
Through the previous sections, we have a clear understanding of the constructor. When we create an object, we call the constructor. So what details should we pay attention to when defining and calling constructor?
The first detail should be noted: constructor and set Method
Let's look at the two function codes:
Class Person {private String name; private int age; // constructor, initializing namePerson (String n) {name = n; System. out. println (name + "age" + age);} // General function, set namepublic void setName (String n) {name = n ;}}We can see that there are two functions above. The first is the constructor, and the second is the general method. Both are the content for setting the name. Can we use the constructor to replace the set method, obviously, this is not good, because we have learned the difference between constructor and general function, and the set Method is a general function. Here, the constructor only initializes the name once and then does not function. The set method can be called whenever we need to change the name.
The second detail should be noted: constructor can call general functions, but general functions cannot directly call constructor.
Let's look at the example:
Class Person {private String name; private int age; // constructor, initializing namePerson (String n) {setName (n); System. out. println (name + ":" + age) ;}// General function, set namepublic void setName (String n) {name = n ;}}Compilation passed. Let's test it.
Class PersonTest {public static void main (String [] args) {Person p = new Person ("Xiaoqiang ");}}Result:
It is clear that the constructors can call general functions.
When we call the constructor in the set Method:
Class Person {private String name; private int age; // constructor, initializing namePerson (String n) {name = n; System. out. println (name + ":" + age) ;}// General function, set namepublic void setName (String n) {Person (n); name = n ;}}Result:
We can see that the compilation is not successful, so the constructors cannot be called directly in general functions.
Third, pay attention to the details: a general method with the same name as the class name
Let's look at a function:
class Person{private String name;private int age;void Person(String n){name = n;System.out.println(name+":"+age);}}Test:
Class PersonTest {public static void main (String [] args) {Person p = new Person ("Xiaoqiang ");}}Result:
We can see the prompt that the constructor cannot be applied to a given type, but there are no parameters, but our code contains the parameter "Xiaoqiang", that is, the function used to create an object is not a constructor, it is a general function. The program does not find the corresponding constructor in the Person class. Because the constructor is not defined in our code, only the default constructor is available, the default constructor does not have any parameters. Therefore, we must pay attention to this phenomenon in the future programming process.
The fourth detail should be noted: the constructor has a return statement.
Let's look at the code to illustrate the problem
Class Person {private String name; private int age; Person (String n, int a) {if (a <0) {System. out. println ("Initialization is illegal! "); Return;} name = n; age = aSystem. out. println (name + ":" + age); return; // we learned from the function. The return statement in the function without return values can be omitted }}Let's test it.
Class PersonTest {public static void main (String [] args) {Person p = new Person ("Xiaoqiang",-1 );}}Result:
We found that the program is compiled and runs normally, and the return Statement jumps out of the constructor. That is to say, the constructor has a return statement.
In this section, we learned the details that need to be paid attention to in the four constructors. We will pay more attention in the future programming process.