Three calling formats and method overloading of Java methods

Three call formats of methods

After the method is defined, it will not be executed. If you want to execute it, you must call it: separate call, print call, assignment call.

  1. Separate call: method name (parameter);
  2. Print call: System.out.println (method name (parameter));
  3. Assignment call: data type variable name = method name (parameter);

Note: The method whose return value type is fixed as void has no return value, and can only be called individually, and cannot be used for printing calls or assignment calls.

public class MethodDefine {
    
    

    public static void main(String[] args) {
    
    
        // 单独调用
        sum(10, 20);
        System.out.println("===========");

        // 打印调用
        System.out.println(sum(10, 20)); // 30
        System.out.println("===========");

        // 赋值调用
        int number = sum(15, 25);
        number += 100;
        System.out.println("变量的值:" + number); // 140
    }

    public static int sum(int a, int b) {
    
    
        System.out.println("方法执行啦!");
        int result = a + b;
        return result;
    }

}

operation result:
Insert picture description here

Method overloading

Method overload (Overload): multiple methods have the same name, but the parameter list is different.
Benefit: You only need to remember the only method name to achieve multiple similar functions.

Method overloading is related to the following factors:

  1. The number of parameters is different
  2. Different parameter types
  3. The order of multiple types of parameters is different

Method overloading has nothing to do with the following factors:

  1. Has nothing to do with the name of the parameter
  2. Has nothing to do with the return value type of the method
public class Demo01MethodOverload {
    
    

    public static void main(String[] args) {
    
    
        /*System.out.println(sumTwo(10, 20)); // 30
        System.out.println(sumThree(10, 20, 30)); // 60
        System.out.println(sumFour(10, 20, 30, 40)); // 100*/

        System.out.println(sum(10, 20)); // 两个参数的方法默认int
        System.out.println(sum(10d, 20)); // 两个参数的方法10d与(double)10相同
        System.out.println(sum(10, 20, 30)); // 三个参数的方法
        System.out.println(sum(10, 20, 30, 40)); // 四个参数的方法
//        System.out.println(sum(10, 20, 30, 40, 50)); // 找不到任何方法来匹配,所以错误!

        sum(10, 20);
    }

    public static int sum(int a, double b) {
    
    

        System.out.println("有2个参数的方法执行1!");
        return (int) (a + b);
    }

    public static int sum(double a, int b) {
    
    
        System.out.println("有2个参数的方法执行2!");
        return (int) (a + b);
    }

    public static int sum(int a, int b) {
    
    
        System.out.println("有2个参数的方法执行3!");
        return a + b;
    }

    // 错误写法!与方法的返回值类型无关
//    public static double sum(int a, int b) {
    
    
//        return a + b + 0.0;
//    }

    // 错误写法!与参数的名称无关
//    public static int sum(int x, int y) {
    
    
//        return x + y;
//    }

    public static int sum(double a, double b) {
    
    
        return (int) (a + b);
    }

    public static int sum(int a, int b, int c) {
    
    
        System.out.println("有3个参数的方法执行!");
        return a + b + c;
    }

    public static int sum(int a, int b, int c, int d) {
    
    
        System.out.println("有4个参数的方法执行!");
        return a + b + c + d;
    }

}

operation result:

Insert picture description here
Give it a try-----determine whether the following method overloading is correct:

  1. public static void sum(){} // correct

  2. public static void sum(int a){} // correct

  3. static void sum(int a,int b){} // Code error: conflict with line 8

  4. public static void sum(double a,int b){} // 正确

  5. public static void sum(int a,double b){} // Code error: conflict with line 6

  6. public void sum(int i,double d){} // Code error: conflict with line 5

  7. public static void SUM(){} // The code is correct and will not report an error, but it is not a valid overload

  8. public static void sum(int i,int j){} // Code error: conflict with line 3
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51492999/article/details/113960298