第4章 继承与多态
选择题
1. 编译和运行以下两文件结果//文件P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//文件 P2.javapackage YourPackage;
import MyPackage.*;
public class P2 extends P1{
public static void main(String argv[]){
P2 p2 = new P2();
p2.afancymethod();
}
}
A.两个均通过编译,P2运行时输出 What a fancy method
B.没一个通过编译
C.两个均通过编译,但P2运行时出错
D.P1 通过编译,但P2出现编译错误package a;
package b;
public class D{
public static void main(String args[]) {
System.out.println("^_^,今天心情不错!");
}
}
A.出现编译错误B.^_^
C.通过编译,运行时出错D.A.B.C.D. private class Base{
Base(){
int i = 100;
System.out.println(i);
}
}
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
Pri p = new Pri();
System.out.println(i);
}
}
A.编译错误 B.200 C.100 200 D.100
5.下列程序运行结果是( C )。
class Base{
Base(){
int i = 100;
System.out.println(i);
}
}
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
Pri p = new Pri();
System.out.println(i);
}
}
A.编译错误 B.200 C.100 200 D.100
6.如何定义一个不能有子类的类KeyA.class Key { } B.final class Key { }
C.public class Key { } D.class Key {final int i;}
7.哪个选项可以做为以下方法的覆盖方法public void add(int a) {…}
A.public void add(int b) {…} B.void add(int a) {…}
C.public int add(int a) {…} D.public void add(float a) {…}
8.在构造方法的哪个地方可以调用超类的构造方法
A.任何地方B.构造方法的第一条语句 C.构造方法的最后一条语句 D.不能在构造方法中调用超类的构造方法 public class Test {
public static void test() {
this.print();
}
public static void print() {
System.out.println("Test");
}
public static void main(String args []) {
test();
}
}
A.输出Test无输出结果
类编译错误,指示设有如下代码:
1 class Example{
2. String str;
3. Example(){
4. str= "example";
5. }
6. Example(String s){
7. str=s;
8. }
9. }
10. class Demo extends Example{
11.