方法重载
a.方法名称相同,参数列表不同(与返回值类型无关)
b.编译器根据方法签名决定调用哪个方法
举一例:
public class overloading {
public static void main(String[] agrs){
//调用方法
int max1=max(5,8);
System.out.println(max1);
double max2=max(66.9,70.2);
System.out.println(max2);
String max3=max("trump","obama");
System.out.println(max3);
}
static int max(int num1,int num2){
return num1>num2 ? num1:num2;
}
static double max(double num1,double num2){//方法重载
return num1>num2 ? num1:num2;
}
static String max(String str1,String str2){//方法重载
if(str1.compareTo(str2)>0){
return str1;
}else{
return str2;
}
}
}
在上面可以看到:三