紫風亂寫

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

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          博友

          收藏的地址

          積分與排名

          • 積分 - 160229
          • 排名 - 380

          最新評論

          看了一下日歷,整整有一周沒有寫blog了。并不是因為自己不想寫了,而是因為很忙很忙的過了一周,估計接下來還有一段時間是沒空寫blog。昨天在和客戶討論完需求之后,偷空看了看Annotation。不過今天是在是沒有時間寫blog了。時間啊……

          posted @ 2005-09-22 23:50 Justfly Shi 閱讀(469) | 評論 (0)編輯 收藏

          一個大將被調去做外包了,一個大校去做論文了,兩個中尉還沒有成長起來。
          就我一個司令又要排兵布陣又要上場殺敵又要行政又要應付公文往來還要到處去救火
          為啥長春開發人員就這么難招呀,別說大將,連小兵都招不來合格的。
          命苦呀

          posted @ 2005-09-14 00:30 Justfly Shi 閱讀(1106) | 評論 (2)編輯 收藏

          范型有兩種,類范型和方法范型。他們可以用于一些類似于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的新特性 : 第三部分 :范型

          posted @ 2005-09-13 23:50 Justfly Shi 閱讀(1207) | 評論 (2)編輯 收藏

          Enum也是java中我比較喜歡的一個改進,雖然使用到的地方并不多。
          每一個enum類型都默認的繼承了java.lang.Enum虛擬類。
          每一個列舉實例都是改enum類型的一個實例。

          package cn.justfly.study.tiger.enums;

          /**
           * Sample code of enum
           * 
           * @author Justfly Shi created at 2005-9-12 23:59:59
           
          */
          public enum Gentle {
            WOMAN(
          ":)"), MAN(":|");
            Gentle(String hello) {
              _hello 
          = hello;
            }

            String _hello;

            String sayHello() {
              
          return _hello;
            }

            
          public static void main(String[] args) {
              System.
          out.println(Gentle.MAN.getDeclaringClass());

              Gentle[] allGentles 
          = Gentle.values();
              System.
          out.println("There are " + allGentles.length + " Gentles");
              
          for (Gentle g : allGentles) {
                System.
          out.println("index: " + g.ordinal() + " name: " + g.name()
                    
          + " HelloSmile: " + g.sayHello());
              }
            }
          }

          posted @ 2005-09-13 00:02 Justfly Shi 閱讀(820) | 評論 (0)編輯 收藏

          改進的for循環是Java 5.0中的一個讓我很喜歡的改進。它只對數組和實現了java.util.Iterable接口的容器類有效。
          package cn.justfly.study.tiger.enhancedfor;

          import java.util.ArrayList;
          import java.util.Collection;


          /**
           * The demo of enhanced for statement
           * it can only used for array and classes implements java.util.Iterable interface
           * @author Justfly Shi
           * created at 2005-8-28 21:42:12
           
          */
          public class EnhancedFor {

            
          /**
             * @param args
             
          */
            
          public static void main(String[] args) {
              
          //for array
              int[] intArray={1,2,3,4};
              System.
          out.println("printing ints:");
              
          for(int i:intArray){
                System.
          out.println(i);   
              }
              
              
          //for Collection
             Collection<String> list=new ArrayList<String>();
              
          for(int i=0;i<4;i++){
                list.add(
          "String"+i);
              }
              System.
          out.println("print Strings in an Collection:");
              
          for(String i:list){
                System.
          out.println(i);
              }
              
              
          //self-define Iterable
              MyIterable<String> myIte=new MyIterable<String>(list);
              System.
          out.println("print Stings in an Iterable");
              
          for(String i:myIte){
                System.
          out.println(i);
              }
              
            }

          }

          package cn.justfly.study.tiger.enhancedfor;

          import java.util.Collection;
          import java.util.Iterator;

          /**
           * an self-defined Iterable ,that can be used in enhanced-for statement 
           * @author Justfly Shi
           * created at 2005-8-28 22:09:05
           * @param 
           
          */
          public class MyIterable<G_E> implements Iterable<G_E> {
            
          private Collection<G_E> _list;

            
          public Iterator<G_E> iterator() {
              
          return new MyIterator<G_E>(_list.iterator());
            }

            
          public MyIterable(Collection<G_E> list) {
              _list 
          = list;
            }

            
          class MyIterator<G_I> implements Iterator<G_I> {
              
          private Iterator<G_I> _ite;

              MyIterator(Iterator
          <G_I> ite) {
                _ite 
          = ite;
              }

              
          public boolean hasNext() {
                
          return _ite.hasNext();
              }

              
          public G_I next() {
                
          return _ite.next();
              }

              
          public void remove() {
                _ite.remove();

              }
            }
          }

          輸出結果
          printing ints:
          1
          2
          3
          4
          print Strings in an Collection:
          String0
          String1
          String2
          String3
          print Stings in an Iterable
          String0
          String1
          String2
          String3

          posted @ 2005-09-12 23:44 Justfly Shi 閱讀(1652) | 評論 (0)編輯 收藏

          如下列代碼所示

          1、傳入的可變參數將做為一個數組處理
          2、一個函數中只能有一個可變參數列,并且只能在函數參數定義的最后。
          package cn.justfly.study.tiger;

          /**
           * Sample of using varargs
           * @author Justfly Shi
           * created at 2005-8-31 0:38:19
           
          */
          public class Varargs {
            
          public void printObjectArgs(Object objects ){
              
          for(Object o:objects)// objects is an Object Array
              {
                System.
          out.println(o);
              }
              System.
          out.println("var count:"+ objects.length);
            }
            
          public void printFloats(float fs ){
              
          for(float f:fs){
                System.
          out.println(f);
              }
              System.
          out.println("var count:"+ fs.length);
            }
          // only one param can be var args,and should be placed at the end of param list   
          //  public void printArgs(Integer integers,String objects  ){}

            
          /**
             * @param args
             
          */
            
          public static void main(String[] args) {
              Varargs vars
          =new Varargs();
              vars.printObjectArgs(
          new Integer(1),new Integer(2),3,"abc");
              vars.printFloats(
          1.2f,1.3f);
            }

          }
          輸出結果:
          1
          2
          3
          abc
          var count:4
          1.2
          1.3
          var count:2

          posted @ 2005-09-12 23:17 Justfly Shi 閱讀(831) | 評論 (0)編輯 收藏

          在java5中添加的一個新特性就是static import(靜態導入?)通過靜態導入我們可以很方便的使用在其他類中定義的函數。如下面的代碼所示,我們可以直接的使用java.lang.Math 的min和max以及其他在java.lang.Math中所定義的靜態方法,只需要在import中添加一句import static java.lang.Math.*。這是一個很方便的功能。

          package cn.justfly.study.tiger;

          import 
          static java.lang.Math.max;
          import 
          static java.lang.Math.min;
          /**
           * Sample of Static Import
           * @author Justfly Shi
           * created at 2005-9-3 23:41:50
           
          */

          public class StaticImport {

            
          /**
             * @param args
             
          */

            
          public static void main(String[] args) {
             
          /*
              * min() and max() are defined in java.lang.Math as static method.
              * but they can now be easily access.
              * 
          */

             
          int min=min(3,4);
             System.
          out.println("min is : "+min);
             
          int max=max(3,4);
             System.
          out.println("max is : "+max);

            }


          }


          但是這個功能卻不能濫用。因為它會導致代碼的可讀性變得很差。考慮一下一個未曾接觸過java.lang.Math類的讀者來看這段代碼。當他讀到“int min=min(3,4)”,他會很迷惑,這個min函數到底是在哪里定義的呢?于是他就得去分析import這里。這段代碼還好說,只有一個類被靜態導入,只需要打開 java.lang.Math的文檔就可以直接了解這些方法的相關信息了。但是如果同時靜態導入了10個類的情況下呢?如果這些類中有著名字類似(相同)但是行為卻不一致的方法的時候呢?比如Person.eat(Food food)和 Animal.eat(Food food)。

          那么這個功能該如何用呢?我認為一些常用的工具類、全局變量類等當需要在一個類中多次使用的時候可以導進來,但是對于系統中的模型類或者是用的次數不多的工具類還是不要導入的好。我們需要在自己寫代碼時的方便和代碼本身的可讀性間做個權衡。

          posted @ 2005-09-11 20:15 Justfly Shi 閱讀(1128) | 評論 (0)編輯 收藏

          要注意的是,當要被unboxing的封裝類為null的時候或未被初始化時會拋出一個nullpoint錯誤

          package cn.justfly.study.tiger;

          import java.util.ArrayList;
          import java.util.Collection;

          /**
           * Sample code of autoBoxing and auto-unboxing 
           * @author Justfly Shi
           * created at 2005-8-28 
           
          */

          public class Boxing {

            
          /**
             * @param args
             
          */

            
          public static void main(String[] args) {
              Collection
          <Integer> c = new ArrayList<Integer>();

              
          for (int i = 0; i < 5; i++{
                c.add(i);
          // autoboxing
              }

              System.
          out.println("iterate with Interger");
              
          for (Integer i : c) {

                System.
          out.println(i + 100);// unboxing
              }


              System.
          out.println("iterate with int");
              
          for (int i : c)// unboxing in enhanced for
              {

                System.
          out.println(i + 100);
              }

              
              Integer i
          =null;
              
          int j=i;//NullPointerException will be throw here
              System.out.println(j);
            }


          }

          posted @ 2005-09-11 02:07 Justfly Shi 閱讀(590) | 評論 (0)編輯 收藏

          小弟我新到貴地,請大家多多照顧。
          現有gmail邀請函一些,有需要者請留Email

          posted @ 2005-09-11 00:16 Justfly Shi 閱讀(450) | 評論 (0)編輯 收藏

          僅列出標題
          共6頁: 上一頁 1 2 3 4 5 6 
          主站蜘蛛池模板: 东乌珠穆沁旗| 卫辉市| 简阳市| 新昌县| 广汉市| 枞阳县| 阿合奇县| 邓州市| 吉木萨尔县| 绵竹市| 蓝田县| 迁西县| 康平县| 平远县| 宝鸡市| 鸡泽县| 巴林左旗| 枞阳县| 新巴尔虎右旗| 金坛市| 永清县| 冕宁县| 肇庆市| 张家港市| 安吉县| 达日县| 凤山县| 固安县| 神农架林区| 通辽市| 定日县| 呼玛县| 永仁县| 大港区| 林州市| 肇庆市| 武隆县| 当雄县| 房山区| 关岭| 朝阳区|