Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
In methods and data members, we mentioned that when an object in Java is createdInitialization). During initialization, the data member of the object is assigned an initial value. We can determine an initial value when declaring a data member, which is calledExplicit initialization. If we do not assign an initial value to a data member, the data member usesDefault Value.
In addition to the above two initialization methods, we can also useConstructor)To initialize the object. In addition to initializing data members, the constructor can also specify specific operations. These operations are automatically executed when an object is created.
Define Constructor
Like normal methods, we define constructor methods in the class. The constructor has the following basic features:
- The constructor name is the same as the class name.
- No return value for the constructor
We define the construction method of the human class:
Public Class Test { Public Static Void Main (string [] ARGs) {HUMAN aperson = New Human (160 ); System. Out. println (aperson. getheight ());}} Class Human {
/**
* Constructor
*/
Human ( Int H ){ This . Height = H;
System. Out. println ("I'm born ");} /** * Accessor */ Int Getheight (){ Return This . Height ;} Int Height ;}
The aboveProgramPrinted
I'm born
160
Let's first look at the definition of the constructor. Constructor can receive parameter lists like normal methods. Here, the constructor human () receives an integer as a parameter. In the body of the method, we assign the integer parameter to the height of the data member. Since the constructor does not return values, the constructor does not need to specify the type of returned values. The main function of constructor is:
- Provide initial values for data membersThis. Height =H;
- Perform specific initial operationsSystem. Out. println ("I'm born ");
Let's look at the call of the constructor. We usedNew human (). In fact, we are calling the human class constructor. When we do not define this method, Java will provide a blank constructor to useNew. However, when we define a constructor, Java calls the defined constructor when creating an object. During the call, we provide a parameter of 160. The final running result also shows that the object height is indeed initialized to 160.
Initial method priority
In methods and data members, we can see that if we provide explicit initial values, the data members will adopt explicit initial values instead of default initial values. However, if we provide both an explicit initial value and initialize the same data member in the constructor, the final initial value willDetermined by the constructor. For example:
Public Class Test { Public Static Void Main (string [] ARGs) {HUMAN aperson = New Human (160 ); System. Out. println (aperson. getheight ());}} Class Human {
/**
* Constructor
*/Human ( Int H ){ This . Height = H ;} /** * Accessor */ Int Getheight (){ Return This . Height ;} Int Height = 170 ;// Explicit initialization}
The running result is:
160
The final initialization value of the object is the same as the value in the build method. Therefore:
Build method> explicit initial value> default initial value
(In fact, the so-called priority is related to the execution sequence during initialization. I will go into this point later)
Method overload
More than one constructor can be defined in a class, for example:
Public Class Test { Public Static Void Main (string [] ARGs ){ Human Nezha = New Human (150, "shit" ); System. Out. println (Nezha. getheight ());}} Class Human { /** * Constructor 1 */ Human ( Int H ){ This . Height = H; system. Out. println ( "I'm born" );} /** * Constructor 2 */ Human ( Int H, string s ){ This . Height =H; system. Out. println ( "Ne Zha: I'm born," + S );} /** * Accessor */ Int Getheight (){ Return This . Height ;} Int Height ;}
Running result:
Ne Zha: I'm born, shit
150
The preceding two constructor methods are defined, and the names are all human. The two constructor methods have different parameter lists.
When using new to create an object, Java willProvided ParametersTo determine which constructor to build. For example, when constructing Nezha, we provide two parameters: integer 150 and string "shit", which correspond to the parameter list of the second build method, so Java will call the second build method.
In JavaMethod NameAndParameter ListTo determine the method to be called. This is calledMethod Overloading). The build method can be overloaded, or a common method can be overloaded, for example, the following breath () method:
Public Class Test { Public Static Void Main (string [] ARGs) {HUMAN aperson = New Human (); aperson. Breath ( 10 );}} Class Human { /** * Breath () 1 */ Void Breath () {system. Out. println ( "Hu ..." );} /** * Breath () 2 */ Void Breath ( Int Rep ){ Int I; For (I = 0; I <rep; I ++ ) {System. Out. println ( "Lu... lu ..." );}} Int Height ;}
Running result:
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
We can see that the Second Breath () method that matches the parameter list is called because an integer 10 is provided during the call.
Summary
Constructor features: Same name as class, no return value
Constructor objective: initialization and initial operations
Method overload: method name + parameter list-> which method is actually called
Welcome to continue reading the "Java quick tutorial" SeriesArticle