avatar
overloading methods# Java - 爪哇娇娃
r*l
1
What is the output of the following code?
01: public class A {
02: public static void test(Object a) {
03: System.out.println("Object");
04: }
05: public static void test(String a) {
06: System.out.println("String");
07: }
08:
09: public static void main(String args[]) {
10: test(null);
11: }
12: }
The answers:
A.Object
B.String
C.Compilation error
D.Runtime error
Answer is B. Why?
avatar
g*g
2
This is a dump question. Forget about it.

【在 r*********l 的大作中提到】
: What is the output of the following code?
: 01: public class A {
: 02: public static void test(Object a) {
: 03: System.out.println("Object");
: 04: }
: 05: public static void test(String a) {
: 06: System.out.println("String");
: 07: }
: 08:
: 09: public static void main(String args[]) {

avatar
l*u
3
java porlimophism mah
runtime dymanically links to subclass
even it declares a variable type sueprclass.

【在 r*********l 的大作中提到】
: What is the output of the following code?
: 01: public class A {
: 02: public static void test(Object a) {
: 03: System.out.println("Object");
: 04: }
: 05: public static void test(String a) {
: 06: System.out.println("String");
: 07: }
: 08:
: 09: public static void main(String args[]) {

avatar
m*t
4

See Sections 8.4.9 and 15.12.2, JLS, 3rd Edition.

【在 r*********l 的大作中提到】
: What is the output of the following code?
: 01: public class A {
: 02: public static void test(Object a) {
: 03: System.out.println("Object");
: 04: }
: 05: public static void test(String a) {
: 06: System.out.println("String");
: 07: }
: 08:
: 09: public static void main(String args[]) {

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。