1.概述
- 完成特定功能的代码块
- 提高代码的复用性
1.1.意义
//键盘录入两个数字,判断其最大值
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个整数");
int a = sc.nextInt();
System.out.println("输入第二个整数");
int b = sc.nextInt();
int c = getMax(a,b); // 2.调用方法,得出结论
System.out.println(c);
}
public static int getMax(int a,int b){ // 1.声明判断方法
return a > b ? a : b;
}
2.方法重载
- 在一个类中定义多个同名的方法
- 分类
参数个数不同
参数类型 / 顺序不同