简体   繁体   English

为什么ToStringBuilder工作不一致?

[英]Why does ToStringBuilder work inconsistently?

In the following code, why do the two lines containing System.out.println(person); 在下面的代码中,为什么两行包含System.out.println(person); yield different outputs? 产生不同的输出? The second line indirectly calls the method Job.toString yielding the string "Manager" , but the first line mysteriously does not yielding Job@28f67ac7 . 第二行间接调用Job.toString方法,产生字符串"Manager" ,但是第一行神秘地不产生Job@28f67ac7 The line in between person.put("a", "b"); person.put("a", "b");之间的行person.put("a", "b"); doesn't seem to me like it should make any difference. 在我看来,这似乎没有什么不同。

Code: 码:

import java.util.*;
import org.apache.commons.lang3.builder.*;

class Job extends HashMap<String, String> {
    @Override public String toString() {
        return "Manager";
    }
}

class Person extends HashMap<String, String> {
    Job job;

    Person() {
        this.job = new Job();
    }

    @Override public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

class Test {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person);
        person.put("a", "b");
        System.out.println(person);
    }
}

Console: 安慰:

Person@2b80d80f[job=Job@28f67ac7,threshold=0,loadFactor=0.75]
Person@2b80d80f[job=Manager,threshold=12,loadFactor=0.75]

2 things contribute to the output changing: 有两点有助于改变输出:

  • ToStringBuilder avoids calling toString() on equal instances to avoid infinite recursions. ToStringBuilder避免在相等的实例上调用toString()以避免无限递归。
  • Your Person and Job classes inherit HashMap's equals() method, causing new Person().equals(new Job()) == true 您的Person和Job类继承HashMap的equals()方法,从而导致new Person().equals(new Job()) == true

This means while person and person.job in your example remain equal to each other, ToStringBuilder will not call person.job.toString(), but when the map contents change, person.job.toString() will be called. 这意味着在您的示例中person和person.job彼此相等时,ToStringBuilder不会调用person.job.toString(),但是当地图内容更改时,将调用person.job.toString()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个Java正则表达式不能用于从美国街道地址中删除街道号码? - Why does this Java regex work inconsistently for stripping street numbers out of US street addresses? 为什么此数组不一致地超出范围? - Why does this array go out of bounds inconsistently? 为什么Eclipse自动格式化块代码不一致? - why does eclipse autoformat blockcode inconsistently? 为什么此HTTP Servlet的行为不一致? - Why does this HTTP servlet behave inconsistently? ToStringBuilder.reflectionToString(Object)以什么格式显示日期? - In what format does ToStringBuilder.reflectionToString(Object) display dates? Selenium Webdriver的鼠标悬停功能不适用于Opera 39,并且与Chrome 53不一致 - Mouseover function with Selenium Webdriver does not work with Opera 39 and works inconsistently with Chrome 53 为什么 ToStringBuilder(ToStringStyle.JSON_STYLE) 在 object 前后添加引号? 是真的吗 - Why ToStringBuilder(ToStringStyle.JSON_STYLE) adding quotes before and after object? is it True 为什么此正则表达式不起作用? - why does this regex not work? 为什么LoadGraphs不起作用? - Why LoadGraphs does not work? 为什么这种方法不起作用? - Why does this method not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM