人在江湖

            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 人在江湖 閱讀(2259) 評論(1)  編輯  收藏 所屬分類: java

          Feedback

          # re: 發現thinking in java 4th edition的一個錯誤[未登錄] 2012-08-01 10:14 thomas
          看了這么多遍,太強了  回復  更多評論
            

          主站蜘蛛池模板: 弥勒县| 新疆| 连平县| 汤原县| 平泉县| 泸溪县| 瑞金市| 湄潭县| 秦安县| 灌南县| 郑州市| 宁化县| 白城市| 芜湖市| 故城县| 湟源县| 抚州市| 论坛| 洪江市| 晋江市| 香格里拉县| 馆陶县| 抚州市| 民乐县| 彰武县| 密山市| 札达县| 阿城市| 贡嘎县| 泽库县| 靖州| 莱芜市| 白河县| 乌拉特后旗| 抚州市| 泸州市| 米林县| 林芝县| 平昌县| 广元市| 通许县|