Member of Java class-method

Preface

There are five members in a class in Java, which are attributes, constructors, methods , code blocks, and inner classes. In this article, I will explain the concept and characteristics of the method from the shallower to the deeper, and I hope it will help you.

1 Give some examples

First, let me give an example of the four most common methods. They are:

public class Person{
    
    
    
    //无返回值,无形参
    public void eat(){
    
    
    }
    
    //无返回值,有形参
    public void sleep(String name){
    
    
    }
    
    //有返回值,无形参
    public int wait(){
    
    
    }
    
    //有返回值,有形参
    public String talk(int age){
    
    
    }
    
}

2 The format of the method declaration

From the four common methods mentioned above, we can derive the declaration format of the method. In the following pages, I will introduce each part of the declaration format to you.

权限修饰符 返回值类型 方法名(形参列表){
    
    
	方法体
}

3 Explanation of method declaration

3.1 Permission modifier

The four permission modifiers specified by Java can be added in front of the method. These four permission modifiers are
private public and the default protected

3.2 Return value type

Return value:

​ If the method has a return value, you must specify the return value type when the method is declared,

​ At the same time, the return keyword must be used in the method to return the specified type of data

public String eat(){
    
    
    return name;
}

None Return Value:

​ If the method does not return a value, declare void to indicate that, usually there is no need to use return in the method with no return value. If used;

If you want to use, it can only return;represent the end of the process, after not write the statement

If, after writing, it will compile error error message is: Unreachable statement

3.3 Method name

Belongs to the identifier, follow the identifier specification and see the name

3.4 Form attendance table

The method can declare any number of formal parameters, such as one, two or more

The following is the format for declaring multiple formal parameters

public void test(int age,String name){
    
    	//多个形参之间用,隔开
    //方法体
}

3.5 Method body

It is the embodiment of the method function, as long as we write the method, it is the method body

4 Use of the return keyword

​ Function: ①End method;

② directed to a method of the type of the return value, using retrun 数据;a manner to return the data needed

​ ③The statement after the return keyword cannot be executed

5 Use of methods

​ You can call the properties and methods of the current class ; calling method A in method A is a recursive method

​ No other methods can be defined in the method

Go deeper into the method

After understanding the declaration format of the method, we learn about the characteristics of the method

1 Method overload

1.1 The concept of overloading

​ Overloading is a group of methods with the same method name and different parameter lists in the same class and multiple methods, which we call method overloading.
Through the definition, we can also
figure out how to judge whether there is an overload relationship between multiple methods ① in the same class; ② the method name is the same; ③ the parameter list is different

​ Different formal parameter lists can be divided into different numbers of parameters, different types of parameters, and different order of parameters.
The following four methods constitute overloads with each other:

public void test(int a){
    
    
    
}

public void test(String b){
    
    
    
}

public void test(int a,String b){
    
    
    
}

public void test(String b,int a){
    
    
    
}

2 Variable number of formal parameters

That is, when you are not sure how many formal parameters you want to pass into the method, you can use deformable parameters. This feature is new in JDK5.0

Format: data type... variable name

public void test(String... strs){
    
    	这就是可变形参,传入的参数个数可以是 01个 。。。 多个
    
}
需要注意的是:可变个数形参必须声明在参数列表最后

可变形参最多只有一个

3 The value transfer mechanism of method parameters (emphasis)

3.1 The concept of formal parameters

Formal parameters : parameters in parentheses when the method is declared

Actual parameter : the data actually passed to the formal parameter when the method is called

3.2 Value passing mechanism

There is only one method of parameter passing in Java: value passing

The formal parameter is a basic data type: the " data value " of the basic data type of the actual parameter is passed to the formal parameter

Form parameter reference data type: Pass the " address value " of the actual parameter reference data type to the formal parameter

Guess you like

Origin blog.csdn.net/weixin_45321793/article/details/109297167