假设我有:
class Event {}
class DoorBell extends Event {}
class PhoneCall extends Event {}
class EventGenerator {
static Event getEvent() {
if (today.isSunday())
return new DoorBell()
else
return new PhoneCall();
}
}
class EventHandler {
void HandleEvent(DoorBell doorBell) { answer door; }
void HandleEvent(PhoneCall phoneCall) { answer phone; }
void consumeEvent() {
Event e = EventGenerator.getEvent();
HandleEvent(e);
}
}
由于 HandleEvent(Event) 未定义,HandleEvent(e) 无法编译。有解决方案吗?还是我必须问一下它是什么类型的事件?