gpt4 book ai didi

Java 对象引用问题?

转载 作者:行者123 更新时间:2023-11-29 08:34:35 25 4
gpt4 key购买 nike

我有以下类(class);

public class Payload{

private Map<String, Object> map;

public static Payload INSTANCE = new Payload();

private Payload(){
map = new HashMap<>();
}

public Payload put(String key, Object value){
map.put(key, value);
return this;
}

public Map<String, Object> getMap(){
return map;
}
}

public class AjaxRequestBinder {

private String url;
private String method;
private Map<String, Object> data;
private String dataType;

public AjaxRequestBinder(String url, String method, Payload payload, AjaxDataType dataType) {
this.url = url;
this.method = method;
this.data = payload != null ? payload.getMap() : Payload.INSTANCE.getMap();
this.dataType = dataType != null ? dataType.name() : AjaxDataType.html.name();
}
//... getters() & setters()
}

public List<AjaxRequestBinder> getSampleAjaxBinders() throws Exception {
List<AjaxRequestBinder> requestBinders = new ArrayList<>();
requestBinders.add(new AjaxRequestBinder(getEndpointURL(ServiceModule.CAT), HttpMethod.GET.name(), null, AjaxDataType.json));
requestBinders.add(new AjaxRequestBinder(getEndpointURL(ServiceModule.DOG), HttpMethod.GET.name(), null, AjaxDataType.json));
requestBinders.add(new AjaxRequestBinder(getEndpointURL(ServiceModule.CHICKEN), HttpMethod.GET.name(), null, AjaxDataType.json));
requestBinders.add(new AjaxRequestBinder(getEndpointURL(ServiceModule.GOAT), HttpMethod.GET.name(), null, AjaxDataType.json));
requestBinders.add(new AjaxRequestBinder(getEndpointURL(ServiceModule.RABBIT), HttpMethod.POST.name(), buildPayload(ServiceModule.RABBIT, HttpMethod.POST), AjaxDataType.json));
return requestBinders;
}

public Payload buildPayload(ServiceModule module, HttpMethod httpMethod) throws Exception {
Payload payload = Payload.INSTANCE;
module = module != null ? module : ServiceModule.NONE;

if(httpMethod.equals(HttpMethod.POST)){

switch(module){
case CAT:{
// Do nothing
}break;
case DOG:{
// Do nothing
}break;
case CHICKEN:{
// Do nothing
}break;
case GOAT:{
// Do nothing
}break;
case RABBIT:{
payload.put("color", "white").put("action", "hops");
}break;
}
}else{
throw new NotYetImplementedException();
}
return payload;
}

但是由于某些奇怪的原因,当方法 getSampleAjaxBinders() 被调用时,它返回一个 AjaxRequestBinder 对象列表,每个对象都具有;

data = {"color":"white", "action":"hops"}

而这仅对最后添加的项目是必需的。所有之前添加的项目都应该只有 data = {}(空 map )。当我通过该方法进行逐步调试时,我发现一切正常,直到 buildPayload(ServiceModule module, HttpMethod httpMethod) 被调用,然后自动覆盖列表中先前添加的项目中的空映射。

有人可以向我解释是什么导致了这里出现的这个奇怪的对象引用问题吗?

最佳答案

发生这种情况是因为您总是使用 Payload 的单个实例,它恰好是为 RABBIT 设置的。

您的 buildPayload 方法返回设置为共享实例的 payload:

Payload payload = Payload.INSTANCE;

同时,当您将null 有效负载传递给AjaxRequestBinder 构造函数时,该构造函数使用相同的Payload.INSTANCE:

this.data = payload != null ? payload.getMap() : Payload.INSTANCE.getMap();

您可以通过公开 Payload 构造函数并在 buildPayload 中创建新实例来修复它,或者为 Payload 创建一个单独的空实例在将 null 提供给 AjaxRequestBinder 构造函数的情况下使用:

public static final Payload INSTANCE = new Payload();
// Add this line to Payload
public static final Payload EMPTY = new Payload();
...
// Use EMPTY payload when the caller does not supply an actual one:
this.data = payload != null ? payload.getMap() : Payload.EMPTY.getMap();

请注意,如果继续使用上面的共享实例方法,则需要在 buildPayload 方法中清除映射。

关于Java 对象引用问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com