In this section, we'll simply say a few details that you need to be aware of when using constructors.
Through our previous sections of learning, we have a clearer idea of the constructors when we create objects. We'll call the constructor. So when we define and invoke constructors. What details do you need to pay attention to?
The first detail to note: constructors and Set methods
Look at two function codes:
Class Person{private string Name;private int age;//constructor, initialization of Nameperson (string n) {name = N; System.out.println (name+ "age" +age); General function, set namepublic void SetName (String n) {name = n;}}
We see that there are two functions above, the first one is the constructor, the second is the general method, and all two are for setting the name, then we can use the constructor instead of the set method, it is very obvious. This is not enough because we have previously learned the difference between constructors and general functions. The Set method is a general function.
The constructor here simply initializes the name, and then it doesn't work anymore. The set method can be called whenever we need to change the name.
The second detail to note: constructors can call general functions, but general functions cannot call constructors directly.
Let's look at some examples:
Class person{private String name;private int age;//constructor. Initialize Nameperson (String N) {setName (n); System.out.println (name+ ":" +age);} General functions. Set namepublic void SetName (String n) {name = n;}}
Compiled by. Let's test it.
Class Persontest {public static void main (string[] args) {person p = new Person ("Johnny");}}
Results:
It is very obvious that the constructor calls the general function to be able.
When we call the constructor in the set method:
Class Person{private string Name;private int age;//constructor, initialization of Nameperson (string n) {name = N; System.out.println (name+ ":" +age);} General function, set namepublic void SetName (String n) {person (n); name = n;}}
Results:
We see that the compilation is straightforward, so it is not possible to call constructors directly in a general function.
Third detail to note: General methods with the same name as the class names
Let's look at a function like this:
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 ("Johnny");}}
Results:
We see a hint that the constructor cannot be applied to the given type. There are no actual parameters, but our code has the reference "Xiao Qiang", which means that the function we are creating the object with is not a constructor. Instead of a generic function, the program does not find the corresponding constructor in the person class, because the constructor is not defined in our code, then there is only a default constructor, and the default constructor is not a parameter.
Therefore, we must pay attention to this phenomenon in the future programming process.
The fourth detail to note: There is a return statement in the constructor.
We look at the code to explain 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 studied in the function. The return statement in a function that has no return value can be omitted}}
Let's test it.
Class Persontest {public static void main (string[] args) {person p = new person ("Xiao Qiang",-1);}}
Results:
We found that the program was compiled and executed properly. The return statement jumps out of the constructor, which means there is a return statement in the constructor.
In this section we learn the details that are often required in the four constructors, and we pay more attention to it later in the programming process.
[Javase Learn notes]-7.5 Some details of the constructor