可以请下面提供一些建议吗?

private String dataName;
private String sourceType;

public void getConnectionData(){
    DataCollector data = new DataCollector();
    portNumber = data.getConnectionData(sourceType, dataName);
    channel  = data.getConnectionData(sourceType, dataName);
    hostName = data.getConnectionData(sourceType, dataName);
    queueName = data.getConnectionData(sourceType, dataName);
    queueManager = data.getConnectionData(sourceType, dataName);

}


我有上面的方法,当我尝试使用其他类中的方法时,我试图使dataName等于上面的每个。

    dataName = "portNumber";
    dataName = "channel";
    dataName = "hostName";
    dataName = "queueName";
    dataName = "queueManager";


有人可以建议我怎么做吗?

谢谢

最佳答案

为什么不像平常一样:

portNumber = data.getConnectionData(sourceType, "portNumber");
channel  = data.getConnectionData(sourceType, "channel");
hostName = data.getConnectionData(sourceType, "hostName");
queueName = data.getConnectionData(sourceType, "queueName");
queueManager = data.getConnectionData(sourceType, "queueManager");

10-06 13:20