一、基本介绍
Java中允许同一个类中,多个同名方法的存在,但要求形参列表不一致;
二、重载的好处
①减轻了起名的麻烦
②减轻了记名的麻烦
三、案例
public class MyCalculator {
public static void main(String[] args) {
MyCalculator.calculate(1, 3);
MyCalculator.calculate(5.3, 9.4);
MyCalculator.calculate(1, 6.5);
MyCalculator.calculate(1, 2, 3);
}
public static int calculate(int n1, int n2) {
return n1 + n2;
}
public static double calculate(double n1, double n2) {
return n1 + n2;
}
public static double calculate(int n1, double n2) {
return n1 + n2;
}
public static double calculate(int n1, int n2, int n3) {
return n1 + n2 + n3;
}
}