下列代码编译和运行的结果是()。 public class A { public void start() { System.out.println("TestA"); } } public class B extends A { public void start() { System.out.println("TestB"); } public static void main(String[] args) { ((A) new B()).start(); } }
A: 输出:TestA
B: 输出:TestB
C: 输出:TestA TestB
D: 编译错误
A: 输出:TestA
B: 输出:TestB
C: 输出:TestA TestB
D: 编译错误
举一反三
- 给定Java代码如下所示,则编译运行后,输出结果是。 class Parent { public void count() { System.out.println(10%3); } } public class Child extends Parent{ public void count() { System.out.println(10/3); } public static void main(String args[]) { Parent p = new Child(); p.count(); } }
- 中国大学MOOC: 下列代码中构造方法的返回类型是()public class Village { Village () { System .out .println(“hiding in Village”) ; } public static void main( String args [ ]) { Village c =new Village ( ) ;}class Village { public static void main( String args [ ]) { Village c =new Village ( ) ; } Village () { System .out .println(“hiding in Village”) ; } }
- interface A{ void x(); } class B implements A{ public void x(){} public void y(){} } class C extends B{ public void x(){System.out.println("C");} } public class Test{ public static void main(String args[]){ B b=new B(); B c=new C(); b.x(); c.y(); } } A: 程序运行错误 B: 程序没有输出结果 C: 程序输出C。 D: class B implements A 编译错误 E: c.y();编译错误
- 下列的程序的运行结果是( )public class Example { public static void main(String[] args) { new Father () { public void show() { System.out.println("helloworld"); } }.show(); }}class Father { public void show() { System.out.println("hellofather"); }} A: hellofather B: helloworld C: 编译报错 D: 编译通过,运行报错
- 阅读以下代码: public class foo{ public static void main (String[] args){ String s; System.out.println("s=" + s); 输出结果应该是