The concept of method overloading and call 10

The concept of method overloading

Two with a different

The same class, the same method name, a different parameter list.

Others do not control (for example: modifiers, return type, etc.)

Parameter refers to a list of different : different types | different order | different number!

Variable parameter list is the same as the parameter names do not care.

public class MethodExecDemo02 {
    // 新方法
    public static void open(){

    }
    // 重载方法
    public static void open(int a){

    }
    // 重载方法
    static void open(int a ,int b){

    }
    // 重载方法
    public static void open(double a,int b){

    }
    // 重载方法
    public static void open(int a,double b){

    }

    // 重复方法 :形参列表是否相同不在乎形参的变量名称
//    public void open(int i,double d){
//
//    }

    // 新方法
    public static void OPEN(){

    }
}

Call overloaded method

It is noteworthy that in the use of numerical lang can not cast more behind the proposed increase in L. See in particular code.

public class MethodExecDemo03 {
    public static void main(String[] args) {
        System.out.println(compare(10,20)); // 整数常量定义出来默认是int类型的:所以这里调用int方法!
        System.out.println(compare((byte)10,(byte)10)); // 调用了的byte那个方法
        System.out.println(compare((short)10,(short)10)); // 调用的short那个方法
        System.out.println(compare(10L,10L)); // L代表了该值是long型值!  调用的long那个方法
    }

    public static boolean compare(byte a , byte b){
        System.out.println("--------byte--------");
        return a == b ;
    }

    public static boolean compare(short a , short b){
        System.out.println("--------short--------");
        return a == b ;
    }

    public static boolean compare(int a , int b){
        System.out.println("--------int--------");
        return a == b ;
    }

    public static boolean compare(long a , long b){
        System.out.println("--------long--------");
        return a == b ;
    }
}
Published 34 original articles · won praise 16 · views 295

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105165309