Java重载匹配优先级

前情提要

Java面向对象的三个基本特征:继承、封装和多态;多态主要体现在重载和重写;

示例代码

无意间看到这样一个问题,为了方便直观,就用代码来描述问题,有这样一个类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class OverloadPriority {
public static void print(Object arg) {
System.out.println("parameter type = Object");
}
public static void print(int arg) {
System.out.println("parameter type = int");
}
public static void print(long arg) {
System.out.println("parameter type = long");
}
public static void print(double arg) {
System.out.println("parameter type = double");
}
public static void print(float arg) {
System.out.println("parameter type = float");
}
public static void print(char arg) {
System.out.println("parameter type = char");
}
public static void print(Character arg) {
System.out.println("parameter type = Character");
}
public static void print(char... arg) {
System.out.println("parameter type = char...");
}
public static void print(Serializable arg) {
System.out.println("parameter type = Serializable");
}
public static void print(Comparable<?> arg) {
System.out.println("parameter type = Comparable");
}
public static void main(String[] args) {
// int
print('g');
}
}



本站访问量   |   您是第 位.