public interface Soundable { public void increaseVolume( ); public void decreaseVolume( ); public void stopSound( ); public void playSound( ); }
3.创建三个类Radio、Walkman和Mobilephone具体实现,分别添加代码:
// Mobilephone类
package experiment5_2;
class Mobilephone implements Soundable{ public void increaseVolume( ) { System.out.println(\增大手机音量\ } public void decreaseVolume( ) { System.out.println(\减小手机音量\ } public void stopSound( ) { System.out.println(\关闭手机\ } public void playSound( ) { System.out.println(\手机发出来电铃声\ } }
// Walkman类
package experiment5_2;
class Walkman implements Soundable { public void increaseVolume( ) { System.out.println(\增大随声听音量\ } public void decreaseVolume( ) { System.out.println(\减小随声听音量\ } public void stopSound( ) { System.out.println(\关闭随声听\ } public void playSound( ) { System.out.println(\随声听发出音乐\ } }
// Radio类
package experiment5_2;
class Radio implements Soundable{ public void increaseVolume( ) { System.out.println(\增大收音机音量\ } public void decreaseVolume( ) { System.out.println(\减小收音机音量\ } public void stopSound( ) { System.out.println(\关闭收音机\ } public void playSound( ) { System.out.println(\收音机播放广播\ } }
4.创建people类及编写测试代码:
//People类
package experiment5_2; class People {
public void listen(Soundable s) { s.playSound( ); } }
//测试代码
package experiment5_2; import java.util.Scanner; public class InterfaceTest {
public static void main(String[] args) { int i; People sportsman = new People( );
Scanner scanner = new Scanner(System.in); Soundable[] soundDevice = new Soundable[3]; //往声音设备数组中放入能发声的设备 soundDevice[0] = new Radio( ); soundDevice[1] = new Walkman( ); soundDevice[2] = new Mobilephone(); System.out.println(\你想听什么? 请输入选择:0-收音机 1-随声听 2-手机\
i = scanner.nextInt( ); //开始听声音 sportsman.listen(soundDevice[i]);
soundDevice[i].increaseVolume( ); soundDevice[i].stopSound(); scanner.close(); } }
? 实验内容三
1.建立package experiment5_3,其最终目录结构如下
package experiment5_3; import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class DateTransf extends JFrame implements KeyListener { /** * */
private static final long serialVersionUID = 1L; private static DateTransf frm; private static JTextField txt; private static JTextField data; DateTransf() { setTitle(\数字与星期转换\ setLocation(700, 300); setSize(400,130); }
public static void main(String[] args) { frm = new DateTransf(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setLayout(new GridLayout(2,2)); txt = new JTextField(12); data = new JTextField(12); JLabel lable1 = new JLabel(\输入数字:\ JLabel lable2 = new JLabel(\星期几:\ frm.add(lable1); txt.addKeyListener(frm); frm.add(txt); frm.add(lable2); frm.add(data); frm.setVisible(true); }
public void keyPressed(KeyEvent e) { if (e.getSource() == txt) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) // 判断按下的键是否是回车键 { try { int number = Integer.parseInt(txt.getText()); switch (number) { case 1:data.setText(\ break; case 2:data.setText(\ break; case 3:data.setText(\ break; case 4:data.setText(\ break; case 5:data.setText(\ break; case 6:data.setText(\ break; case 7:data.setText(\ break; default: JOptionPane.showMessageDialog(null, \您输入的数字不是1~7\无效日期\ break; } }catch (Exception e1) { // TODO: handle exception JOptionPane.showMessageDialog(null, \您输入的不是整数\无效日期\TION_MESSAGE); } } } }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { } }
2.编译运行
五、实验结果