2012-12-21 39 views
1

對象我有代碼看起來像這樣:印刷只有一個從一個HashMap

public Flight{ 
String code; 
char status; 
char type; 

    Flight(String code, char status, char type){ 
    this.code = code; 
    this.status = status; 
    this.type = type; 
    } 
} 


public Menu{ 
Flight flight1 = new Flight("DL123",'A','D'); 
Flight flight2 = new Flight("DL146",'A','I'); 
flightMap.put("DL123", flight1) 
flightMap.put("DL146", flight2) 
} 


    if(schedule.flightMap.containsKey(decision)) 
    { 

    } 

如果用戶輸入DL123和containsKey方法返回true,我想回到 flight1的對象屬性。我將如何能夠做到這一點?我已經嘗試覆蓋toString,但因爲toString只能返回字符串,我不知道如何返回狀態和字符屬性。

請詢問您是否需要更多信息!

+1

從map中調用並調用toString(),假設toString()被任何想要打印的內容覆蓋。 – kosa

+0

如果您想要從單個函數返回所有屬性,則需要將它們包裝在單個對象中,因爲函數只能返回一件事......所以您只需返回整個對象。對象的字段(或屬性)不獨立於該對象存在。 – ApproachingDarknessFish

+0

請詳細解釋你的問題。當您調用flightMap.get(「DL123」)時,您將在地圖中添加具有字符串鍵(例如「DL123」)的飛行物體,它將自動返回與該鍵相關的對象。你是否只想返回一個屬性或飛行對象。 –

回答

3

定義在Flightgetter方法,然後:

if(schedule.flightMap.containsKey(decision)){ 
    Fligth matchingFlight = schedule.flightMap.get(decision); 
    String code = matchingFlight.getCode(); 
    char status = matchingFlight.getStatus(); 
    char type = matchingFlight.getType(); 
} 
+0

正是我所需要的。 – Brian

+0

@Brian你的意思是,這有幫助嗎? –

+0

@Brian然後接受答案。人們會考慮回答其他問題。 – vels4j

1

飛行航班= schedule.flightMap.get(決定);

從飛行物體

然後,您可以檢索所有的值

1

你需要的是

Flight flight = schedule.flightMap.get(decision); 

與這些你可以簡單地訪問對象,因爲他們的知名度是默認這樣

flight.code 
flight.status 

但更符合道德的方式是定義所有像這樣的變量的獲得者和設置者

public void setCode(String code) 
{ 
    this.code = code; 
} 
public String getCode() 
{ 
    return this.code; 
} 

這種方式,你可以使用這個

String code = flight.getCode(); 

同時參閱

Why use getters and setters in java

+0

我認爲Java自動假定這些字段由於OO原則是私人的。 – Brian

+1

@Brian:不,他們有默認的知名度。有關更多信息,請參閱http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html –

0

我試圖解決你的問題,得出的結論得到了變數。見下面的代碼。

package com.rais; 

import java.util.HashMap; 
import java.util.Map; 

/** 
* @author Rais.Alam 
* @project Utils 
* @date Dec 21, 2012 
*/ 


public class FlightClient 
{ 
/** 
* @param args 
*/ 
public static void main(String[] args) 
{ 
    Map<String,Flight> flightMaps = new HashMap<String, Flight>(); 
    Flight flight1 = new Flight("DL123", "STATUS-1", "TYPE-1"); 
    Flight flight2 = new Flight("DL124", "STATUS-2", "TYPE-2"); 
    Flight flight3 = new Flight("DL125", "STATUS-3", "TYPE-3"); 

    flightMaps.put("DL123", flight1); 
    flightMaps.put("DL124", flight2); 
    flightMaps.put("DL125", flight3); 

    System.out.println(getValue(flightMaps, "DL123")); 


} 


public static String getValue(Map<String,Flight> flightMaps, String key)  
{ 
    if(flightMaps !=null && flightMaps.containsKey(key)) 
    { 
     return flightMaps.get(key).status; 
    } 
    else 
    { 
     throw new RuntimeException("Flight does not exists"); 
    } 


    } 
} 

    class Flight 
    { 
    String code; 
    String status; 
    String type; 
    /** 
    * @param code 
    * @param status 
    * @param type 
    */ 
    public Flight(String code, String status, String type) 
    { 
     super(); 
     this.code = code; 
     this.status = status; 
     this.type = type; 
    } 



}