java 继承与多态 习题_JAVA基础第4章节继承与多态练习题200910.doc

第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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值