当前位置:网站首页>2021-04-16 method overload parameter transfer

2021-04-16 method overload parameter transfer

2022-06-23 10:07:00 Deer like deer

Method overloading

Overloading is in a class , Have the same function name , But functions with different parameters
Method overload rules :

  1. The method name must be the same
  2. Parameter list must be different ( The number is different. , Or of different types , Different parameter sorting lists, etc )
  3. Methods can have the same or different return types
  4. It's not enough to be an overload of a method just because the return type is different
public class Demo3 {
    
    public static void main(String[]args){
    
        int max = max(10,20,30);
        System.out.println(max);
    }
    // Than the size 
    public static int max(double num1,double num2){
    
        return 0;
    }
    // Than the size 
    public static int max(int num1,int num2){
    
        return  0;
    }
    // Than the size 
    public static int max(int num1,int num2,int num3){
    
        return 0;
    }
}

Command line arguments

Sometimes I want to give him a message when I run a program , This depends on passing command line arguments to main() Function implementation

Variable parameters

Java Support passing variable parameters of the same type to a method
In the method declaration , Add an ellipsis after the specified parameter type
Only one variable parameter can be specified in a method , It must be the last parameter of the method , Any ordinary parameter must be declared before it

public class Demo4 {
    
    public static void main(String[]args){
    
        Demo4 demo4 = new Demo4();
        demo4.test(1);
    }
    public void test (int...i){
    
        System.out.println(i[0]);
    }
}

原网站

版权声明
本文为[Deer like deer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230954196117.html