Construction Method Details Summary ~~~~~
1: First to understand why you need to construct a method ,, there are too many properties in the class, each time to assign a value to the property is very troublesome: the encoding is large, you cannot reuse the code to assign value to the property.
2: What is a construction method?
The constructor method is responsible for initializing the instance variables in the class.
The constructor method is a special method that must satisfy the following syntax rules:
A: The construction method must be the same as the class name, and does not contain a return value type Description section.
B: The construction method cannot have static and final keyword adornments.
3: How do I use the construction method? call construction method using the New keyword
4: Display construction method and implicit construction method
When you create a class, if you do not explicitly define a construction method, there is a default parameterless construction method for that class;
You can declare one or more arguments in a class, but each construction method has a difference in the number of arguments or parameter data Types!!!
!!!!!!!!!!
If an explicit construction method exists in a class, the default parameterless construction method will no longer exist unless you explicitly define an parameterless construction method
There can be more than one constructor in a class, the constructor can be called through this implementation, but the calling constructor code must be written in the first line of the valid code, which is very important oh ~
1 Packageconstructor;2 3 Public classApple {4 5 PrivateString name;6 PrivateString color;7 Private DoublePrice ;8 9 //Non-parametric construction methodTen PublicApple () { One A } - - PublicApple (String name) { the This. Name =name; - } - - //multiple constructor methods, which are identical to the type of the parameter and the type of the parameter in the constructor method when called + PublicApple (string name, string color,DoublePrice ) { - This(name);//the call in the constructor method is called with this (parameter), but note that a call to another constructor method can only be made once in a constructor method. + This. color =color; A This. Price =Price ; at } - - Public Static voidMain (string[] args) { -Apple apple1=NewApple ("Apple"); -Apple apple2=NewApple ("Apple", "Red", 12.5); - } in - to}
Summary of considerations for Java construction methods