标题: 泛型有那么多的好处,为什么Map体系中的Properties没有泛型定义 [打印本页] 作者: 廖理 时间: 2012-5-8 11:10 标题: 泛型有那么多的好处,为什么Map体系中的Properties没有泛型定义 泛型有那么多的好处,为什么Properties没有定义泛型
Properties是HashTable的子类,为什么Properties没有定义泛型呢?他不定义泛型的原因是什么?他的键值都是String类型的,为了什么这样呢?作者: 隋营营 时间: 2012-5-8 11:34
这种问题看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;
}