image.png

    1. package com.itheima.overload;
    2. public class MethodDemo1 {
    3. public static void main(String[] args) {
    4. // 目标:识别方法重载的形式,并理解其调用流程,最后需要知道使用方法重载的好处
    5. fire();
    6. fire("美国");
    7. fire("日本" , 1000);
    8. }
    9. // 方法重载:方法名一样,参数不同(有多个参数,或一个,或没有)
    10. public static void fire(){ // 同级的方法可以相互调用
    11. // System.out.println("默认发射一枚武器给美国");
    12. fire("日本"); // 同级方法相互调用可以让代码更加简洁
    13. }
    14. public static void fire(String location){
    15. System.out.println("默认给" + location + "发射一枚武器");
    16. }
    17. public static void fire(String location,int number) {
    18. System.out.println("默认给" + location + "发射" + number + "枚武器");
    19. }
    20. }

    判断方法重载:
    image.png
    image.png