Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-观察模式-观察一个类,但访问另一个类中观察到的内容_Java - Fatal编程技术网

Java-观察模式-观察一个类,但访问另一个类中观察到的内容

Java-观察模式-观察一个类,但访问另一个类中观察到的内容,java,Java,我正在创建一个空中交通控制系统。我有一个叫做plane的类,它是一个被观察到的任何变化的类,当一个平面随机生成时,它的名称会发生变化。问题是,机场需要访问该名称的名称以将其存储到数组列表中,如果我可以让它工作的话,可能是一个队列。无论如何,这是机场等级的代码: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package airtra

我正在创建一个空中交通控制系统。我有一个叫做plane的类,它是一个被观察到的任何变化的类,当一个平面随机生成时,它的名称会发生变化。问题是,机场需要访问该名称的名称以将其存储到数组列表中,如果我可以让它工作的话,可能是一个队列。无论如何,这是机场等级的代码:

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package airtrafficcontrolv3;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author
 */
public class Airport
{
        Subject s = new SubjectImpl();

    Plane point = new Plane();
    Queue <String> waiting = new LinkedList<String>();

    public Airport()
    {
        //String name =
        s.getState();
        System.out.println(s);

        while (!waiting.isEmpty())
        {
            System.out.println("Waiting to take off: "+waiting);
            try
            {
                System.out.println("Preparing to taxi: "+waiting.remove());
                Thread.sleep(5000);
            }
            catch (InterruptedException ex)
            {
                Logger.getLogger(Airport.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}
这是我在学科课上学到的:

    package airtrafficcontrolv3;

/*
 * @author S0082708
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

interface Subject
{

    public void addObserver(Observer o);

    public void removeObserver(Observer o);

    public String getState();

    public void setState(String state);
}

/*
 * Creates an observer that will watch for any changes
 */

interface Observer
{
    public void update(Subject o);
}

/*
 * Class implements the observer
 * Observer is what is doing the watching
 * The string is set to blank waiting ready for any updates that will occur
 * Anything in getState is then passed to state
 */
class ObserverImpl implements Observer
{
    private String state = "";

    public void update(Subject o)
    {
        state = o.getState();

        //Need to add text to the label
       // AirTrafficControlv3View.Incoming.add(state);

    }
}

/*
 * Subject is what is being watched
 * The array is then created to store the information ready to add further information
 * Or ready to delete
 * State is set to blank ready for whatever information is being passed
 */

class SubjectImpl implements Subject
{

    private List observers = new ArrayList();
    private String state = "";

    /*
     * Returns whatever is being passed to state
     */

    public String getState()
    {
        //System.out.println(state);
        return state;
    }

    /*
     * Sets the state to current state and then notifies the observer of the changes
     * Made to the state ready to update
     */
    public void setState(String state)
    {
        this.state = state;
        notifyObservers();
    }

    /*
     * Adds the observer along with the name, so several observers can be implemented
     */
    public void addObserver(Observer o)
    {
        observers.add(o);
    }

    /*
     * Removes the observer once it has been used
     */

    public void removeObserver(Observer o)
    {
        observers.remove(o);
    }

    /*
     * The iterator allows the ability to loop through the array
     * While the iterator has a next then it allows the ability to take in more
     * Changes to be observed. It is then updated with the current information
     */

    public void notifyObservers()
    {
        Iterator i = observers.iterator();
        while (i.hasNext())
        {
            Observer o = (Observer) i.next();
            o.update(this);
        }      
    }
}
有谁能告诉我正确的方向,如何从get state获取字符串,该字符串将作为KLM发布,但不会在机场类中发布


任何建议都将受到欢迎。

您需要在从
getState()
返回的对象的类中实现一个
toString()
,您得到的随机数是
object
类中表示实例标识的
toString()
的默认实现,不满足。

您看到类似
SubjectImpl@5a199939
作为
System.out.println(s)
的输出,
toString()
方法在
SubjectImpl
类中未被重写,但除此之外,我不确定您在问什么。谢谢,我以为是这样的,但我想得到第二个意见,因为我不想把我目前所拥有的搞砸。再次感谢你。
    package airtrafficcontrolv3;

/*
 * @author S0082708
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

interface Subject
{

    public void addObserver(Observer o);

    public void removeObserver(Observer o);

    public String getState();

    public void setState(String state);
}

/*
 * Creates an observer that will watch for any changes
 */

interface Observer
{
    public void update(Subject o);
}

/*
 * Class implements the observer
 * Observer is what is doing the watching
 * The string is set to blank waiting ready for any updates that will occur
 * Anything in getState is then passed to state
 */
class ObserverImpl implements Observer
{
    private String state = "";

    public void update(Subject o)
    {
        state = o.getState();

        //Need to add text to the label
       // AirTrafficControlv3View.Incoming.add(state);

    }
}

/*
 * Subject is what is being watched
 * The array is then created to store the information ready to add further information
 * Or ready to delete
 * State is set to blank ready for whatever information is being passed
 */

class SubjectImpl implements Subject
{

    private List observers = new ArrayList();
    private String state = "";

    /*
     * Returns whatever is being passed to state
     */

    public String getState()
    {
        //System.out.println(state);
        return state;
    }

    /*
     * Sets the state to current state and then notifies the observer of the changes
     * Made to the state ready to update
     */
    public void setState(String state)
    {
        this.state = state;
        notifyObservers();
    }

    /*
     * Adds the observer along with the name, so several observers can be implemented
     */
    public void addObserver(Observer o)
    {
        observers.add(o);
    }

    /*
     * Removes the observer once it has been used
     */

    public void removeObserver(Observer o)
    {
        observers.remove(o);
    }

    /*
     * The iterator allows the ability to loop through the array
     * While the iterator has a next then it allows the ability to take in more
     * Changes to be observed. It is then updated with the current information
     */

    public void notifyObservers()
    {
        Iterator i = observers.iterator();
        while (i.hasNext())
        {
            Observer o = (Observer) i.next();
            o.update(this);
        }      
    }
}