java 如何在java中动态创建新变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5537363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:35:53  来源:igfitidea点击:

How to create new variable in java dynamically

java

提问by ihavprobs

Is it possible to create new variables in java dynamically.

是否可以在java中动态创建新变量。

class A {
methodA(String variableName) {

      }
}

So if new method is called twice, 2 new variables should be newly added to this class?

那么如果新方法被调用两次,那么这个类应该新添加2个新变量吗?

Is it possible?

是否可以?

回答by Jon Skeet

No. Have you considered storing a Map<String, Object>in the class instead? The keys in the map would be the "variable names" and the values in the map would be the logical variable names.

不。你有没有考虑过Map<String, Object>在类中存储 a ?映射中的键是“变量名”,映射中的值是逻辑变量名。

If you could give more information about what you're trying to achieve (from a high-level perspective) that would help.

如果你能提供更多关于你想要实现的目标的信息(从高层次的角度),那会有所帮助。

回答by aioobe

No, this is not possible to do in Java.

不,这在 Java 中是不可能的。

The fields in a class is determined at compile time and can't be changed during runtime (except though sophisticated techniques such as class reloading though for instance JRebel). I would however not recommend doing this, unless you're writing some IDE for instance.

类中的字段在编译时确定,不能在运行时更改(除非使用复杂的技术,例如类重新加载,例如JRebel)。但是,我不建议这样做,除非您正在编写一些 IDE。

回答by Ben

A class and its members are defined and then compiled to bytecode, so they cannot be readily modified at run-time. That said, there are a number of libraries out there, such as cglib, which provide runtime modification functionality. This page can tell you more: http://java-source.net/open-source/bytecode-libraries

定义类及其成员,然后编译为字节码,因此在运行时不能轻易修改它们。也就是说,有许多库,例如 cglib,它们提供运行时修改功能。这个页面可以告诉你更多:http: //java-source.net/open-source/bytecode-libraries

(This is not to say that runtime modification is the right thing to do!)

(这并不是说运行时修改是正确的做法!)

回答by slezica

In a good design, a class must represent something, semantically speaking. You design it to represent an object in your system.

在一个好的设计中,从语义上讲,一个类必须代表一些东西。你设计它来代表你系统中的一个对象。

If you want to add more things to a design in run-time, well, something's not quite right -- unless, of course, the design needs adding information in run-time, and there are tons of data structures just ready for the job!

如果您想在运行时向设计添加更多内容,那么有些事情不太正确——当然,除非设计需要在运行时添加信息,并且有大量数据结构已准备就绪!

Check out Maps in Java, for example.

Map例如,检查Java 中的 s。

回答by gmhk

Following is the way that i have implemented and helped me to fix my solution easily without much hurdles.

以下是我实施并帮助我轻松解决我的解决方案的方式,没有太多障碍。

// Creating the array List

// 创建数组列表

List accountList = new ArrayList(); 




for(int k=0;k < counter;k++){
        accountList.add(k, (String)flowCtx.getValueAt("transitId"+m));
}

Iterating the loop and adding the objects into the arraylist with the index.

迭代循环并将对象添加到带有索引的数组列表中。

//Retrieving the object at run time with the help of the index

//在运行时借助索引检索对象

String a = accountList.get(i));

回答by Capensis

Using a HashMap could be a solution. For example, if we have the following class:

使用 HashMap 可能是一个解决方案。例如,如果我们有以下类:

class Staff {
    private HashMap<String, Object> mylist = new HashMap<String, Object>() ;

    void setNewVar(String s, Object o) {
        mylist .put(s, o);
    }

    HashMap<String, Object> getVar() {
        return mylist;
    }
}

I can use it as:

我可以将它用作:

staff.setNewVar("NumVars",11);
staff.setNewVar("NumBatches",300);
...

and then:

接着:

staff.getVar()

wherever you need. I use it to convert some variables (the number can change) to JSON, successfully.

无论您需要什么。我用它成功地将一些变量(数字可以改变)转换为 JSON。