我有一个设计问题:让我用简单的例子来解释:
Public class A()
{
public static HashMap map = new HashMap();
public static String url = "default";
static {
getJson();
}
//url getters and setters are defined
public static getJson() {
//code which uses url to get json and populate hashmap
}
public string getresult(String key) {
//uses hashmap to send result.
}
我使用静态初始化块是因为我希望只获取一次JSON。
public class B {
//我想改变url并调用getJson方法。如果我调用A.setUrl(),则在设置url之前,A.getJson()方法将按照静态初始化块中的方式被调用。如何先设置URL然后再调用getJson()方法。
//我希望先调用A.setUrl()方法设置URL,然后再调用getJson()方法。您可以使用以下代码:
A.setUrl(url);
A.getJson();
//is this a bad design?
}