如何从一个类到另一个类获取字符串

时间:2012-04-06 21:09:00

标签: java

我正在建立一个空中交通管制系统,我有一个类飞机,根据是否有飞机来调用名称。如果有1架飞机那么它就说KLM如果没有那么它会说没有飞机。

我正在寻找一种方法来将这个飞机名称从飞机类升级到机场类以放入队列。这是平面类的代码

package airtrafficcontrolv3;

import java.util.TimerTask;


class Plane
        extends TimerTask
{

    public int nextPlaneLoop = 0;
    public int planes;
    public int fuel;
    public String planeName;

    @Override
    public void run()
    {
        Observer o = new ObserverImpl();
        Subject s = new SubjectImpl();

        if (nextPlaneLoop <= 167)
        {
            //Currently only running 1 or 0 planes...
            planes = (int) (Math.random() * ((2 - 1) + 1));
            //System.out.println("Random generated plane amount: " + planes);
            //System.out.println("Method called, one whole day loop");
            //Adds to the plane in the airspace loop
            nextPlaneLoop++;
            //System.out.println("Loop incrementing: " + nextPlaneLoop);

            if (planes == 0)
            {
                //System.out.println("No fuel is required as no planes are coming in");
                planeName = "No incoming plane";
                //System.out.println("Planes name is: " + planeName);

                System.out.println("Inbound amount of planes: "+planes);
                System.out.println("Inbound: " + planeName);
                System.out.println("Inbound fuel amount: None ");

                System.out.println(" ");
            }
            else
            {
                //Amount of fuel
                fuel = 30 + (int) (Math.random() * ((120 - 30) + 1));
                //System.out.println("Random fuel: " + fuel);
                planeName = "KLM AirFrance";
                System.out.println("Inbound amount of planes: "+planes);
                System.out.println("Inbound: " + planeName);
                System.out.println("Inbound fuel amount: "+fuel);

                System.out.println(" ");
            }
        }
        else
        {
            this.cancel();
            System.out.println("Day Finished");
        }

                s.addObserver(o);
                s.setState(planeName);

                System.out.println(planeName);

                //finalName = planeName;
                Airport point = new Airport();

                //System.out.println(planeName);
    }
}

这就是我机场的课程。

/*
 * 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;

public class Airport
{
    Plane point = new Plane();
    Queue <String> waiting = new LinkedList<String>();

    public Airport()
    {
        //waiting.add(point.);

        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);
            }
        }
    }

}

是否有人可以建议如何从飞机舱到机场舱的名称。

4 个答案:

答案 0 :(得分:0)

在班级planeName中为Plane创建一个访问者(getName()是一种常见的样式命名法)。每个Airport实例从Plane课程中调用它。

答案 1 :(得分:0)

不会只是point.planeName您的机场班级呼叫点中有一个计划成员。不确定你在问什么......

答案 2 :(得分:0)

一样吸气
public String getName() {
    return planeName;
}

或只是通过

访问它
point.planeName

哪种方法也有效,但会被认为是不好的风格。

答案 3 :(得分:0)

getter实现在这里不起作用,因为Plane中的代码是异步运行的。为了使Observer模式在这里工作,Plane需要包装一个Observable实例。然后在某个时刻,需要在Observable中注册一个Airport(需要实现Observer)实例。