当前位置:SCJP程序员认证考试题库

问题:

[单选]

class Parent {    
String one, two; 
public Parent(String a, String b){    
one = a;    
two = b;    } 
public void print(){ System.out.println(one); }    } 
public class Child extends Parent {    
public Child(String a, String b){    
super(a,b);    
} 
public void print(){ 
System.out.println(one + " to " + two);    
} 
public static void main(String arg[]){   
 Parent p = new Parent("south", "north");    
Parent t = new Child("east", "west");    
p.print();    
t.print();    
}    
} 
Which of the following is correct?()

A .  Cause error during compilation. 
B .  south     east 
C .  south to north     east to west    
D .  south to north     east    
E .  south     east to west

计量管理程序所称的能源计量器具是指指为准确计量各种能源、水资源和载能工质消耗量所配备的()与装置。 工具。 器具。 计量仪表。 用具。 讼前的证据保全包括()。 行政机关采取的证据保全。 公证机关采取的诉讼保全。 人民法院应申请采取的证据保全。 公安机关采取的诉讼保全。 在我国,业主方主要通过()的方式选择设计方案和设计单位。 委托设计。 政府指定。 设计招标。 方案竞赛。 新《准则》《条例》自()起施行。 2015年12月1日。 2016年1月1日。 2016年7月1日。 2015年11月1日。 什么是职场基本知识?() 打印文件。 编写程序。 填写报单。 处理文档和数据。

class Parent {    
String one, two; 
public Parent(String a, String b){    
one = a;    
two = b;    } 
public void print(){ System.out.println(one); }    } 
public class Child extends Parent {    
public Child(String a, String b){    
super(a,b);    
} 
public void print(){ 
System.out.println(one + " to " + two);    
} 
public static void main(String arg[]){   
 Parent p = new Parent("south", "north");    
Parent t = new Child("east", "west");    
p.print();    
t.print();    
}    
} 
Which of the following is correct?()

参考答案:

  参考解析

这个题目涉及继承时的多态性问题,在前面的问题中已经有讲述,要注意的是语句t.print();在运行时t实际指向的是一个Child对象,即java在运行时决定变量的实际类型,而在编译时t是一个Parent对象,因此,如果子类Child中有父类中没有的方法,例如printAll(),那么不能使用t.printAll()。

在线 客服