Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA在对象名中使用变量_Java_Variables - Fatal编程技术网

JAVA在对象名中使用变量

JAVA在对象名中使用变量,java,variables,Java,Variables,下面是我班的一些代码: public class update implements ItemListener { private String TBL; public void init(String pav, String type) { try { this.TBL = type; Connection conn = sqlite.ConnectDb(); Statement stat = conn.createStatemen

下面是我班的一些代码:

public class update implements ItemListener {

private String TBL;

public void init(String pav, String type) { 
    try {
        this.TBL = type;
        Connection conn = sqlite.ConnectDb();
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("SELECT * FROM "+TBL+"_imones order by pav asc;");

        pard_reg_imone_choice.removeAll();
        pard_imone_choice.removeAll();

        pard_reg_imone_choice.addItem("VISOS");

        while (rs.next()) {
            pard_reg_imone_choice.addItem(rs.getString("pav"));
            pard_imone_choice.addItem(rs.getString("pav"));
        }

        pard_imone_choice.addItemListener(this);

        rs.close();
我需要这样的东西:

{variable}_reg_imone_choice.removeAll();
{variable}_imone_choice.removeAll();
  Map<String, Choice> choices = ... // initialize
  ....
  String prefix = ...
  ....
  choices.get(prefix + "_reg_imone_choice").removeAll();
  choices.get(prefix + "_imone_choice").removeAll();
变量是字符串类型(它是“pirk”和“pard”)


谢谢大家!

如果案例不太多,可以使用switch语句


除了map方法之外,其他人建议我能想到的唯一机制就是反射

Field field = this.getClass().getField(name+"_reg_imone_choice");
Object object = field.get (this);
((List) object).removeAll ();

在Java中,不能将字符串插入变量名。不是在运行时。不是在编译时。Java变量名必须在编译时完整拼写

在Java中,最接近这一点的方法是使用
Map
。。。大概是这样的:

{variable}_reg_imone_choice.removeAll();
{variable}_imone_choice.removeAll();
  Map<String, Choice> choices = ... // initialize
  ....
  String prefix = ...
  ....
  choices.get(prefix + "_reg_imone_choice").removeAll();
  choices.get(prefix + "_imone_choice").removeAll();
Map选项=…//初始化
....
字符串前缀=。。。
....
choices.get(前缀+“\u reg\u imone\u choice”).removeAll();
choices.get(前缀+“\imone\u choice”).removeAll();
如果变量是静态字段或实例字段,也可以使用反射来实现这一点。(反射不能用于访问局部变量或方法参数。)


然而,对于有经验的Java程序员来说,这些解决方案是不合适的,因为您引入了各种各样的运行时检查。。。以及各种不必要的(IMO)脆弱性


当你开始用Java的方式思考问题时,通常会有更好的方法来做这类事情。

你是否有四个实例名为
pard\u reg\u imone\u choice
pirk\u reg\u imone\u choice
pard\u imone\u choice
pirk\u imone\u choice
?(顺便说一句,Java命名约定使用驼峰大小写,您也应该采用驼峰大小写)您可能真的想要一个
Map
,而不是硬编码的变量名。我知道,我认为您的设计很糟糕。在Java中,不可能在对象名中使用变量。@Rytis您正在寻找的语言“功能”称为HashMap。甚至在支持变量名动态求值的语言中。。。那样做是不对的。因为这就是HashMap等“关联数据结构”的发明目的。另一个选择是拥有不同的列表;然后,您只需在列表上迭代,并对放入“list list”中的所有列表对象调用“removeAll()”。理论上,您可以使用java反射通过子字符串搜索来访问变量;但那是。。。再次:错。“2”不是一个有效的英语单词。