catalogue
What is the method
Method definition and call
Method overloading
Command line parameters
Variable parameter
recursion
What is the method?
-
Method is an ordered combination of steps to solve a class of problems
-
Methods are contained in classes or objects
-
Methods are created in the program and referenced elsewhere
-
Use a piece of code to complete a specific function
example
package com.ckw.blog.mathod; public class demo01 { //The main method is the program's own execution method //Public is an identifier meaning: public static is also an identifier meaning: static void means space //Add static to become a class variable, which can be called directly public static void main(String[] args) { int i =add(8,9); System.out.println(i); dda(); } //Write an addition method //Identifier identifier return value type method name (variable) {} public static int add(int a ,int b){ return a+b; } //How to write a triangle public static void dda() { for (int j = 0; j <=5; j++) { for (int i = 5; i >=j; i--) { System.out.print(" "); } for (int i = 0; i <=j; i++) { System.out.print("*"); } for (int i = j; i >0; i--) { System.out.print("*"); } System.out.println(""); } }}
1. Add static to become a class variable
2. Do not add static as instance variable. new reference is required
Class variable = new class ();
Variable method
Principles of design method
The atomicity of a method is that a method only completes one function, which is convenient for later expansion
Keep the main method as simple and clean as possible
Naming rules for methods
Methods naming rules, initial lowercase, hump principle
Composition of method
Method contains a method header and a method body
Modifier return value type method name (parameter type variable) {method body}
Return return value
-
The modifier (optional) tells the compiler how to call the method and defines the access type of the method
-
return value type: a method may return a value. Return value is the data type of the return value of the method. Some methods perform the required operation but do not return a value. In this case, return type is null. It is the number type if there is a number return. No, it's void
-
Method name: the actual name of the method. The method name and parameter table together constitute the method signature
-
Parameter (optional): the parameter is like a placeholder. When the method is called, the value is passed to the parameter. This value is called an argument or variable. The parameter list values are the parameter type, order and number of parameters of the method. Parameters required or not are optional. Methods can contain no parameters.
Interpretation of formal parameters and actual parameters
package com.ckw.blog.mathod; public class demo01test { //Formal parameters: A and B are like placeholders. Used to define the role public static int add(int a ,int b){ return a+b; } public static void main(String[] args) { //Actual parameters: the parameters actually passed to him. System.out.println(add(2,3)); } }
- Method body: contains specific statements that define the function of the method.
Note:
If there is a return value, write the return value type.
If there is no return value, the return value type is void
If ruturn=0, the method terminates and no longer runs.
Specific size
package com.ckw.blog.mathod; public class demo02_big { public static int dda(int a ,int b){ int c = 0 ; if(a==b){ System.out.println("The results are the same and cannot be compared"); return 0;}//return=0 the method terminates and will no longer run if (a>b){c=a;} else if(a<b){c=b;} return c; } public static void main(String[] args) { int dd =dda(90 ,90); System.out.println(dd); } }
Method overloading
In the same class, functions with the same function name but different formal parameters.
- Same method name
- Different parameter lists (different number, different types, different parameter arrangement and different order)
- Method return type does not want to dry
- Different return types are not enough to overload methods
Realization theory
When the method name is the same, the compiler will match one by one according to the number and type of parameters calling the method to select the corresponding method. If the matching fails, the compiler will report an error.
(the compiler will choose the method suitable for its own size type) really intelligent.
(automatically match the formal parameter selection method according to the parameter type or quantity)
package com.ckw.blog.mathod; public class demo03_idea { public static int max(int a ,int b){ int c = 0 ; if(a==b){ System.out.println("The results are the same and cannot be compared"); return 0;} if (a>b){c=a;} else if(a<b){c=b;} return c; } public static double max(double a ,double b){ double c = 0 ; if(a==b){ System.out.println("The results are the same and cannot be compared"); return 0;} if (a>b){c=a;} else if(a<b){c=b;} return c; } public static double max(double a ,double b,double d){ double c = 0 ; if(a==b){ System.out.println("The results are the same and cannot be compared"); return 0;} if (a>b){c=a;} else if(a<b){c=b;} return c+d; } public static void main(String[] args) { double dd =max(92 ,90,91); System.out.println(dd); } }