Java constructor and object creation
You can use classes to declare objects, and after declaring objects, you must create objects
1Construction method
First, let’s talk about what a construction method is. Since it is said that this is a construction method, it is obviously an essentially a method.
So, since as a method, it should look like a method. It has no other code to define methods except for calling back a Class();? This is because, without customizing the constructor to the class, the compiler will automatically add the default constructor to it during the compilation period.
(1) When a program creates an object with a class, it is necessary to use the constructor method of the class
(2) The name of the constructor in the class must be exactly the same as the class name, and there is no type
(3) Allow several construction methods to be written in a class, but the parameters must be ensured that different parameters are (the number of parameters is the same but the corresponding parameter type in the parameter list is different; the number of parameters is different)
(4) If no constructor is written in the class, the system will default that the class has only one constructor (no parameters, no statements in the method body)
1.1. Default constructor and custom constructor
If one or more constructors are defined in the class, Java does not provide the default constructor.
1.2. The constructor has no type
2Create an object
2.1. Object declaration
Class name object name
//Example: Person person;
2.2. Assign variables to declared objects
Use the new operator and class constructor to assign variables to declared objects, that is, to create objects
//Example: Assign variables to declared objects public class Example4_2_Point { int x; int y; Example4_2_Point(int x, int y) { this.x = x; this.y = y; }} public class Example4_2 { public static void main(String[] args) { // TODO Auto-generated method stub Example4_2_Point example4_2_Point1 = new Example4_2_Point(10, 10);//Declare the object, assign variables to the object using new and constructor methods in the class Example4_2_Point example4_2_Point2 = new Example4_2_Point(23, 25);//Declare the object, assign variables to the object using new and constructor methods in the class}}2.3. Object memory model
2.4 Using Objects
"・ " operator: By using the "・ " operator, you can access and call your own variables and methods
1. The object operates its own variables (reflects the object's properties)
(1) By using the "・" operator, you can achieve access to your own variables and call methods
(2) The dot operator is also called a reference operator or access operator. Format: object and method
3. Object calls methods in the class (reflects the behavior of the object)
3.1 Object references and entities (omitted)
Code example:
//Default constructor, custom constructor, non-constructor method example class Lader(){int x ,y;//Method 1: Default constructor Lader(){}//Method 2: Custom constructor Lader(){x = 1;y = 1;}//Method 3: Custom constructor Lader(int a,int b){x = a;y = b;}//Method 4: This method is type void, so it is not a constructor void Lader(int a,int b){x = a;y = b;}}Summarize
The above is all the detailed explanation of Java programming construction methods and object creation in this article. I hope it will be helpful to everyone. If you have any questions, you can leave a message at any time. The editor will reply to everyone in time and look forward to your valuable opinions.