简体   繁体   English

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

[英]How to get a string from one class to another

I'm making an air traffic control system and I have a class Plane where a name is called depending on if there is a plane coming.我正在制作一个空中交通管制系统,我有一个 class 飞机,根据是否有飞机来叫名字。 If there is 1 plane then it says KLM if there's none then it says that there is no plane.如果有 1 架飞机,则表示 KLM,如果没有,则表示没有飞机。

I am looking for a way to get this plane name from the plane class to the airport class to put into a queue.我正在寻找一种方法将这个飞机名称从飞机 class 到机场 class 放入队列中。 This is the code for the plane class这是飞机代码 class

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

This is what is in my airport class.这是我机场 class 里的东西。

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

}

Is it possible that anyone can suggest how to get the name from the plane class to the airport class please.是否有人可以建议如何从飞机 class 到机场 class 获取姓名。

Create an accessor for planeName in the class Plane ( getName() is a common style nomenclature).在 class Plane中为planeName创建一个访问器( getName()是一种常见的样式命名法)。 Call it from your Airport class per instance of Plane .每个Plane实例从您的Airport class 调用它。

wouldn't it just be point.planeName You have a member that is a Plan in your airport class call point.不会只是point.planeName您有一个成员是您机场 class 呼叫点的计划。 Not sure exactly what your are asking...不确定你到底在问什么......

Make a getter like做一个吸气剂

public String getName() {
    return planeName;
}

or just access it through或者只是通过访问它

point.planeName

Which would also work but would be considered bad style.这也可以,但会被认为是糟糕的风格。

The getter implementations won't work here, since the code in Plane is running asynchronously. getter 实现在这里不起作用,因为 Plane 中的代码是异步运行的。 To get the Observer pattern to work here, Plane needs to wrap an instance of Observable.为了让 Observer 模式在这里工作,Plane 需要包装一个 Observable 实例。 Then at some point, an Airport (which needs to implement Observer) instance needs to be registered with that Observable.然后在某个时候,需要向该 Observable 注册一个 Airport(需要实现 Observer)实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM