2012-04-06 123 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上註冊。

相關問題