[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
}
}
[代码运行效果截图]
[源代码打包下载]