题目
【填空题】给定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(); ) }
【填空题】给定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(); } }
题目解答
答案
3
解析
步骤 1:定义父类Parent
在代码中,定义了一个名为Parent的类,其中包含一个名为count的方法,该方法输出10除以3的余数,即10%3的结果。
步骤 2:定义子类Child
在代码中,定义了一个名为Child的类,该类继承自Parent类。Child类中也定义了一个名为count的方法,该方法输出10除以3的商,即10/3的结果。
步骤 3:创建Child对象并调用count方法
在main方法中,创建了一个Parent类型的引用变量p,并将其指向一个Child对象。然后调用p的count方法。由于p实际上指向的是一个Child对象,因此调用的是Child类中的count方法,输出10/3的结果。
在代码中,定义了一个名为Parent的类,其中包含一个名为count的方法,该方法输出10除以3的余数,即10%3的结果。
步骤 2:定义子类Child
在代码中,定义了一个名为Child的类,该类继承自Parent类。Child类中也定义了一个名为count的方法,该方法输出10除以3的商,即10/3的结果。
步骤 3:创建Child对象并调用count方法
在main方法中,创建了一个Parent类型的引用变量p,并将其指向一个Child对象。然后调用p的count方法。由于p实际上指向的是一个Child对象,因此调用的是Child类中的count方法,输出10/3的结果。