实验:接口与多态
一、实验目的
- 学习如何定义接口
- 掌握接口的实现方式
- 使用实现了接口的类
- 掌握接口回调
- 理解接口与抽象类的区别
二、实验一
1. 实验内容
本实验的任务是设计和实现一个Soundable接口,该接口具有发声功能,同时还能调节声音大小(playSound( )、decreaseVolume( )、stopSound( ))。Soundable接口的这些功能将会由3种声音设备来实现,它们分别是:Radio,Walkman和Mobilephone。最后还需设计一个应用程序类来使用这些实现了Soundable接口的声音设备。程序运行时,先询问用户想听哪知设备,然后程序按照该设备的工作方式来输出发音。程序运行效果如下:
2. 类图
main()方法在使用上述接口和类的时候用到了接口的回调
3. 代码
- Soundable接口
package edu.moth12.JieKou;
public interface Soundable {
void playSound();
void decreaseVolume();
void stopSound();
}
- Radio类 implement Soundable
package edu.moth12.JieKou;
public class Radio implements Soundable {
@Override
public void playSound() {
System.out.println("收音机播放广播:中央人民广播电视台");
}
@Override
public void decreaseVolume() {
System.out.println("增大收音机音量");
}
@Override
public void stopSound() {
System.out.println("关闭收音机");
}
}
- Walkman类 implement Soundable
package edu.moth12.JieKou;
public class Walkman implements Soundable {
@Override
public void playSound() {
System.out.println("随身听发出音乐:1234567");
}
@Override
public void decreaseVolume() {
System.out.println("增大随身听音量");
}
@Override
public void stopSound() {
System.out.println("关闭随身听");
}
}
- Mobilephone类 implement Soundable
package edu.moth12.JieKou;
public class Mobilephone implements Soundable {
@Override
public void playSound() {
System.out.println("手机发出来电铃声:叮当,叮当");
}
@Override
public void decreaseVolume() {
System.out.println("增大手机音量");
}
@Override
public void stopSound() {
System.out.println("关闭手机");
}
}
- 测试类
package edu.moth12.JieKou;
import java.util.Scanner;
public class Testdemo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("你想听什么?请输入选择:0-收音机 1-随身听 2-手机");
int command = sc.nextInt();
if(command == 0){
Soundable s = new Radio();
s.playSound();
s.decreaseVolume();
s.stopSound();
}else if(command == 1){
Soundable s = new Walkman();
s.playSound();
s.decreaseVolume();
s.stopSound();
}else if(command == 2){
Soundable s = new Mobilephone();
s.playSound();
s.decreaseVolume();
s.stopSound();
}else
System.out.println("输入无效命令");
}
}
4. 运行结果
三、实验二
1. 实验内容
货车要装载一批货物,货物由三种商品组成:电视、计算机、洗衣机。卡车需要计算出整批货物的重量。
要求有一个ComputeWeight接口,该接口中有一个方法:
public double computeWeight()
有三个实现该接口的类:Television、Computer和WashMachine. 这三个类通过实现接口给出自重。
有一个Truck类,该类用ComputeWeight接口类型的数组作为成员(Truck类面向接口),那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。
程序要求:①Truck中这三种物品的总量范围为30~50,随机生成;②用对象数组来管理;③根据随机数生成Television、Computer和WashMachine三类对象;④输出Truck对象所装载的货物的总重量。
2. 类图
其中Truck里的setCw方法使用了接口的回调,mian方法直接创建和使用的类是Trunk
3. 代码
package edu.moth12.Trunk;
public interface ComputeWeight {
public double ComputeWeight();
}
package edu.moth12.Trunk;
public class Television implements ComputeWeight {
@Override
public double ComputeWeight() {
return 10;
}
}
package edu.moth12.Trunk;
public class Computer implements ComputeWeight {
@Override
public double ComputeWeight() {
return 15;
}
}
package edu.moth12.Trunk;
public class WashMachine implements ComputeWeight {
@Override
public double ComputeWeight() {
return 18.3;
}
}
package edu.moth12.Trunk;
import java.util.Random;
public class Trunk {
ComputeWeight[] cw = new ComputeWeight[50];
public void setCw() {
Random r = new Random();
int size = r.nextInt(20)+30;//随机数确定货车里的当前容纳的总台数
for (int i = 0; i < size; i++) {
int command = r.nextInt(3);
if(command == 0){
ComputeWeight c = new Television();
cw[i] = c;
System.out.print("第"+(i+1)+"台是一台电视机,它的重量是:");
}else if(command == 1){
ComputeWeight c = new Computer();
cw[i] = c;
System.out.print("第"+(i+1)+"台是一台电脑,它的重量是:");
}else{
ComputeWeight c = new WashMachine();
cw[i] = c;
System.out.print("第"+(i+1)+"台是一台洗衣机,它的重量是:");
}
System.out.println(cw[i].ComputeWeight());
}
}
}
package edu.moth12.Trunk;
import java.util.Random;
public class Test {
public static void main(String[] args) {
Trunk t = new Trunk();
t.setCw();
}
}
4. 运行结果
四、实验心得
个人觉得当项目很小的时候没有使用接口的必要,项目比较大的时候,接口的使用能够屏蔽实现,方便维护,方便扩展(接口的多继承),方便使用,能够增加程序的可读性。