gpt4 book ai didi

java - 系统清除属性不起作用。怎么会这样?

转载 作者:搜寻专家 更新时间:2023-10-30 19:45:50 25 4
gpt4 key购买 nike

我真的不明白!

我运行包含 cod 的单元测试:

  String progDir = "prog.dir";
System.clearProperty(progDir);
System.out.println(System.getProperty(progDir));

在控制台上我看到了 prog 目录路径。虽然必须有null。

我在 setUp block 中设置这个变量。这是联合测试。这个变量需要所有其他测试但不是那个,所以我试图在这个测试方法的开始清理它。如果我从 setUp block 中删除此 var 的设置,则此测试将通过。

System.setProperty 工作正常。

怎么可能呢?谢谢

最佳答案

如果 Properties 对象中没有定义属性,则 getProperty 将在父 Properties 对象中查找

Properties javadoc说:

public String getProperty(String key)

Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.

clearProperty 调用 Hashtable.remove 因为 removeProperties 中没有被覆盖所以不会影响默认属性列表。

因此很可能通过 getProperty 清除的属性仍然可见,因为 System javadoc 没有指定系统 Properties 是否是分层或扁平。

关于java - 系统清除属性不起作用。怎么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16218410/

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