方法重载

满足的条件:

  在一个类里

  方法名相同

  参数不同、类型不同或数量不同

 

 

特点:

  只对方法定义,与调用无关

  只与方法名称和参数识别,与返回值无关

 

在调用时,java虚拟机根据参数不同来区分同名方法 

 

 

比较不同类型的数:

public class Test {
public static void main(String[] args) {
System.out.println(compare(10, 20));
System.out.println(compare((short) 10, (short) 20));
System.out.println(compare((byte) 10, (byte) 20));
System.out.println(compare(10L, 20L));
}


public static boolean compare(int a, int b) {
return a == b;
}

public static boolean compare(byte a, byte b) {
return a == b;
}

public static boolean compare(short a, short b) {
return a == b;
}

public static boolean compare(long a, long b) {
return a == b;
}

}
posted @ 2022-01-14 20:38  大灰狼21  阅读(51)  评论(0)    收藏  举报