table of Contents
Define the format of the method
The first category: methods with no parameters and no return value
The second category: methods with parameters and return values
The third category: How to determine whether the method needs parameters?
The fourth category: How to determine whether a method needs a return value?
Preface
Various languages have their own characteristics, and at the same time have a lot in common with other languages. For the study of any language, the foundation is very important. The following editor will take you to learn about methods and method overloading in Java.
Method definition
A method is actually a collection of functions of several sentences. Give a very vivid example:
Ice cream factory: raw materials (water, white sugar, cream)
Output product: ice cream
Parameters (raw materials): the data that enters the method
Return value (output): the data from the method
The method is to use the raw materials that enter the factory as the parameters to obtain the product that the consumer needs, that is, the return value. Among them, the method has a variety of parameter types, which can be more or less, and the method return value is optional.
Define the format of the method
修饰符 返回值类型 方法名称(参数类型1 参数名称1,参数类型2 参数名称2,...){
方法体;
return 返回值;
}
Explain in detail:
Modifiers: public static, private...
Return value type: What is the data type of the data finally generated by the method
Method name: The name of the method, like the variable, is named with a small hump (xxxYyyZzz)
Parameter type: what type of data entered the method
Parameter name: the name of the variable corresponding to the data entered into the method
ps: If there are multiple parameters, use commas to separate
Method body: what the method needs to do, several lines of code
return: Two functions, the first stops the current method, and the second returns the return value to the caller
Return value: the final result after the method is executed
Note: The return value after return must be the same as the return value before the method name.
Classification of methods
According to the parameters: divided into parameter methods and non-parameter methods
According to the return value: divided into methods with return value and methods without return value
Code example
The first category: methods with no parameters and no return value
public class Demo02Method {
public static void main(String[] args) {
//方法的调用
printMethod();
}
//定义方法:该方法用来循环打印输出五行*,每行20个
public static void printMethod() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 20; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
The second category: methods with parameters and return values
Define a method for adding two int numbers. The method has three elements:
return value type: int
method name: sum
parameter list: int num1, int num2
public class Demo02MethodDefine {
public static void main(String[] args) {
int a=1;
int b=6;
//调用求和方法
int sum=sum(a,b);
System.out.println("ab两数的和为:"+sum);
}
//定义方法:该方法用来求两个数之和
public static int sum(int num1,int num2) {
int sum=num1+num2;
return sum;
}
}
The third category: How to determine whether the method needs parameters?
Method with parameters: Multiply two numbers. You must know what the two numbers are before you can calculate. You must have parameters.
No-parameter method: You can complete the task without any data. For example, print out a fixed text string 10 times.
public class Demo02MethodParam {
public static void main(String[] args) {
multiply(10,20);
print();
}
//有参数的方法::两个数字相乘,必须知道两个数字是多少,才能进行计算,必须要有参数。
public static void multiply(int a,int b) {
int result=a*b;
System.out.println("结果是:"+result);
}
//无参数的方法:不需要任何数据,就可以完成任务。
//例如打印输出固定的10次文本字符串
public static void print() {
for (int i = 0; i < 10; i++) {
System.out.println("Hello World"+i);
}
}
}
The fourth category: How to determine whether a method needs a return value?
public class Demo02MethodReturn {
public static void main(String[] args) {
//调用getSum()方法,接受返回值
int sum=getSum(10,20);
System.out.println("返回值为:"+sum);
//调用无返回值的printResult方法
printResult(100,200);
}
//有返回值:计算两数之和
public static int getSum(int a,int b){
int result=a+b;
return result;
}
//无返回值,直接打印输出结果
public static void printResult(int a,int b){
int result=a+b;
System.out.println("结果是:"+result);
}
}
Method considerations
Method overload
Definition: The names of multiple methods are the same, but the parameter lists are different.
Method overloading is related to the following factors:
1. The number of parameters is different
2. Different parameter types
3. The order of multiple types of parameters is different
Method overloading has nothing to do with the following factors:
1. Parameter name
2. It has nothing to do with the return value type of the method
Example of overloaded code
If we want to define a sum method to find the sum of numbers, but we want to realize the sum method can find the sum of 2 int numbers or 3 ints
For the sum of numbers, you can also find the sum of int+double. In short, no matter how many data, whether it is int or double, use a sum method to achieve.
public class Demo01MethodOverload {
public static void main(String[] args) {
System.out.println(sum(10,20)); //30
System.out.println(sum(10,20,30)); //60
System.out.println(sum(10,20,30,40)); //100
System.out.println(sum(10,50.2)); //60
System.out.println(sum(50.3,80)); //130
}
public static int sum(int num1,int num2){
return num1+num2;
}
public static int sum(int num1,int num2,int num3){
return num1+num2+num3;
}
public static int sum(int num1,int num2,int num3,int num4){
return num1+num2+num3+num4;
}
public static int sum(int num1,double num2){
return (int)(num1+num2);
}
public static int sum(double num1,int num2){
return (int)(num1+num2);
}
}
The method and the overloading of the method are introduced here first, I hope it can be helpful to you!