观察以下代码,你发现了有什么特殊之处吗?
|
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package dongshou1; public class MehodOverload {public static void main(String args[]) { System.out.println("The square of integer 7 is " + square(7)); System.out.println("The square of integer 7.5 is " + square(7.5)); } public static int square(int x) { return x*x; } public static double square(double y) { return y*y; }} |

上面square为方法的重载,方法名相同,传入的参数类型不同,导致使用时会访问方法名相同,但方法不一样的方法。
满足以下条件构成方法的重载:
1.方法名相同
2.参数类型不同,参数个数不同,或者是参数类型的顺序不同。
|
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package dongshou1;//RandomInt.java//Shifted, scaled random integersimport javax.swing.JOptionPane;import java.util.*;public class RandomInt {public static void main( String args[] ){Random rand=new Random();int j=2;int m=(int) (Math.pow(2, 18)-1);int a=11;int c=0;System.out.println("请输入产生的随机数的数量:");Scanner input=new Scanner (System.in);int n=input.nextInt();//开辟输入大小的数组;int b[];b=new int[n];System.out.println("请输入随机数的种子:");//输入随机数的种子;b[0]=rand.nextInt(10);System.out.println("第1个随机数: "+b[0]);for(int i=0;i<n-1;i++){b[i+1]=(b[i]*a+c)%m;System.out.print("第"+j+"个随机数:");System.out.println(b[i+1]);j++;}}} |
程序运行结果



浙公网安备 33010602011771号