我想创建5-6个类,我存储在哈希图中的值在第一个类&我想调用它从第4,第5和第6个class.How,以获得此任何代码片段或示例来实现这将是有帮助的,谢谢
发布于 2012-01-04 14:27:17
public class Example {
private HashMap<String, String> hashmap = new HashMap<String, String>();
public HashMap<String, String> getHashmap() {
return hashmap;
}
public void setHashmap(HashMap<String, String> hashmap) {
this.hashmap = hashmap;
}
}
public class AnotherClass {
public static void main(String args[]) {
Example ex = new Example();
HashMap<String, String> hm = ex.getHashmap();
}
}发布于 2012-01-04 14:30:29
您应该为hashmap使用setter和getter。
private HashMap h = null;
//instantiate hashmap in the constructor
public ...
//add value to hashmap
public void add(Object value)
{
h.put(value);//eventually cast value or declare it as you did it in the hashmap
}
//get hashmap
public HashMap getMap()
{
return h;
}
//set hashmap
public void setMap(HashMap hm)
{
h=hm;
}...发布于 2012-01-04 14:33:57
两种合理的方法。
纯粹主义者有一个“迪米特定律”,说总是做选项2,但我认为这往往是夸大其词。
https://stackoverflow.com/questions/8722822
复制相似问题