我正在尝试为以下问题找到解决方案:
我有课 Alert
它被创建了很多次,它的实例显示为一个类型的字段:List<Alert>
.
Alert
的领域之一假设使用作为预定义字典的 map (此项永远不会更改)。
如何配置 map
因此不必在每次创建 Alert
时都创建它?
含义:
public class Alerts {
private List<Alert> list;
.
.
.
public Alerts() {
list = new ArrayList<Alert>();
for (...) {
Alert alert = new Alert();
list.add(alert);
}
}
}
public class Alert() {
private String str = "";
.
.
.
public Alert() {
for (int i = 0; i < map.size; i++) {
str += map.get(i); //map is visible and never changes for any
//instance
}
}
}
最佳答案
听起来这个map
应该是您类的静态成员。您可以在静态初始化程序 block 中对其进行初始化。
关于java - 字典作为类的不同实例的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074873/