??????Java???????????????
???????????? ???????[ 2015/4/2 11:44:55 ] ????????Java ???? ???
?????????????????????????????????????Σ? ????????????????????????????????Java ????API??????????????????????????????????????HashMap???絥????????????????????????棬???????????
?????????????????????????????????Java????????????????Joshua Bloch????Effective Java????????棬??29???н????????????????????????typesafe hetereogeneous container????
??????????????????????Щ??????????????????????????????????н???????????????????Щ???????????
???????Java???????????????
????????????????????????????ó????????????????????????????????????????????String???????HashMap????????????????????type safe???????????????????
????public class Context {
????private final Map<String??Object> values = new HashMap<>();
????public void put( String key?? Object value ) {
????values.put( key?? value );
????}
????public Object get( String key ) {
????return values.get( key );
????}
????[...]
????}
????????????????????????????????????Context ??
????Context context = new Context();
????Runnable runnable = ...
????context.put( "key"?? runnable );
????// several computation cycles later...
????Runnable value = ( Runnable )context.get( "key" );
??????????????????????????????6?????????????????down cast????????滻??????????????????????????ClassCastException??:
????Context context = new Context();
????Runnable runnable = ...
????context.put( "key"?? runnable );
????// several computation cycles later...
????Executor executor = ...
????context.put( "key"?? executor );
????// even more computation cycles later...
????Runnable value = ( Runnable )context.get( "key" ); // runtime problem
??????????????????????????????????????????????????????????????????????????С?
???????????????????ò???value??????key??????value?????а????????
?????????????????????????????????????У???????????????????????Context??????
????public class Context {
????private final <String?? Object> values = new HashMap<>();
????public <T> void put( String key?? T value?? Class<T> valueType ) {
????values.put( key?? value );
????}
????public <T> T get( String key?? Class<T> valueType ) {
????return ( T )values.get( key );
????}
????[...]
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11