即使不使用put(),对象的属性也会在Map中更改?

use*_*127 0 java dependency-injection inversion-of-control

嗨我从书中得到了代码:

public class Container {

    Map<String, Object> components;

    public Container() {
        components = new HashMap<String, Object>();

        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("components.properties"));
            for (Map.Entry entry : properties.entrySet()) {
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                processEntry(key, value);
            }
        } catch (Exception ex) {
            throw new RuntimeException();
        }

    }

    private void processEntry(String key, String value) throws Exception {
        String parts[] = key.split("\\.");

        if (parts.length == 1) {
            Object component = Class.forName(value).newInstance();
            components.put(parts[0], component);
        } else {
            Object component = components.get(parts[0]);
            Object reference = components.get(value);
            PropertyUtils.setProperty(component, parts[1], reference);
        }

    }

    public Object getComponent(String id) {
        return components.get(id);
    }

}
Run Code Online (Sandbox Code Playgroud)

我的问题是,就行了

PropertyUtils.setProperty(component, parts[1], reference);
Run Code Online (Sandbox Code Playgroud)

Map中对象的属性已更改.即使在更新属性之后,没有component.put()来更新地图内的对象,该对象也会更新.这是为什么?

Jon*_*eet 9

这是因为地图只包含对象的引用 - 而不是对象的副本.

当您更改对象中的某些内容时,无论是否通过地图中的引用,您都可以看到更改.

这与做(假设)完全相同:

StringBuilder first = new StringBuilder();
StringBuilder second = first;

first.append("Hello");
System.out.println(second); // Prints "Hello"
Run Code Online (Sandbox Code Playgroud)

这里既有first并且second是同一个参考StringBuilder对象...当你通过改变该对象的内容first.append(),当你通过看对象的变化仍然可见second在随后的行参考.