代码之家  ›  专栏  ›  技术社区  ›  SourceVisor

Java对象引用问题?

  •  1
  • SourceVisor  · 技术社区  · 7 年前

    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 = {} buildPayload(ServiceModule module, HttpMethod httpMethod)

    谁能给我解释一下,是什么导致了这里出现的这个奇怪的物体参照问题;

    1 回复  |  直到 7 年前
        1
  •  4
  •   Sergey Kalinichenko    7 年前

    Payload ,正好设置为 RABBIT

    你的 buildPayload payload 设置为共享实例:

    Payload payload = Payload.INSTANCE;
    

    同时,当你经过的时候 null 有效载荷至 AjaxRequestBinder 构造函数,构造函数使用相同的 Payload.INSTANCE

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

    你可以通过制作 构造函数public并在中创建新实例 有效载荷 用于以下情况: 提供给 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();
    

    构建有效负载

    推荐文章