2012-04-06 125 views
1

我正在制定一个空中交通管制系统,并且我有一个班级,根据是否有飞机来到,名称被调用。如果有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); 
      } 
     } 
    } 

} 

有没有可能任何人都可以建议如何从飞机班级名称到机场级别请。

+1

可能你可以让机场成为飞机的观察者,所以飞机应该实现可观测的.. – aishwarya 2012-04-06 21:11:56

+0

@aishwarya只是想指出你必须扩展Observable,因为它是一个类,而不是一个接口。构图是解决此问题的好方法。 – jpm 2012-04-06 21:14:40

+0

但是,我仍然可以执行队列吗? – 2012-04-06 21:17:20

回答

0

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

+0

如果我这样做:public getName() { return planeName; }它告诉我,planeName是一个void方法,所以它不能被返回。我已经尝试过了。对不起,我应该说 – 2012-04-06 21:14:13

+1

'public String getName(){return planeName; }'。不要忘记返回类型。 – Makoto 2012-04-06 21:14:47

+0

@SalMichaelson:你必须把它放在自己的方法中。把它放在'run()'里面是没有用的。对不起,我没有赶上! – Makoto 2012-04-06 21:33:27

0

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

0

做一个getter像

public String getName() { 
    return planeName; 
} 

或只是通过

point.planeName 

这也将工作,但会被认为是不好的风格访问它。

+0

我也试过它通过point.planeName它不会返回任何东西...我将不得不尝试通过它出现的观察者 – 2012-04-06 21:25:20

0

由于Plane中的代码是异步运行的,因此getter实现在这里不起作用。为了让观察者模式在这里工作,Plane需要包装一个Observable的实例。然后在某个时刻,机场(需要实现Observer)实例需要在该Observable上注册。

相关问题