难度级别: 简单
程序
程序一
1) 以下程序的输出是什么?
public class Test { public int getData() //getdata 1 { return 0; } public long getData() //getdata 2 { return 1; } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.getData()); } }
a) 1
b) 0
c) 运行时错误
d) 编译错误
点此跳转到答案
程序二
2) 以下程序的输出是什么?
public class Test { public int getData(String temp) throws IOException { return 0; } public int getData(String temp) throws Exception { return 1; } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.getData("HY")); } }
a) 0
b) 1
c) 编译错误
d) 运行时错误
点此跳转到答案
程序三
3) 以下程序的输出是什么?
public class Test { private String function() { return ("HY"); } public final static String function(int data) { return ("haiyong"); } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.function()); } }
a) 编译错误
b) 运行时错误
c) HY
d) 都不是
点此跳转到答案
程序四
4) 以下程序的输出是什么?
public class Test { private String function(String temp, int data) { return ("HY"); } private String function(int data, String temp) { return ("haiyong"); } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.function(4, "HY")); } }
a) HY
b) haiyong
c) 编译错误
d) 运行时错误
程序五
5) 以下程序的输出是什么?
public class Test { private String function(String temp, int data, int sum) { return ("HY"); } private String function(String temp, int data) { return ("haiyong"); } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.function("HY", 0, 20)); } }
a) HY
b) 编译错误
c) 运行时错误
d) haiyong
点此跳转到答案
6) 以下程序的输出是什么?
public class Test { private String function(float i, int f) { return ("hy"); } private String function(double i, double f) { return ("HY"); } public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.function(1.0, 20)); } }
a) HY
b) 编译错误
c) 运行时错误
d) hy
点此跳转到答案
文章后半部分是程序的输出及解析
输出及解析
程序一输出
答案:
d
说明:
对于方法重载,方法必须有不同的签名。方法的返回类型对不同的方法签名没有贡献,因此上面的代码给出了编译错误。getdata 1 和 getdata 2 仅在返回类型和 NOT 签名上有所不同。
程序二输出
答案:
c
说明:
抛出不同异常的方法不会重载,因为它们的签名仍然相同。
程序三输出
答案:
c
说明:
与方法相关的访问修饰符不能确定重载的标准。重载方法也可以声明为 final 或 static 而不影响重载标准。
程序四答案
答案
b
说明:
参数的顺序是决定方法重载的重要参数。由于属性的顺序不同,方法被重载。
程序五答案
答案 :
a
说明:
参数的顺序是决定方法重载的重要参数。
程序六答案
答案 :
a
说明:
这个真的很简单。不同类型的参数会导致方法重载,因为方法的签名随不同类型的属性而改变。将调用与主函数中传递的参数集匹配的任何方法。在这里,传递的第一个参数是 double,因此打印 HY。
以上就是本篇文章的所有内容了