设计与实现一个 ISoundable 接口,该接口具有发声功能、还能调节音量大小

5年以前  |  阅读数:682 次  |  编程语言:JAVA 
package experiment6;

    public interface ISoundable {
        public void increaseVolume();
        public void decreaseVolume();
        public void stopSound();
        public void playSound();
    }

package experiment6;

public class Radio implements ISoundable {
    public void increaseVolume(){
        System.out.println("增大收音机音量");
    }
    public void decreaseVolume(){
        System.out.println("减小收音机音量");
    }
    public void stopSound(){
        System.out.println("关闭收音机");
    }
    public void playSound(){
        System.out.println("收音机播放广播");
    }
}

package experiment6;

public class Mobliephone implements ISoundable{
    public void increaseVolume(){
        System.out.println("增大收音机音量");
    }
    public void decreaseVolume(){
        System.out.println("减小收音机音量");
    }
    public void stopSound(){
        System.out.println("关闭手机");
    }
    public void playSound(){
        System.out.println("手机音乐播放");
    }

}

package experiment6;

public class MusicPeople {
    private String name;
    private int age;
    private void listen(ISoundable s){
        s.playSound();
    }
}

package sunlin;

public class Walkman implements ISoundable{
    public void increaseVolume(){
        System.out.println("增大随身听音量");
    }
    public void decreaseVolume(){
        System.out.println("减小随身听音量");
    }
    public void stopSound(){
        System.out.println("关闭随身听机");
    }
    public void playSound(){
        System.out.println("随身听播放音乐");
    }
}

package sunlin;
import java.util.Scanner;
public class InitailSound {
    public static void main(String[] args){
    int i;
    MusicPeople musicPeople=new MusicPeople();
    Scanner scanner=new Scanner(System.in);
    ISoundable[] soundDevice=new ISoundable[3];
    soundDevice[0]=new Radio();
    soundDevice[1]=new Walkman();
    soundDevice[2]=new Mobliephone();
    System.out.println("你听到什么?请输入选择:0=收音机 1=随身听 2=手机");
    i=scanner.nextInt();
    int n=0;
    while(i>2&&i<=2){
        System.out.println("输入有误:请重新输入");
        i=scanner.nextInt();
        n++;
    }
    if(i<=2){
        musicPeople.listen(soundDevice[i]);
        soundDevice[i].increaseVolume();
        soundDevice[i].stopSound();
        scanner.close();
    }else{
        System.out.println("输入错误,程序已停止");
    }
    }

    private static ISoundable stopSound() {
        // TODO Auto-generated method stub
        return null;
    }

    private static void nextInt() {
        // TODO Auto-generated method stub

    }
}
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例