Construction method
1. Create a new object by using the new + construct method
2. The constructor method is defined in the Java class as a method for initializing the object
Construction method does not return a value
Statement format for constructed methods
Use of non-parametric construction methods
Public class Test6 { publicstaticvoid main (string[] args) { new Car (); }} class car{ public Car () { // TODO auto-generated Constructor Stub System.out.println ("constructor method without parameters");} }
Operation Result:
No-Parameter construction method ~ ~ ~
The result of the operation shows that when an object is instantiated, the constructor method is actually called.
A method of constructing a parameter
Why is there a way to construct a parameter?
In fact, there is only one purpose, which is to initialize the value of the member variable.
classcar{inttread; intseat; PublicCar () {System.out.println ("No parameter construction method ~ ~ ~"); } PublicCar (intNewtread,intnewseat) {Tread=Newtread; Seat=newseat; System.out.println ("Tyre:" +tread+ "seat:" +seat+ "with parameter construction method ~ ~ ~); }} Public classTest6 { Public Static voidMain (string[] args) {Car C1=NewCar (4, 7);//call a parameter constructorCar C2 =NewCar ();//call the parameterless constructor }}
Operation Result:
Tires: 4 Seats: 7 construction method with parameters ~ ~ ~ No parameter construction method ~ ~ ~
Overloading of construction methods
Methods with the same method name but different parameters, the method is automatically selected according to the different parameters.
classcar{inttread; intseat; PublicCar (intnewtread) {Tread=Newtread; System.out.println ("Tyre:" +tread+ "non-parametric construction method ~ ~ ~"); } PublicCar (intNewtread,intnewseat) {Tread=Newtread; Seat=newseat; System.out.println ("Tyre:" +tread+ "seat:" +seat+ "with parameter construction method ~ ~ ~); }} Public classTest6 { Public Static voidMain (string[] args) {Car C1=NewCar (4, 7);//Call the second parameter constructor }}
Operation Result:
Tires: 4 Seats: 7 construction method with parameters ~ ~ ~
The running result shows that the program will choose which constructor to call based on the number of arguments passed in when the object is instantiated.
Constructing methods assigning values to objects
The construction method can not only assign values to the object's properties, but also ensure that the object is given a reasonable value.
classcar{inttread; intseat; PublicCar (intNewtread,intnewseat) { if(tread<4) {//cars have 4 wheels, and if they pass in 3 wheels they will automatically change to 4 wheels.Tread = 4; System.out.println ("Wrong input!" Auto correct to 4 "); } ElseTread=Newtread; Seat=newseat; System.out.println ("Tyre:" +tread+ "seat:" +seat+ "with parameter construction method ~ ~ ~); }} Public classTest6 { Public Static voidMain (string[] args) {Car C1=NewCar (3, 7);//Call the second parameter constructor }}
Operation Result:
wrong input! Auto correct for 4 tires:4 seats: 7 Parameter Construction method ~ ~ ~
Attention:
1. The parameter constructor and the parameterless constructor can coexist.
2. Call the parameter constructor or the parameterless constructor is to see if the object is instantiated when there are no parameters passed in, the number of parameters passed, the system automatically selected.
3. The constructor is called when there is a constructor, and the default constructor given by the system is not called.
4. When no construction method is specified, the system automatically adds an argument-less construction method.
5. When there is a specified construction method, no parameter construction method is automatically added, whether it is a constructor with a parameter or no argument.
Java Construction Methods