Class design, the second of two important structures: method
Method: describe the function that the class should have.
- For example: Math class: sqrt()\random()
-
Scanner Class: nextXxx() ...
-
Arrays Class: sort() \ binarySearch() \ toString() \ equals() \ ...
- 1. Examples:
- public void eat(){}
- public void sleep(int hour){}
- public String getName(){}
- public String getNation(String nation){}
-
- Method declaration: permission modifier return value type method name (formal parameter list) {method body}
- Note: static, final and abstract are used to modify the method, which will be discussed later.
-
- explain:
-
3.1 About permission modifiers: the permission modifiers of the default method are used first public
-
Java There are four types of authority modifiers specified: private,public,Default protected -->More details on encapsulation
-
3.2 Return value type: return value vs No value returned
-
3.2.1 If the method returns a value, the type of the return value must be specified when the method is declared. At the same time, in the method, you need to use
-
return Keyword to return a variable or constant of the specified type:“ return Data ".
-
If the method does not return a value, when declaring the method, use void To show. Usually, in methods that do not return a value, you do not need to
-
use return.However, if used, only“ return;"Means to end this method.
-
3.2.2 Do we define whether the method should return a value?
-
① Title Requirements
-
② Based on experience: specific analysis of specific problems
-
3.3 Method name: it belongs to the identifier and follows the rules and specifications of the identifier, "see the meaning of the name"
-
3.4 Formal parameter list: method can declare 0, 1, or more formal parameters.
-
3.4.1 Format: data type 1 parameter 1,Data type 2 parameter 2,...
-
3.4.2 When we define methods, should we define formal parameters?
-
① Title Requirements
-
② Based on experience: specific analysis of specific problems
-
3.5 Method body: the embodiment of method function.
- Method, you can call the properties or methods of the current class
-
Special: method A Method called again in A:Recursive method.
-
Method cannot be defined.
return keyword:
1. Scope of application: used in the method body
2. Function: ① end method
-
② For methods that return value types, use"return data"Method returns the desired data.
3. Note: you cannot declare an execution statement after the return keyword.
1. Concept of method overloading
Definition: more than one method with the same name is allowed in the same class, as long as their parameter number or parameter type are different.
*
Summary: "two are the same and different": the same class and the same method name
Different parameter lists: different parameter numbers and different parameter types
2. Examples of heavy load:
Example 1: overloaded sort() / binarySearch() in Arrays class; println() in PrintStream
Example 2:
//The following four methods constitute overloads
public void getSum(int i,int j){
System.out.println("1");
}
public void getSum(double d1,double d2){ System.out.println("2"); } public void getSum(String s ,int i){ System.out.println("3"); } public void getSum(int i,String s){ System.out.println("4"); }
Examples that do not constitute overloading:
//The following three methods cannot be overloaded with the above four methods
// public int getSum(int i,int j){
// return 0;
// }
// public void getSum(int m,int n){
//
// }
// private void getSum(int i,int j){
//
// }
3. How to judge whether it constitutes a method overload?
Judge strictly according to the definition: two are the same and different.
It has nothing to do with the permission modifier, return value type, formal parameter variable name and method body of the method!
4. How to determine the call of a method in a class:
Method name - > parameter list
Interview question: what is the difference between method overloading and rewriting?
throws\throw
String\StringBuffer\StringBuilder
Collection\Collections
final\finally\finalize
...
Abstract classes and interfaces
sleep() / wait()
Method of variable number formal parameters
1. Instructions:
- 1. New content in JDK 5.0
- 2. Specific use:
- 2.1 format of variable number formal parameters: data type... Variable name
- 2.2 when calling the method with variable number of formal parameters, the number of parameters passed in can be: 0, 1, 2,...
- 2.3 the methods of variable number formal parameters have the same name as the methods in this class, and methods with different formal parameters constitute overloads
- 2.4 the methods of variable number formal parameters are the same as those in this class, and the arrays with the same type of formal parameters do not constitute overloading. In other words, the two cannot coexist.
- 2.5 variable number formal parameters must be declared at the end in the formal parameters of the method
- 2.6 variable number formal parameters in the formal parameters of a method, at most one deformable parameter can be declared.
2. Examples:
public void show(int i){
} public void show(String s){ System.out.println("show(String)"); } public void show(String ... strs){ System.out.println("show(String ... strs)"); for(int i = 0;i < strs.length;i++){ System.out.println(strs[i]); } } //Cannot exist with previous method
// public void show(String[] strs){
//
// }
When called:
test.show("hello");
test.show("hello","world");
test.show();
test.show(new String[]{"AA","BB","CC"});