要运行我的代码,只需将其复制并粘贴到您的IDE中就可以了。这是一个使用Observer-Observable设计模式的飞行监视程序。 FlightStautsMonitor是观察者,它监视航班的飞行状态(整数)。 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();

}


}

最佳答案

当堆栈溢出发生时,会抛出StackOverflowError,因为应用程序的递归太深了。

您的问题出在update方法中。
您正在调用updateStatus方法,并且updateStatus方法将再次调用update
删除fli.updateStatus(status);将解决您的问题。

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)) {
                // THIS WILL CAUSE STACKOVERFLOWERROR
                fli.updateStatus(status);
                System.out.print("Flight status updated !");

                break;

            }

        }

    }

10-08 01:58