“get"与“getProperty"之间的区别 [英] Difference between "get' VS "getProperty"

查看:66
本文介绍了“get"与“getProperty"之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 of an Object? Why is it not returning 'steel' when using get?

推荐答案

get继承自Hashtable,并声明返回Object.

getPropertyProperties 引入,声明返回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 why get is returning null.
  • myProp does contain an entry for "material", so when you call prop1.getProperty("material"), it will find that it doesn't have it directly, and check in myProp instead, and find "steel" there.

这篇关于“get"与“getProperty"之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆