紫風亂寫

          除了他眼前的屏幕,這個人什么也沒看見。
          被周圍的電腦簇擁著,他只知道他所創造的現實,但又意識到那是虛幻。
          他已經超越了技術。也超越了機器。
          posts - 62, comments - 93, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java 5.0中的范型(Generics)

          Posted on 2005-09-13 23:50 Justfly Shi 閱讀(1205) 評論(2)  編輯  收藏 所屬分類: Study Tiger
          范型有兩種,類范型和方法范型。他們可以用于一些類似于C++中的模板之類的作用。
          這里主要有這么幾點要注意:
          0、范型類之間的轉型
          1、范型類的繼承
          2、范型方法的override
          3、關鍵字 super 和exends的使用。

          package cn.justfly.study.tiger.generics;

          import java.util.Collection;

          /**
           * Sample of defining generics type
           * 
           * @author Justfly Shi 
           * created at 2005-8-25 22:03:09
           
          */

          public class Defination<G_T, G_B> {
            
          private G_T _t = null;

            
          private G_B _b = null;

            
          public G_B getB() {
              
          return _b;
            }


            
          public void setB(G_B b) {
              _b 
          = b;
            }

            
            
          //Generics Method
            <G_A> G_A abc(G_A a,Defination<G_T, ? extends G_B> f)//keyword extends
            {
              
          return a;
            }


            
          //class Generics Method
            static <G_F,G_F2> void test(G_F[] a, Collection super G_F> b)// keyword super 
            {
              
          for (G_F o : a) {
                b.add(o);
              }

            }


            
          public G_T getT() {
              
          return _t;
            }


            
          public void setT(G_T t) {
              _t 
          = t;
            }


            
          public Defination(G_T t, G_B b) {
              super();
              _b 
          = b;
              _t 
          = t;
            }


            
          /**
             * @param args
             
          */

            
          public static void main(String[] args) {
              Defination
          <A, A> d = new Defination<A, A>(new AImp2(), new AImp1());
              printDefination(d);

              
          // about extends
              Defination<A, A> right = new SubDefination<A, A>(new AImp2(), new AImp1());
              printDefination(right);
              
          // Type mismatch: cannot convert from SubDefination// Defination// Defination// AImp1());

              
          // Type mismatch: cannot convert from Defination// Defination// Defination// AImp1());
            }


            
          private static void printDefination(Defination<A, A> defination) {
              A t 
          = defination.getT();
              A b 
          = defination.getB();
              System.
          out.println(t.getValue());
              System.
          out.println(b.getValue());
            }

          }


          class SubDefination<G_T, G_J> extends Defination<G_T, G_J> {

            
          public SubDefination(G_T t, G_J b) {
              super(t, b);
            }

            
            @Override
            
          <G_A> G_A abc(G_A a, Defination<G_T, ? extends G_J> f) {
              
          return super.abc(a, f);
            }

            
          }


          class A {
            String getValue() 
          {
              
          return "class A";
            }

          }


          class AImp1 extends A {
            
          public String getValue() {
              
          return "class AImp1";
            }

          }


          class AImp2 extends A {
            
          public String getValue() {
              
          return "class AImp2";
            }

          }

          最后再推薦一篇中文的范型學習資料
          在Eclipse 3.1中體驗J2SE 5.0的新特性 : 第三部分 :范型

          評論

          # re: Java 5.0中的范型(Generics)  回復  更多評論   

          2005-09-14 00:03 by Justfly Shi
          哼,我自己評一下:)

          # re: Java 5.0中的范型(Generics)  回復  更多評論   

          2005-11-05 14:21 by longaway
          不錯。

          俺最近才剛開始看 java 的范型。

          這邊的 bolg 平臺也挺不錯的說。
          代碼還帶折疊的。
          主站蜘蛛池模板: 察雅县| 惠水县| 天镇县| 嫩江县| 民勤县| 光泽县| 毕节市| 资中县| 邛崃市| 嘉义市| 连平县| 普陀区| 浙江省| 平武县| 辽中县| 安岳县| 扬州市| 白河县| 宜春市| 永年县| 长泰县| 宁津县| 阳泉市| 鄱阳县| 电白县| 类乌齐县| 屯昌县| 木里| 互助| 大名县| 交口县| 凤城市| 五大连池市| 休宁县| 伊川县| 海城市| 五河县| 凤城市| 蓬溪县| 舟山市| 永平县|