Constructor in java-program initialization

Java continues to automatically call the constructor (a special method) when creating an object in C++. When creating an object, if its class has a constructor, Java will automatically call the corresponding constructor before the user has the ability to manipulate the object, thus Ensure that the initialization is carried out.
1. Introduction
(1) Definition
1. Construction method
Consistent with the class name, there is no special method for returning the type, which is used to initialize the object.
2. Syntax :
access modifier construction method name (){ //initialization code } 3. Example When creating an object-new family(), storage space will be allocated for the object and the corresponding constructor will be called. This ensures that the object is properly initialized before it is manipulated. (2) Features



Insert picture description here


Insert picture description here

1. The name of the method and the class are the same, that is, the constructor uses the same name as the class;
reason 1. When the editor calls the constructor, it needs to know which initialization method is called;
reason 2. Any name may be The member names conflict;
instead of this, it is easier to use the C++ language solution (constructor name = class name).
2. There is no return value type after the modifier; the
constructor is a special type of method because it has no return value.
The difference with the return value of void is as follows:
Null return value method: The method itself does not return a value, but you can choose to let him return other parameters;
Constructor: Will not return anything (if the constructor has a return value, you are allowed to choose Return type, then the compiler will not know how to deal with).
3. Parameters can be specified or not specified in the method.
(3) Benefits The
constructor helps reduce errors and makes the code easier to read. Conceptually, "initialization" and "creation" are independent of each other, but in java code, the two concepts are bundled together, and the two cannot be separated.
2. Examples
(1) No-parameter constructor (also known as: default constructor) A constructor that
does not accept any parameters is called a default constructor (ie, no-parameter constructor).
1. When there is no method in a constructor, the system defaults to add a no-parameter construction method with the same class name , as shown in the following figure:
Insert picture description here
2. When you need to customize a no-parameter constructor, you can do it yourself according to your own needs Edit, the example in the first part above is a custom no-parameter structure, which initializes a family and contains two persons: father and mother by default.
(2) Constructor with parameters
Define a class with a constructor with parameters, then it will accept the incoming variables, the editor will not automatically create a default constructor (because the compiler thinks you already know what to do, no need to default Constructor) , in the family class as shown below, a family parameterized constructor is defined, and "son" and "daughter" are respectively passed in to call. In this case, if you call the constructor without parameters, an error will be reported and tell You cannot find a matching constructor.
Insert picture description here
(3) Constructors with no parameters and constructors with parameters exist at the same time.
If the constructor is the only constructor in the class, then the compiler will not allow new objects in other forms;
when there are multiple constructors in the class, you can Called when passing parameters. As shown in the figure below: the
Insert picture description here
call and execution results are as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44801116/article/details/105909833