Java 2 实用教程(第5版)耿祥义版 习题六

一、问答题

1.接口中能声明变量吗?

2.接口中能定义非抽象方法吗?

3.什么叫接口的回调?

4.接口中的常量可以不指定初值吗?

5.可以在接口中只声明常量,不声明抽象方法吗?

二、选择题

1.下列哪个叙述是正确的

A.一个类最多可以实现两个接口。

B.如果一个抽象类实现某个接口,那么它必须要重写接口中的全部方法。

C.如果一个非抽象类实现某个接口,那么它可以只重写接口中的部分方法。

D.允许接口中只有一个抽象方法。

2.下列接口中标注的(A,B,C,D)中,哪两个是错误的?

interface Takecare {

    protected void speakHello();          //A

    public abstract static void cry();        //B

    int f();                            //C

    abstract float g();                   //D

}

3.将下列(A,B,C,D)哪个代码替换下列程序中的【代码】不会导致编译错误。

A.public int f(){return 100+M;}

B.int f(){return 100;}

C.public double f(){return 2.6;}。

D.public abstract int f();

interface Com {

    int M = 200;

    int f();

}

class ImpCom implements Com {

   【代码】

}

三、阅读程序

1.请说出E类中【代码1】,【代码2】的输出结果。

interface A {

   double f(double x,double y);

}

class B implements A {

  public double f(double x,double y) {

     return x*y;

  }

  int g(int a,int b) {

     return a+b;

  }

}

public class E {

public static void main(String args[]) {

      A a = new B();

      System.out.println(a.f(3,5)); //【代码1】

      B b = (B)a;

      System.out.println(b.g(3,5)); //【代码2】

  }

}

2.请说出E类中【代码1】,【代码2】的输出结果。

interface Com {

   int add(int a,int b);

}

abstract class A {

   abstract int add(int a,int b);

}

class B extends A implements Com{

   public int add(int a,int b) {

      return a+b;

   }

}

public class E {

public static void main(String args[]) {

     B b = new B();

     Com com = b;

     System.out.println(com.add(12,6)); //【代码1】

     A a = b;

     System.out.println(a.add(10,5));   //【代码2】

  }

}

四、编程题(参考例子6)

该题目和第5章习题5的编程题类似,只不过这里要求使用接口而已。

设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声。要求如下:

  1. 编写接口Animal

Animal接口有2个抽象方法cry()和getAnimaName(),即要求实现该接口的各种具体动物类给出自己的叫声和种类名称。

  1. 编写模拟器类Simulator

该类有一个playSound(Animal animal)方法,该方法的参数是Animal类型。即参数animal可以调用实现Animal接口类重写的cry()方法播放具体动物的声音、调用重写的getAnimalName()方法显示动物种类的名称。

  1. 编写实现Animal接口的Dog类和Cat类

图6.14是Simulator、Animal、Dog、Cat的UML图。

  1. 编写主类Application(用户程序)

在主类Application的main方法中至少包含如下代码:

Simulator simulator = new Simulator();

simulator.playSound(new Dog());

simulator.playSound(new Cat());

 

一、问答题
1.不能。
2.不能。
3.可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中。那么该接口变量就可以调用被类实现的接口中的方法。
4.不可以。
5.可以。
二、选择题
1.D。2.AB。3.B。
三、阅读程序
1.【代码1】:15.0。【代码2】:8。
2.【代码1】:18。【代码2】:15。
四、编程题
Animal.java
public interface Animal {
    public abstract void cry();
    public abstract String getAnimalName();
}
Simulator.java
public class Simulator {
   public void playSound(Animal animal) {
       System.out.print("现在播放"+animal.getAnimalName()+"类的声音:");
       animal.cry();
   }
}
Dog.java
public class Dog implements Animal {
   public void cry() {
      System.out.println("汪汪...汪汪"); 
   }  
   public String getAnimalName() {
      return "狗";
   }
}
Cat.java
public class Cat implements Animal {
   public void cry() {
      System.out.println("喵喵...喵喵"); 
   }  
   public String getAnimalName() {
      return "猫";
   }
}
Application.java
public class Example5_13 {
   public static void main(String args[]) {
      Simulator simulator = new Simulator();
      simulator.playSound(new Dog());
      simulator.playSound(new Cat());
   }
}

 

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值