已知有如下定义://Runnable.javapublic interface Runnable{public default void run(){System.out.println("Runnable");}}//Movable.javapublic interface Movable{public default void run(){System.out.println("Movable");}}//Animal.javapublic class Animal implements Runnable,Movable{public static void main(String... args){Animal animal=new Animal();animal.run();}}关于上述代码说法正确的是?
Animal类编译失败,因为无法实现关联的run方法;
B. Animal类编译正常,运行错误,因为无法判断运行哪个run方法;
C. Animal类编译失败,因为不能同时实现多个接口
D. Animal编译正常,运行正常,输出Runnable
E. Animal编译正常,运行正常,输出:Movable
查看答案
已知有如下定义://Runnable.javainterface Runnable{public default void run(){System.out.println("Running");}}//Animal.javapublic class Animal implements Runnable{public void run(String name){System.out.println(name+" running");}public static void main(String... args){Animal animal = new Animal();animal.run("tiger");}}关于上述代码说法正确的是?
A. 代码编译正常,运行正常
B. Animal类中有两个run方法,这是方法重载现象
C. Animal类中有两个run方法,这是方法覆盖
D. Aniaml类编译失败,因为Animal不能定义同名方法,带参的run方法会覆盖实现Runnable接口的默认方法run;
E. 以上说法均不正确
已知有如下定义://Runnable.javainterface Runnable{public default void run(){System.out.println("Running");}}//Animal.javapublic class Animal implements Runnable{public void run(){System.out.println("Animal is running");}public static void main(String... args){Animal animal=new Animal();animal.run();}}
A. 编译成功,运行成功,运行结果是:Running
B. 编译成功,运行成功,运行结果是:Animal is running
C. 编译成功,运行成功,运行结果是:RunningAnimal is running
D. Animal编译失败,因为实现的Runnable接口中已经定义了run方法,导致方法定义重复
E. Animal编译成功,运行失败,因为实现的Runnable接口中已经定义了run方法,因为无法判断运行哪个run方法
已知有如下定义://Runnable.javapublic interface Runnable{public static void run(){System.out.println("Running");}}//Animal.javapublic class Animal implements Runnable{public void run(){System.out.println("Animal is running");}public static void main(String... args){Aniaml.run();}}关于上述代码说法正确的是?
Animal类编译正常,运行正常,运行结果是:Running
B. Animal类编译正常,运行正常,运行结果是:Animal is running
C. Animal类编译失败,因为存在两个run同名方法
D. Animal类编译失败,因为run方法为非静态方法,需要通过Animal对象实例来调用;
E. 如果在Animal类中调用接口的run方法,需要通过接口名来调用,形如Runnable.run();
已知有如下定义:public class Animal{public String toString(){System.out.println("Animal0");return "Animal1";}public static void main(String... args){Animal animal = new Animal();System.out.println(animal);}}关于上述代码说法正确的是?
A. 编译成功,运行成功,运行结果是:Animal0Animal1
B. 编译成功,运行成功,运行结果是:Animal1Animal0
C. Animal定义的toString方法覆盖了继承来自java.lang.Object的toString方法
D. Animal定义的toString方法重载了继承来自java.lang.Object的toString方法