Methods in Java


Methods are named code blocks. Methods can have parameters or no parameters, which can improve the reusability of code
Define method format: modifier return value method name ([parameter list]) {method body;}
Return value:
1. Use the return keyword to return the result to the calling position.
2. If there is no return value, write void. If there is a return value, write void according to the type of return value
Parameter list: can have more than one, separated by, sign.

Overload of methodIt means that multiple methods with the same name are defined in a class, but each method is required to have a different parameter list (that is, the number and type of parameters are different). When a program calls a method, it can decide which method to use by passing different numbers and types of parameters to them.

Method rewritingObject oriented feature inheritance: after a subclass inherits the parent class, it can modify the method in the parent class (the source code of the parent class cannot be modified, OCP principle). This is called method rewriting.
1. When a method is rewritten, the return value, method name and parameter list of the original method cannot be modified. Only the method body can be modified.
2. The modifier of the method must be greater than or equal to the original method.
3. Private methods modified by private in the parent class cannot be overridden.
4. Improve code reusability and efficiency.
5. After rewriting, the method after rewriting is executed.
6. For an overridden method, the annotation @ override should be added to the method to indicate that the method is an overridden method. This makes it easier to read.

Construction method: is the method of the method name and class name.
Syntax: modifier class name (parameter list) {method body}
1. Assign values to member variables.
2. To create an object, complete the initialization of the object.

The constructor is called and triggered when the object is created.
By default, there is a parameterless construction method in the bottom layer, and there will be one even if it is not created. However, only parameterless construction methods can’t be provided. If only parameterless construction methods are available, then parameterless construction methods will be gone.
In the inheritance relationship, a super () is hidden by default in the construction method of the subclass, so the construction method of the parent class is executed first, and then the construction method of the subclass is executed.

Abstract methodA method without a method body.
Syntax: modifier Abstract returns the value method name (parameter list);

Code block: wrapped code. The function and usage of names are different with different positions.

Construct code block:
1. The position is outside the method in the class, which is triggered when the construction method is called.
2. It is usually used to extract common code of construction method
3. Loading prior to construction method

Local code block:
1. Position in method
2. Scope of action for control variables
3. Trigger when calling method

Static code block
1. Position in member position
2. It is loaded along with the loading of the class and is only loaded once. It is generally used for the initialization of the project
3. Syntax: static { }

Execution order: static code block > construction code block > construction method > local code block

Access control symbol
It is used to control the access scope of a class or its members.
Methods in Java