当前位置:网站首页>Use of construction methods
Use of construction methods
2022-07-29 04:25:00 【Chinatown is full of bitter gourd faces】
Construction method
Constructor is a special method in class , It is usually created by the system ( Class instantiation ) Automatically call . The constructor is the first object to be
Method of execution , Mainly used to apply for memory 、 Initialize the member variables of the class . Although the constructor is also located in the class , But in many cases
Different from the ordinary member method , So some people think it is not a member method , And call it “ Constructors ”. The general form of the construction method is :
Constructor name ([ parameter list ]){
[this([ parameter list ]);] |[super([ parameter list ]);]
Statement sequence
}
among ,this Is to call other construction methods ,super Is to call the constructor of the parent class . They must be placed in front of other statements .
The compilation construction method should pay attention to the following points :
1. The name of the constructor must be exactly the same as the name of the class .
2. In addition to the access modifier , There cannot be any other modifiers , There can be no return value .
3. Although there is no return value , But it doesn't work “void” modification .
4. The construction method cannot be used static and final To modify . In general, it is not necessary to private modification , This will result in the inability to create objects externally .
5. Constructors cannot be explicitly called by objects . Usually by new Keyword to call , Or use this、super To call .
6. this() and super() Can't exist at the same time
7. The parameter list of the constructor can be empty , You can also have parameters . According to the presence or absence of parameters , It can be divided into the construction method without parameters and the construction method with parameters .
8. User defined classes can have multiple constructor methods , But the required parameter list is different .
9. If the user-defined class does not provide any constructor , The system will automatically provide a parameterless construction method .
1、 Definition and use of parameterless construction method
Examples of parameterless construction methods :
Calling the constructor uses “new Animal()”, This is an implicit call method . Notice the member variable , It has been given the beginning at the time of definition
value . In the constructor , First, output a message , And then again for x assignment . Because the execution of the construction method is after the member variable is defined , It will cover
The original initial value , So the value of the variable is 100.
If we add “void” It will look like this :
The output result is the initial value of the variable , It shows that the parameterless construction method we defined does not execute at all . This is because of the addition of “void” After the modifier ,
constructNoPara() It is no longer a construction method , It has become a common method . What we call is the default nonparametric constructor provided by the system itself
Law .
// The default nonparametric construction method provided by the system
public Animal() {
}
// This method actually does nothing , Naturally, it will not change x Value .
2、 Definition and use of construction method with parameters
In many cases , You need to assign different initial values to member variables according to different situations , This requires passing parameters to the constructor . therefore ,Java Zhongyunding
Construction method of semantic parameters , Moreover, this construction method with parameters can also define multiple ( The premise is that the parameter list is different ) This phenomenon is called the constructor
Overloading of law .
Java Regulations , If the programmer does not define a constructor , Then the system will automatically add a construction method without parameters . If the programmer goes to
Less than one construction method is defined , Then the system will no longer provide a construction method without parameters .
When used new To create an object , You need to provide type compatible parameters , Otherwise, the compiler will report an error .

Here, when defining the parameter construction method , At the same time, a parameter free construction method is defined , It is to let others create in the parameter free construction method that they habitually use
Object will not report an error , So even if you can't use this parameter free construction method, you have to define .
Be careful :
Method overloading : The method names in a class are the same , Different parameter list , It has nothing to do with the return value
Different parameter lists :
1 The number of parameters is different
2 Different types of parameters
3 The order of the parameters is different
This phenomenon is caused by the construction method , But not only constructors can be overloaded , Methods defined by ourselves can also be overloaded .
Advantages of overloading : It can be used when the method is called , Automatically select the method to be executed according to the type of parameter
3、this Keywords and constructor calls
this The role of keywords , Is used to call constructors explicitly . Its usage format is as follows :
Constructor name ([ parameter list ]){
[this([ parameter list ]);]
Statement sequence
}
The system will decide which constructor to call according to the parameter list . Use this You should also pay attention to the following points :
1. use this When a constructor is called , This statement can only be used in constructor .
2. this The statement must be the first statement in the constructor .
3. and new Different ,this Although you can call the constructor , But it just executes the statements in the constructor , Objects are not created .
4. this() and super() Can't exist at the same time

stay main() Method uses a parameterless construction method to create objects , In the construction method without parameters , use this To call the constructor with parameters , however
Then output a message . Be careful this The call constructor can only be used as the first statement . The reason for this is , In many cases , Multiple structures
Method may do the same thing , It's just that the parameters are a little different , This can extract the same code separately into a construction method , Then make
use this To call it .
4、super Keywords and constructor calls
super The role of keywords , If the subclass and parent have the same method , You can use super call
If the class is on an inheritance chain , He can call public methods in his own parent class and above the parent class . Its usage format is as follows :
Constructor name ([ parameter list ]){
[super([ parameter list ]);]
Statement sequence
}
adopt super( Up ) key word , Verify the positional relationship between father and son . Use super You should also pay attention to the following points :
1. this The statement must be the first statement in the constructor
2. super Variables of the parent class can be called , At this time, pay attention to the access permission modifier
3. Class's default nonparametric constructor , There is a sentence by default super()
4. this() and super() Can't exist at the same time

In the original Animal Class eat() Method , Override... In a subclass , In the subclass constructor , Call directly eat() Method and Application super Key word call eat() Method .
stay main() Use in method Cat Class to create objects , In the construction method without parameters , use suoer To call the constructor with parameters in the parent class , Then output a message . From the output, we can see that the parent object is superior to the child object , The construction method of the parent class is better than that of the child class. It executes first .
边栏推荐
猜你喜欢

Won't you just stick to 69 days? Merge range

11. Backup switch

Basic operation of queue

不会就坚持60天吧 神奇的字典

Not for 60 days, magical dictionary

Deep learning training strategy -- warming up the learning rate

14. Haproxy+kept load balancing and high availability

Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment

Jenkins 参数化构建中 各参数介绍与示例

Definition and implementation of stack and queue (detailed)
随机推荐
Visio draw grid
C language: enumerating knowledge points summary
What is the difference between field, variable and property
C language: summary of consortium knowledge points
Implementation of jump connection of RESNET (pytorch)
HC06 HC05 BT
全屋WiFi方案:Mesh路由器组网和AC+AP
Object detection: object_ Detection API +ssd target detection model
Coding questions encountered in the interview
Whole house WiFi solution: mesh router networking and ac+ap
C language: structure simple syntax summary
11. Backup switch
i++与++i详解
post导出数据,返回
15.federation
pyscript无法引入包
TypeError: Cannot read properties of undefined (reading ‘then‘)
Pytorch fixed random seed & recurrence model
Machine vision Series 1: Visual Studio 2019 dynamic link library DLL establishment
Taobao product details interface (product details page data interface)