要运行我的代码,只需将其复制并粘贴到您的 IDE 中 - 一切都在那里。这是一个使用 Observer-Observable 设计模式的飞行监控程序。FlightStautsMonitor 是监控 Flight 的飞行状态(整数)的 Observer。FlightStatusChangeEvent 是一个记录航班状态变化的类。Demo 是驱动类,或者是带main的类。
我的问题是我不确定我是否使用了 Observer 和 Observable API。我得到一个带有运行时错误的输出 -
Old flight status
F:1, S: 0
F:2, S: 0
F:3, S: 0
F:4, S: 0
F:5, S: 0
Exception in thread "main" java.lang.StackOverflowError
at java.util.Vector.toArray(Unknown Source)
at java.util.Observable.notifyObservers(Unknown Source)
at demo.Flight.updateStatus(Demo.java:40)
at demo.FlightStatusMonitor.update(Demo.java:91)
at java.util.Observable.notifyObservers(Unknown Source)
问题的原因是什么以及如何修复此代码?
//package demo;
import java.util.*;
class FlightStatusChangeEvent{
int status;
Flight flight;
FlightStatusChangeEvent(int statusCode, Flight flight){
this.status = statusCode;
this.flight = flight;
}
public int getStatus(){return this.status;}
public Flight getFlight(){return this.flight;}
}
class Flight extends Observable{
int status;// 0 = on time, -1 = late, +1 = before scheduled time
String flightCode;
Flight(String flightCode){
this.flightCode = flightCode;
}
public void updateStatus(int statusCode){
this.status = statusCode;
FlightStatusChangeEvent fsce = new FlightStatusChangeEvent(status, this);
setChanged();
notifyObservers(fsce);
}
public String getFlightCode(){return this.flightCode;}
public String toString(){return "F:" + flightCode + ", S: " + status;}
}
class FlightStatusMonitor implements Observer{
public static ArrayList<Flight> flights = new ArrayList<Flight>();
static{
for(int i = 1; i < 6; i++){
Flight ff = new Flight("" + i);
flights.add(ff);
}
}
//keep calling this method every 10 sec to get latest flight status
public static void displayStatusOfFlights(){
//print all flight statuses in array list - flights
for(Flight fl : flights){
System.out.println(fl);
}
}
public void update(Observable flight, Object flightStatusEvent){
FlightStatusChangeEvent fsce = (FlightStatusChangeEvent) flightStatusEvent;
Flight fl = fsce.getFlight();
String code = fl.getFlightCode();
int status = fsce.getStatus();
//find the flight in array flights and then update its status
for(int i = 0; i < flights.size(); i++){
Flight fli = flights.get(i);
String flCode = fli.getFlightCode();
if(flCode.equals(code)){
fli.updateStatus(status);
System.out.print("Flight status updated !");
break;
}
}
}
}
public class Demo{
public static void main(String[]args){
FlightStatusMonitor fsm = new FlightStatusMonitor();
System.out.println("Old flight status");
ArrayList<Flight> fllist = fsm.flights;
fsm.displayStatusOfFlights();
for(Flight fl : fllist ){
fl.addObserver(fsm);
}
fsm.flights.get(1).updateStatus(-1);
System.out.println("New flight status");
fsm.displayStatusOfFlights();
}
}