2017-03-27 63 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