9.3
重载就是在一个类中,有相同的函数名称,但形参不同的函数
方法的重载的规则
实现理论
在方法声明中,在指定参数类型后加一个省略号(…)。
一个方法中只能指定一个可变草书,它必须是方法的最后一个参数,任何普通的参数必须在他之前声明
package method; import com.sun.org.apache.xpath.internal.objects.XNumber; public class Demo02 { public static void printMax(double...numbers) { if(numbers.length==0){ System.out.println("没有输入数!"); return; } double result= numbers[0]; //排序! for (int i = 1; i <numbers.length ; i++) { if(numbers[i]>result){ result=numbers[i]; } } System.out.println("The max value is "+result); } public static void main(String[] args) { Demo02 demo02 = new Demo02(); demo02.printMax(1,2,3,4,5,6); } }
package method; import java.util.Scanner; public class caculate { public static void main(String[] args) { int a=0; int b=0; Scanner scanner = new Scanner(System.in); System.out.println("请输入运算符:"); String operator=scanner.nextLine(); System.out.println("请输入第一个运算数:"); a=scanner.nextInt(); System.out.println("请输入第二个运算数:"); b= scanner.nextInt(); scanner.close(); System.out.println("=================="); switch(operator){ case"+": System.out.println(a+operator+b+"="+add(a,b)); break; case"-": System.out.println(a+operator+b+"="+sub(a,b)); break; case"*": System.out.println(a+operator+b+"="+mul(a,b)); break; case"/": System.out.println(a+operator+b+"="+div(a,b)); break; } } public static double add(double a,double b){ return a+b; } public static double sub(double a,double b){ return a-b; } public static double mul(double a,double b){ return a*b; } public static double div(double a,double b){ return a/b; } }