02-01_习题.pdf
文本预览下载声明
第四章 Java 面向对象进阶(1)
习题一 类的继承
目标:
给定示例代码,确定某个方法是否正确地重载了另一个方法,确定方法
正确的返回值(包括协变返回)。给定一个情景,编写用于声明和/或调用重
写方法的代码,并编写用于声明和/或调用超类或重载构造函数的代码。
考查内容:
方法的重写与重载的比较。
1.1. Consider the following code:
1. class A {
2. A(String message) {
3. System.out.println(message + from A.);
4. }
5. }
6. class B extends A{
7. B() {
8. System.out.println(Hello from B.);
9. }
10. }
11. class RunSubClass {
12. public static void main(String[] args) {
13. B b = new B();
14. }
15. }
What is the output of this code?
A. Hello from B.
B. A compiler error occurs at line 2.
C. A compiler error is triggered by the call made at line 7.
D. It compiles fine but throws a runtime exception.
1.2. 给定下面的类定义
public class Upton {
public static void main(String argv[]) {
}
public void amethod(int i) {
}
// Here
}
下面哪一个在替换//Here 后是合法的?
A public int amethod(int z){}
B public int amethod(int i,int j){return 99;}
C protected void amethod(long l){ }
D private void anothermethod(){}
1.3. 给定下面的类定义
class Base {
public void amethod() {
System.out .println(Base);
}
}
public class Hay extends Base {
public static void main(String argv[]) {
Hay h = new Hay();
h.amethod();
}
}
下面在类 Hay 中的哪一个方法将会编译并使程序打印出字符串Hay?
A public int amethod(){ System.out.println(Hay);}
B public void amethod(long l){ System.out.println(Hay);}
C public void amethod(){ System.out.println(Hay);}
D public void amethod(void){ System.out.println(Hay);}
1.4. 给定下面的类定义
public class ShrubHill {
public void foregate(String sName) {
}
// Here
}
下面的哪一个方法可以合法的直接替换//Here ?
A public int foregate
显示全部