问题描述
Properties myProp = new Properties();
myProp.put("material", "steel");
Properties prop1 = new Properties(myProp);
System.out.println(prop1.get("material") + ", " + prop1.getProperty("material"));
// outputs "null, steel"
在返回以下条目/属性的意义上,它与 getProperty 不相似一个东西?为什么在使用 get 时不返回 'steel'?
Isn't get similar to getProperty in the sense that it returns the entries/attributes ofan Object? Why is it not returning 'steel' when using get?
推荐答案
get
继承自Hashtable
,并声明返回Object
.
getProperty
由Properties
引入,声明返回String
.
getProperty
is introduced by Properties
, and is declared to return String
.
请注意,getProperty
将查询您可以传递给 Properties
的构造函数的默认"属性;get
不会.但在大多数情况下,它们会返回相同的值.在您给出的示例中,您使用了默认的支持属性:
Note thatgetProperty
will consult the "defaults" properties which you can pass into the constructor for Properties
; get
won't. In most cases they'll return the same value though. In the example you've given, you are using a default backing properties:
prop1
不直接包含"material"
的条目,因此为什么get
返回 null.myProp
确实包含"material"
的条目,因此当您调用prop1.getProperty("material")
,它会发现它没有它,而是检查myProp
,并在那里找到"steel"
.
prop1
doesn't directly contain an entry for"material"
, hence whyget
is returning null.myProp
does contain an entry for"material"
, so when you callprop1.getProperty("material")
, it will find that it doesn't have it directly, and check inmyProp
instead, and find"steel"
there.
这篇关于“get"与“getProperty"之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!