b47617

          攀登!進(jìn)步!
          隨筆 - 20, 文章 - 1, 評(píng)論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          轉(zhuǎn) Java1.5語言新特性簡單總結(jié)

          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)?{??//傳過來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語言的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:
          ?*這樣編譯器不會(huì)讓你通過*/
          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;?//程序開頭處
          ??
          ??  ...
          ??
          ??  double?x?=?Math.random();?
          ??1.5版本中可以這樣寫
          ???import?static?java.lang.Math.random;?//程序開頭處
          ???
          ???...
          ??  
          ???double?x?=?random();??

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 汉寿县| 东乡族自治县| 太仆寺旗| 化隆| 五峰| 漯河市| 天镇县| 名山县| 伽师县| 武乡县| 宾阳县| 千阳县| 衡水市| 五原县| 广宗县| 鹰潭市| 淮南市| 卓资县| 达拉特旗| 磐石市| 集安市| 台中市| 札达县| 资兴市| 富锦市| 阿拉尔市| 中江县| 多伦县| 静宁县| 河源市| 图片| 尉氏县| 中西区| 上犹县| 冕宁县| 乌鲁木齐县| 全椒县| 剑川县| 航空| 东丽区| 新昌县|