posts - 19, comments - 1, trackbacks - 0, articles - 0
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Java1.5語(yǔ)言新特性

          Posted on 2006-10-09 23:05 xyang 閱讀(275) 評(píng)論(0)  編輯  收藏

          1.?自動(dòng)裝箱與拆箱?對(duì)應(yīng)C#
          ?例1.1
          ??Integer?i?=?10;
          ??int?j?=?i;
          ??
          2.?更優(yōu)化的for循環(huán)?對(duì)應(yīng)就C#---foreach循環(huán)
          ?例2.1
          ??String[]?names?=?{"BadBoy","GoodBoy","HappyGirl","sadGirl"};
          ??for(String?option:?names)?{
          ???System.out.println(option);
          ??}
          ?例2.2?加泛型?對(duì)應(yīng)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.參數(shù)可變的方法和printf
          ?例3.1
          ??定義:
          ??public?int?sum(int...?n)?{??//傳過(guò)來(lái)n為一個(gè)int型數(shù)組
          ???int?tempSum;
          ???for(int?option?:?n)?{
          ????tempSum+=option;
          ???}
          ???/*
          ???for(int?i?=?0;?i?<?n.length;?i++)?{
          ????tempSum+=n[i];
          ???}
          ???*/
          ???return?tempSum;
          ??}
          ??調(diào)用1:?sum(1);
          ??調(diào)用2:?sum(1,2);
          ??調(diào)用3:?sum(1,2,3,4);
          ?例3.2?printf方法,??對(duì)應(yīng)c語(yǔ)言的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語(yǔ)句里這樣寫case?MyColors.red:
          ?*這樣編譯器不會(huì)讓你通過(guò)*/
          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.靜態(tài)引用
          ?例5.1
          ??1.5版本以前的寫法是:
          ?
          ??  import?java.lang.Math;?//程序開(kāi)頭處
          ??
          ??  ...
          ??
          ??  double?x?=?Math.random();?
          ??1.5版本中可以這樣寫
          ???import?static?java.lang.Math.random;?//程序開(kāi)頭處
          ???
          ???...
          ??  
          ???double?x?=?random();??


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 胶州市| 德保县| 敦化市| 平邑县| 塔城市| 将乐县| 于田县| 黔西县| 汽车| 林甸县| 田阳县| 沂源县| 密云县| 会泽县| 庆云县| 颍上县| 介休市| 鹿邑县| 梨树县| 盘锦市| 普定县| 洛宁县| 贞丰县| 长宁区| 兴仁县| 静乐县| 新泰市| 平罗县| 唐海县| 景德镇市| 万年县| 射洪县| 新宾| 曲沃县| 科尔| 渭南市| 乐至县| 凤山县| 仁化县| 体育| 元谋县|