gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:23:00 25 4
gpt4 key购买 nike

我正在制作一个空中交通管制系统,我有一个 Plane 类,根据是否有飞机来调用它的名称。如果有 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);
}
}
}

}

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

最佳答案

Plane 类中为 planeName 创建一个访问器(getName() 是常见的样式命名法)。从每个 Plane 实例的 Airport 类中调用它。

关于java - 如何将字符串从一个类获取到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10049278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com