人在江湖

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

thinking in java看了差不多有5,6遍,但是之前有些章节看得不仔细。最近有感于自己技术的不扎实,开始重看thinking in java, 发现了书中的一个错误,在原书的491页。

 

   1: //: generics/Wildcards.java
   2: // Exploring the meaning of wildcards.
   3:  
   4: public class Wildcards {
   5:    // Raw argument:
   6:   static void rawArgs(Holder holder, Object arg) {
   7:     // holder.set(arg); // Warning:
   8:     //   Unchecked call to set(T) as a
   9:     //   member of the raw type Holder
  10:     // holder.set(new Wildcards()); // Same warning
  11:  
  12:     // Can't do this; don't have any 'T':
  13:     // T t = holder.get();
  14:  
  15:     // OK, but type information has been lost:
  16:     Object obj = holder.get();
  17:   }    
  18:   // Similar to rawArgs(), but errors instead of warnings:
  19:   static void unboundedArg(Holder<?> holder, Object arg) {
  20:     // holder.set(arg); // Error:
  21:     //   set(capture of ?) in Holder<capture of ?>
  22:     //   cannot be applied to (Object)
  23:     // holder.set(new Wildcards()); // Same error
  24:  
  25:     // Can't do this; don't have any 'T':
  26:     // T t = holder.get();
  27:  
  28:     // OK, but type information has been lost:
  29:     Object obj = holder.get();
  30:   }    
  31:   static <T> T exact1(Holder<T> holder) {
  32:     T t = holder.get();
  33:     return t;
  34:   }
  35:   static <T> T exact2(Holder<T> holder, T arg) {
  36:     holder.set(arg);
  37:     T t = holder.get();
  38:     return t;
  39:   }
  40:   static <T>
  41:   T wildSubtype(Holder<? extends T> holder, T arg) {
  42:     // holder.set(arg); // Error:
  43:     //   set(capture of ? extends T) in
  44:     //   Holder<capture of ? extends T>
  45:     //   cannot be applied to (T)
  46:     T t = holder.get();
  47:     return t;
  48:   }    
  49:   static <T>
  50:   void wildSupertype(Holder<? super T> holder, T arg) {
  51:     holder.set(arg);
  52:     // T t = holder.get();  // Error:
  53:     //   Incompatible types: found Object, required T
  54:  
  55:     // OK, but type information has been lost:
  56:     Object obj = holder.get();
  57:   }
  58:   public static void main(String[] args) {
  59:     Holder raw = new Holder<Long>();
  60:     // Or:
  61:     raw = new Holder();
  62:     Holder<Long> qualified = new Holder<Long>();
  63:     Holder<?> unbounded = new Holder<Long>();
  64:     Holder<? extends Long> bounded = new Holder<Long>();
  65:     Long lng = 1L;
  66:  
  67:     rawArgs(raw, lng);
  68:     rawArgs(qualified, lng);
  69:     rawArgs(unbounded, lng);
  70:     rawArgs(bounded, lng);
  71:     
  72:     unboundedArg(raw, lng);
  73:     unboundedArg(qualified, lng);
  74:     unboundedArg(unbounded, lng);
  75:     unboundedArg(bounded, lng);
  76:  
  77:     // Object r1 = exact1(raw); // Warnings:
  78:     //   Unchecked conversion from Holder to Holder<T>
  79:     //   Unchecked method invocation: exact1(Holder<T>)
  80:     //   is applied to (Holder)
  81:     Long r2 = exact1(qualified);
  82:     Object r3 = exact1(unbounded); // Must return Object
  83:     Long r4 = exact1(bounded);
  84:     
  85:     // Long r5 = exact2(raw, lng); // Warnings:
  86:     //   Unchecked conversion from Holder to Holder<Long>
  87:     //   Unchecked method invocation: exact2(Holder<T>,T)
  88:     //   is applied to (Holder,Long)
  89:     Long r6 = exact2(qualified, lng);
  90:     // Long r7 = exact2(unbounded, lng); // Error:
  91:     //   exact2(Holder<T>,T) cannot be applied to
  92:     //   (Holder<capture of ?>,Long)
  93:     // Long r8 = exact2(bounded, lng); // Error:
  94:     //   exact2(Holder<T>,T) cannot be applied
  95:     //   to (Holder<capture of ? extends Long>,Long)
  96:     
  97:     // Long r9 = wildSubtype(raw, lng); // Warnings:
  98:     //   Unchecked conversion from Holder
  99:     //   to Holder<? extends Long>
 100:     //   Unchecked method invocation:
 101:     //   wildSubtype(Holder<? extends T>,T) is
 102:     //   applied to (Holder,Long)
 103:     Long r10 = wildSubtype(qualified, lng);
 104:     // OK, but can only return Object:
 105:     Object r11 = wildSubtype(unbounded, lng);
 106:     Long r12 = wildSubtype(bounded, lng);
 107:     
 108:     // wildSupertype(raw, lng); // Warnings:
 109:     //   Unchecked conversion from Holder
 110:     //   to Holder<? super Long>
 111:     //   Unchecked method invocation:
 112:     //   wildSupertype(Holder<? super T>,T)
 113:     //   is applied to (Holder,Long)
 114:     wildSupertype(qualified, lng);
 115:     // wildSupertype(unbounded, lng); // Error:
 116:     //   wildSupertype(Holder<? super T>,T) cannot be
 117:     //   applied to (Holder<capture of ?>,Long)
 118:     // wildSupertype(bounded, lng); // Error:
 119:     //   wildSupertype(Holder<? super T>,T) cannot be
 120:     //  applied to (Holder<capture of ? extends Long>,Long)
 121:   }
 122: } ///:~

看第104-105行:

// OK, but can only return Object:
Object r11 = wildSubtype(unbounded, lng);

Holder<?>是不能传给Holder<? extends T>的。

 

java的Generics限制和陷阱都很多,一本书1033页,generics这章占了近100页,和UI那章差不多长。陷阱越多的地方越是应该好好学学。这章不易理解,读起来很慢,啃了三分之二多了,还在继续啃。等啃完了有心情就总结一篇博客。

posted on 2012-07-30 10:30 人在江湖 阅读(2245) 评论(1)  编辑  收藏 所属分类: java

Feedback

# re: 发现thinking in java 4th edition的一个错误[未登录] 2012-08-01 10:14 thomas
看了这么多遍,太强了  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: