b47617

          攀登!進步!
          隨筆 - 20, 文章 - 1, 評論 - 0, 引用 - 0
          數據加載中……

          轉 Java1.5語言新特性簡單總結

          1.?自動裝箱與拆箱?對應C#
          ?例1.1
          ??Integer?i?=?10;
          ??int?j?=?i;
          ??
          2.?更優化的for循環?對應就C#---foreach循環
          ?例2.1
          ??String[]?names?=?{"BadBoy","GoodBoy","HappyGirl","sadGirl"};
          ??for(String?option:?names)?{
          ???System.out.println(option);
          ??}
          ?例2.2?加泛型?對應C++模板
          ??import?java.util.*;
          ??
          ??ArrayList<String>?animals?=?new?ArrayList<String>();
          ??animals.add("Dog");
          ??animals.add("Cat");
          ??animals.add("Chick");
          ??animals.add("Cow");
          ??for(String?option?:?animals)?{
          ???System.out.println(option);
          ??}
          ??
          3.參數可變的方法和printf
          ?例3.1
          ??定義:
          ??public?int?sum(int...?n)?{??//傳過來n為一個int型數組
          ???int?tempSum;
          ???for(int?option?:?n)?{
          ????tempSum+=option;
          ???}
          ???/*
          ???for(int?i?=?0;?i?<?n.length;?i++)?{
          ????tempSum+=n[i];
          ???}
          ???*/
          ???return?tempSum;
          ??}
          ??調用1:?sum(1);
          ??調用2:?sum(1,2);
          ??調用3:?sum(1,2,3,4);
          ?例3.2?printf方法,??對應c語言的printf
          ??int?x?=?10;
          ??int?y?=?20;
          ??int?sum?=?x?+?y;
          ??System.out.printf("%d?+?%d?=?%d",x,y,sum);
          4.?枚舉
          ?例4.1
          ??public?enum?MyColors?{
          ???red,
          ???black,
          ???blue,
          ???green,
          ???yellow
          ??}
          ??
          ??MyColors?color?=?MyColors.red;
          ??for(MyColors?option?:?color.values())?{
          ???System.out.println(option);
          ??}

          /**不能在switch語句里這樣寫case?MyColors.red:
          ?*這樣編譯器不會讓你通過*/
          switch(color)?{
          ?case?red:
          ??System.out.println("best?color?is?"+red);
          ??break;
          ?case?black:
          ??System.out.println("NO?"?+?black);
          ??break;
          ?default:
          ??System.out.println("What");
          ??break;
          }

          5.靜態引用
          ?例5.1
          ??1.5版本以前的寫法是:
          ?
          ??  import?java.lang.Math;?//程序開頭處
          ??
          ??  ...
          ??
          ??  double?x?=?Math.random();?
          ??1.5版本中可以這樣寫
          ???import?static?java.lang.Math.random;?//程序開頭處
          ???
          ???...
          ??  
          ???double?x?=?random();??

          posted on 2006-03-27 00:19 原語 閱讀(175) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 盐城市| 阿拉善左旗| 旌德县| 桐柏县| 二连浩特市| 于田县| 时尚| 思南县| 景泰县| 庄浪县| 广宁县| 静安区| 南丰县| 安塞县| 广昌县| 余庆县| 禄劝| 龙口市| 文成县| 都江堰市| 沈丘县| 祥云县| 宁河县| 河曲县| 舒城县| 禹州市| 婺源县| 象山县| 安远县| 南康市| 闽侯县| 吕梁市| 宕昌县| 乐安县| 永年县| 永顺县| 兰坪| 明水县| 涿州市| 枣阳市| 张家口市|