- A constructor method is a special method that is a method that has the same name as the class and no return value type (even void) .
- The creation of an object is accomplished by means of a construction method, whose function is to initialize the object .
- Constructor methods are called automatically when a class instantiates an object. Construction methods can be overloaded as well as other methods.
- There are two kinds of construction methods: The method of constructing method with no parameters
- Class must have a construction method, if not written, the system automatically add a non-parametric structure. (Interfaces cannot be instantiated, so there is no construction method in the interface.) )
Example Demo: Calculate the distance from a coordinate point
Entity class Point Code:
1 Public classPoint {2 3 //properties of points horizontal ordinate4 intx;5 inty;6 7 //default no parameter constructor8 PublicPoint () {9SYSTEM.OUT.PRINTLN ("Default construct. ");Ten } One A //The structure of the reference 1 - PublicPoint (intAintb) { -System.out.println ("with a reference structure. "); the //Assigning a value to an object -x =A; -y =b; - } + //Parametric Construction 2 (heavy duty) - PublicPoint (inta) { + //System.out.println (); A This(A,a);//the overloaded call above has the parameter construct 1 (this syntax is specifically used to construct a call between methods and must be written on the first line of the constructor method) at } - - /** - * Calculation method of point distance - * Parameters are: Non-point sitting punctuation object - * Method Overloading in */ - Public DoubleDistance () {//No reference to returnMath.sqrt (Math.pow ( This. x, 2) + Math.pow ( This. Y, 2));//the distance from the original point + } - the //the distance to a point * Public DoubleDistanceintAintb) {//parameter is point coordinate $ returnMath.sqrt (Math.pow ( This. X-a, 2) +math.pow ( This. Y-b, 2));Panax Notoginseng } - the Public DoubleDistance (point P) {//parameter is a point object + returndistance (p.x, p.y); A } the +}
Demo:
1 Public classPointdemo {2 Public Static voidMain (string[] args) {3 4Point P1 =NewPoint (3,2);5System.out.println ("p1" coordinates are: "+" ("+ p1.x +", "+ P1.y +") ");6 7Point P2 =NewPoint (5);8System.out.println ("p2" coordinates are: "+" ("+ p2.x +", "+ P2.y +") ");9 Ten /** One * To find the distance to the point A */ -Point P =NewPoint (6,8); -System.out.println ("Distance to Origin:" + p.distance ());//the distance from the original point theSystem.out.println ("Distance to another point is:" + p.distance (3, 3));//distance to (3,3) -System.out.println ("The distance to a point object is:" + p.distance (p2));//distance to point object P2 - } -}
The output is:
There is a reference structure.
The coordinates of P1 are: (3,2)
There is a reference structure.
The coordinates of P2 are: (5,5)
There is a reference structure.
The distance from the origin point is: 10.0
Distance to another point is: 5.830951894845301
The distance to a point object is: 3.1622776601683795
The construction method of Java Foundation and its application