建议将File类对象传递给playClip()
,该对象引用动物声音文件并捕获任何异常,如果捕获到任何异常,则输出“ Meow”。当我运行Main function时,我听到扬声器中的猫叫声。
我是否已正确地将File对象引用传递给playClip()
?
public void makeSound() {
try {
playClip(new File("Cat.wav"));
} catch (Exception e) {
System.out.println("Meow");
}
}
具有以下主要功能
public class Main {
public static void main(String[] args) {
Cat sound = new Cat();
sound.makeSound();
}
}
在您的抽象类中,创建方法playClip:
public abstract class Sound {
abstract public void makeSound();
public void playClip(File clipFile) {
// code plays a file and has some exception handling
}
}
public class Cat extends Sound {
public Cat() {
super();
}
@Override
public void makeSound() {
playClip(new File("/sound_file"));
}
}
当然,playClip()也可以是抽象的。这取决于您想要的。