Today I would like to share with you about the construction method of knowledge!
Definition and function of construction method
A construction method is a special type of method . When an object is created, the constructor is used to initialize the object, that is, the construction method is actually a noun rather than a verb, as I have just begun to contact the construction method as a verb to understand, it is more troublesome. The constructor method is the same as the name of the class it is in, but the constructor does not return a value, so you cannot use the return statement in the constructor method to return a value.
The syntax format for the constructor method is as follows:
[Access modifier]< class name > ([parameter list]) { construct method's statement body }
Where the modifier can be public,protect,private; The argument list is an argument, it can be empty, and the body of the statement that constructs the method can be empty.
The construction method is very useful in programming, and he can initialize the member variables of the class. When an instance object of a class is first created, the constructor of the class is called, and the code to complete the initialization work can be added to this method. Regardless of whether you have customized the constructor method, all classes have a constructor method, because Java automatically provides a default constructor method, which has no parameters and no code in its method body, which initializes all members to 0 and does nothing else. However, once we have defined our own construction methods, the default constructor is invalidated.
Here is an example of how to use a constructor:
A simple construction method class demo{ int x; Demo () {//construction method x=1; }}
How to invoke a constructor method to initialize an object:
public class mydemo{public static void Main (String args[]) { Demo a = new demo (); Demo B = new demo (); System.out.println (a.x+ "" +b.x);} The operating structure is: 1 1
The above construction method is a non-parametric construction method, the following is for you to write a parameterized construction method:
Class Demo {//A simple construction method int x; Demo (int a) {//Parameter construction method x=a; }}
A call to a constructor method is similar to a parameterless construction method:
public class mydemo{public static void Main (String args[]) { Demo a = new demo (5); Demo B = new Demo (ten); System.out.println (a.x+ "" +b.x);} Operation results are: 5 10
Here's a more specific example to show you how to use the constructor:
Class Employee{private Double employeesalary = 2000;public Employee () {//A constructor method System.out.print ("The constructed method has been called");} public void Getsalary () {System.out.println ("The basic salary of the employee is:" +employeesalary);} public static class demo{public static void Main (string[] args) { Employee A1 = new Employee (); A1.getsalary (); Employee A2 = new employee (); A2.getsalary ();}}} The result of the operation is: the constructor method is called! The basic salary of the staff is: 2000.00
Constructor method is called! The basic salary of the staff is: 2000.00
We can see that the employee () method is called automatically every time an employee object is created, as shown in the example running results. This is the function of the construction method and the result of the operation.
(11.13) Java trivia!