/**
本bolog为吉软58班作业贴 更新日期:2018年6月3日 最后更新时间:11:04
使用本博代码请与博主联系
**/
Media接口的具体实现:
package edu.ccst.jlu;public interface MediaPlayer {void play();//播放void stop();//停止void open();//开仓}
TapePlayer类:
package edu.ccst.jlu;public class TapePlayer implements MediaPlayer{@Overridepublic void play() {// TODO Auto-generated method stubSystem.out.println("播放磁带");}@Overridepublic void stop() {// TODO Auto-generated method stubSystem.out.println("停止播放磁带");}@Overridepublic void open() {// TODO Auto-generated method stubSystem.out.println("打开磁带仓");}}
DVDPlayer类
package edu.ccst.jlu;public class DVDPlayer implements MediaPlayer{@Overridepublic void play() {// TODO Auto-generated method stubSystem.out.println("播放DVD");}@Overridepublic void stop() {// TODO Auto-generated method stubSystem.out.println("停止播DVD");}@Overridepublic void open() {// TODO Auto-generated method stubSystem.out.println("打开DVD仓");}}
CDPlayer类
package edu.ccst.jlu;public class CDPlayer implements MediaPlayer{@Overridepublic void play() {// TODO Auto-generated method stubSystem.out.println("播放CD");}@Overridepublic void stop() {// TODO Auto-generated method stubSystem.out.println("停止播放CD");}@Overridepublic void open() {// TODO Auto-generated method stubSystem.out.println("打开CD仓");}}
Controller类,实现三种播放器的功能调用:
package edu.ccst.jlu;public class Controller {private MediaPlayer[] players;public Controller(){//构造函数初始化player数组players = new MediaPlayer[3];players[0] = new TapePlayer();players[1] = new DVDPlayer();players[2] = new CDPlayer();}//对相应的设备调用play方法public void play(int i){players[i].play();}//对相应的设备调用stop方法public void stop(int i){players[i].stop();}//对相应的设备调用open方法public void open(int i){players[i].open();}
}
测试函数:
package edu.ccst.jlu;public class Test {public static void main(String[] args) {Controller c = new Controller();//对磁带三个操作c.play(0);c.stop(0);c.open(0);//对DVD三个操作c.play(1);c.stop(1);c.open(1);//对CD三个操作c.play(2);c.stop(2);c.open(2);}
}
运行结果输出显示: