My sixth day of learning Java in Shanghai LeByte

What is a method? A
method is a piece of code that can be called repeatedly.
In Java, the specific syntax format for declaring a method is as follows:

Modifier return value type method name ({parameter type parameter name 1, parameter type parameter name 2,...}) { execution statement ... return return value; } 1 2 3 4 5 The specific description of the above syntax format is as follows:









Modifiers: There are many modifiers for methods, such as those that restrict access rights, such as static modifier static and final modifier final.
Return value type: the data type used to limit the return value of the method.
Parameter type: used to limit The data type of the
parameter passed in when the method is called Parameter name: is a variable used to receive the data passed in when the method is called.
return keyword: used to end the method and return the value of the specified type of the method
1
2
3
4
5
Note: special Note that the "parameter type parameter name 1, parameter type parameter name 2" in the method is called the parameter list, which is used to describe the parameters that the method needs to receive when it is called. If the method does not need to receive any parameters, the parameter list is empty, that is, nothing is written in (). The return value of the method must be the return value type declared by the method. If there is no return value in the method, the return value type must be declared as void. In this case, the return statement in the method can be omitted

The following two methods are used to print the rectangle

Use ordinary methods to print rectangles with different length and width
public class Example01 { public static void main(String[] args) { //The following loop is to print a rectangle with width 5 and height 3 for (int i = 0; i <3; i++) { for (int j = 0; j <5; j++) { System.out.print(" "); } System.out.println(); } //The following loop is to print a wide Rectangle with 4 and height 2 for (int i = 0; i <4; i++) { for (int j = 0; j <2; j++) { System.out.print(" "); } System.out .println(); } //The following loop is to print a rectangle with width 10 and height 6 for (int i = 0; i <10; i++) { for (int j = 0; j <6; j++ ) { System.out.print(" "); }




















System.out.println();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Use parameter method to print rectangles with different length and width
public class Example02 { public static void main(String[] args) { printRectangle(3, 5); printRectangle(2, 4); printRectangle(6, 10);



}
public static void printRectangle(int height,int width) {
	for (int i = 0; i < height; i++) {
		for (int j = 0; j < width; j++) {
			System.out.print("*");
		}
		System.out.print("\n");
	}
	System.out.print("\n");
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Summary: It is much easier to use parameter methods when doing a lot of repetitive work

Method overloading
Java allows multiple methods with the same name to be defined in the same program, but the type or number of parameters must be different. This is method overloading

It is worth noting that the overloading of the method has nothing to do with the return value type, it needs to meet two conditions, one is the same method name, and the other is the number of parameters or parameter types are different.
Example:

public class Example01 {
	public static void main(String[] args) {
		//下面是针对求和方法的调用
		System.out.println("sum1="+add(10, 20));
		System.out.println("sum2="+add(10, 20, 30));
		System.out.println("sum3="+add(10.25, 10.30));
	}
	//下面的方法实现了两个整数相加
	public static int add(int x,int y) {
		return x+y;
	}
	//下面的方法实现了三个整数相加
	public static int add(int x,int y,int z) {
		return x+y+z;
	}
	//下面的方法实现了两个小数相加
	public static double add(double x,double y) {
		return x+y;
	}
}
```

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/109282577