public class Main {
public static void main(String[] args){
IMsg[]msgs=new IMsg[3];
msgs[0]=new VoiceMsg();
msgs[1]=new MoneyMsg(300);
msgs[2]=new TextMsg("我给你说哈....");
People p=new People();
p.read(msgs);
}
}
interface IMsg{
public void beReadable();
}
class TextMsg implements IMsg {
String text;
public TextMsg(String text) {
this.text = text;
}
@Override
public String toString() {
return "TextMsg:" + text;
}
class VoiceMsg implements IMsg {
@Override
public void beReadable() {
sound();
}
public void sound() {
System.out.println("播放语音信息");
}
}
class MoneyMsg implements IMsg {
double money;
public MoneyMsg(double money) {
this.money = money;
}
@Override
public String toString() {
return "MoneyMsg:" + money;
}
}
class People {
public void read(IMsg[] msgs) {
//处理每一条消息
for (int i = 0; i < msgs.length; i++) {
msgs[i].beReadable();
}
}
}
}
这段代码如何优化,纠正其中的错误