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
| public class MethodOverload { public static void main(String[] args) { System.out.println(sum(10, 20)); System.out.println(sum(10, 20, 30)); System.out.println(sum(10, 20, 30, 40)); System.out.println(sum(10, 20, 30, 40)); System.out.println(sum(10, 20.0)); System.out.println(sum(10.0, 20)); System.out.println(sum(10.0, 20.0)); }
public static int sum(int a, double b){ return (int) (a + b); }
public static int sum(double a, int b){ return (int) (a + b); }
public static int sum(double a, double b){ return (int) (a + b); }
public static int sum(int a, int b){ System.out.println("有俩个参数方法的执行!"); return a + b; }
public static int sum(int a, int b, int c){ System.out.println("有三个参数方法的执行!"); return a + b + c; }
public static int sum(int a, int b, int c, int d){ System.out.println("有四个参数方法的执行!"); return a + b + c + d; } }
|