方法重载的定义
使用案例
public static void main(String[] args) {
// 匹配到max(int a, int b)
System.out.println(max(1, 3));
// 匹配到max(double a, double b)
System.out.println(max(1L, 3L));
// 匹配到max(double a, double b, double c, double d),int自动转换为double
System.out.println(max(1, 3, 2));
}
static int max(int a, int b) {
System.out.println("执行了:max(int a, int b)");
return a > b ? a : b;
}
static double max(double a, double b) {
System.out.println("执行了:max(double a, double b)");
return a > b ? a : b;
}
static double max(double a, double b, double c) {
System.out.println("执行了:max(double a, double b, double c)");
double result1 = a > b ? a : b;
return result1 > c ? result1 : c;
}
static double max(double a, double b, double c, double d) {
System.out.println("执行了:max(double a, double b, double c, double d)");
double result1 = a > b ? a : b;
double result2 = result1 > c ? result1 : c;
return result2 > d ? result2 : d;
}