这种问题看JDK中的 Properties 类 的源代码就OK了:
...............
public class Properties extends Hashtable<Object,Object> {
.................
/**
* Calls the <tt>Hashtable</tt> method <code>put</code>. Provided for
* parallelism with the <tt>getProperty</tt> method. Enforces use of
* strings for property keys and values. The value returned is the
* result of the <tt>Hashtable</tt> call to <code>put</code>.
*
* @param key the key to be placed into this property list.
* @param value the value corresponding to <tt>key</tt>.
* @return the previous value of the specified key in this property
* list, or <code>null</code> if it did not have one.
* @see #getProperty
* @since 1.2
*/
public synchronized Object setProperty(String key, String value) {
return put(key, value);
}
...................
/**
* 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
* <code>null</code> if the property is not found.
*
* @param key the property key.
* @return the value in this property list with the specified key value.
* @see #setProperty
* @see #defaults
*/
public String getProperty(String key) {
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}
所以:实际上在向Properties对象中存储数据时调用用了Hashtable类的put(key, value)方法
而从Properties对象中取值时也是调用了Hashtable类的get(key)方法
之所以将Properties类的setProperty()方法的参数强制设置成String类型,要从设计Properties类的初衷说起:
Properties类的使命是读取存储在某些文件中的配置信息或资源
|