2017-03-27 65 views
-1

它可能是一個簡單的問題,甚至不可能沒有任何類型的接口(數組,地圖等),但我想知道是否有任何將對象名稱轉換爲字符串的可能性,所以我可以作爲參數傳遞。我有兩個類Paciente和Sintomas與多個對象,我需要作爲參數傳遞給一個函數,但我不想使用數組(它必須是這樣),我不能想出任何其他方式這樣做,而無需手動爲每一個插入一個插入。將字符串轉換爲對象名稱Java

Paciente Paciente1 = new Paciente("001", "Ana Melo", 33, ""); 
Paciente Paciente2 = new Paciente("002", "Rui Costa", 13, ""); 
Paciente Paciente3 = new Paciente("003", "Joana Martins", 85, ""); 
Paciente Paciente4 = new Paciente("004", "Pedro Torres", 53, ""); 
Paciente Paciente5 = new Paciente("005", "Ana Gomes", 93, ""); 
Paciente Paciente6 = new Paciente("006", "Jorge Costa", 56, ""); 

Sintomas Sintoma1 = new Sintomas("001", "febre"); 
Sintomas Sintoma2 = new Sintomas("001", "dores"); 
Sintomas Sintoma3 = new Sintomas("001", "machas"); 
Sintomas Sintoma4 = new Sintomas("002", "febre"); 
Sintomas Sintoma5 = new Sintomas("002", "manchas"); 
Sintomas Sintoma6 = new Sintomas("003", "febre"); 
Sintomas Sintoma7 = new Sintomas("003", "dores"); 
Sintomas Sintoma8 = new Sintomas("004", "febre"); 
Sintomas Sintoma9 = new Sintomas("006", "manchas"); 
Sintomas Sintoma10 = new Sintomas("006", "dores"); 

// now I would like to pass to a function as argument something like this: 

for(int i = 0 ; i < 6 ; i++) 
    kSession.insert("Paciente"+(i+1)); 

// instead of making 

kSession.insert(Paciente1); 
kSession.insert(Paciente2); 
kSession.insert(Paciente3); 

// and so on. 
+0

Paciente和Sintomas的意思是pa西班牙語中的「tient」和「symptoms」,對於那些好奇的人來說。 –

+3

所以你基本上想要eval ...使用數組... – Li357

+0

對象沒有名稱。使用參考。你在找「地圖」嗎? – EJP

回答

1

像這樣的東西應該工作(asuming你的意思是否定的,因爲大小的限制陣列),注意,必須有某個地方你添加數據,這也可以從一個txt什麼加載它,但它已經在某些時候被定義

List<Paciente> pacientes = new ArrayList<>(); // no size constraints, automatically expands if too small 

pacientes.add(new Paciente("", "", "")); 

for (Paciente paciente : pacientes) { // loop all Patientes in pacientes 
    kSession.insert(paciente); // add a paciente to the session, for every entry 
} 

ofcource同樣可以做任何類或對象

這一切真的可以歸結爲,你怎麼想存儲和訪問數據,以及在哪裏你需要存儲和訪問它。使用ArrayList和地圖的報價很容易改變的大小和數據的列表內容的實用性,但由於任何數據,必須首先插入

作爲一個側面說明,如果患者有那麼一個ID使用地圖

Map<String, Paciente> pacientes = new HashMap<>(); 

提供了一種非常快速地訪問病人的方法,並且TreeMap結構按鍵排序,如果需要的話。

其他選項可能是 管理數據的包裝類將類似於ArrayList <>,但您可以從列表中定義添加,刪除等規則。

public class Wrapper{ 
    private List<Paciente> pacientes = new ArrayList<>(); 

    public void addPaciente(Paciente paciente){ 
     if(!this.pacientes.contains(paciente)) // prevent multi entries 
      paciente.add(paciente); 
    } 

    public void addPacientes(List<Paciente> pacientes){ 
     for(Paciente paciente : pacientes) // add all patients using the add method 
      this.addPaciente(paciente); 
    } 

    public List<Paciente> getPacientes(){ 
     return this.pacientes; 
    } 
} 

然後,您可以添加病人到kSession,如前所述

最後,沒有理由Paciente,可以有Sintomas的列表,以便

public class Paciente{ 
    private List<Sintomas> sintomas = new ArrayList<>(); 

    public addSintomas(Sintomas sintomas){ 
     if(!this.sintomas.contains(sintomas)) 
      this.sintomas.add(sintomas); 
    } 
    // rest is the same principle as above in "Wrapper" 
} 

這方式你可以得到一個Paciente,並添加一個Sintomas,然後當你想檢查一個Pacientes Sintomas時,你可以從該Paciente得到Sintomas的列表

+0

@MiguelRuivo這樣的工作? – Mikenno

+0

感謝您的回覆,但我不想使用數組或地圖。我寧願讓java通過傳遞名稱作爲字符串來獲取對象。可能是不可能的,我不知道。 – Miguel

+0

@MiguelRuivo你在這裏描述的是激動人心的是一個映射是什麼,它是一種方法來存儲值的基礎上,例如一個字符串的關鍵,然後使用所說的字符串Map 返回它,如果你的意思一個對象TYPE然後將其保存爲.getClass()。toStrin(),然後通過循環獲取它,它認爲它尋找相同的.getClass()。toString()注意,這是IMPLEMENTATION而不是接口,它會查找那麼。但是你必須認識到,除非你將它保存在數據結構中,否則你不能尋找它 – Mikenno