方法重载介绍:在同一个类里,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可;
特点:与返回值类型无关,只看方法名和参数列表;
在调用时,JVM虚拟机会通过参数列表的不同来区分同名方法并输出
示例:
public class Demo {
public static void main(String[] args) {
int a = 4;
int b = 5;
int c = 6;
System.out.println(sum(a,b,c));
System.out.println(sum(a,b));
}
public static int sum(int a,int b) {
int c = a + b;
return c;
}
public static int sum(int a,int b,int c){
int d = a + b + c;
return d;
}
输出结果如下:
15
9