隨筆-72  評(píng)論-20  文章-0  trackbacks-1
          1.可變參數(shù)列表的簡(jiǎn)單實(shí)現(xiàn)
          當(dāng)調(diào)用方法時(shí),方法的參數(shù)個(gè)數(shù)或類(lèi)型未知時(shí),稱其為可變參數(shù)列表。在以前的Java代碼中,可以使用Object數(shù)組來(lái)實(shí)現(xiàn)這樣的功能。因?yàn)椋械念?lèi)都是直接或間接繼承于Object類(lèi)。
           
          VarArgs.java
          package sample;
           
          class A1{}
           
          public class VarArgs {
                 static void printArray(Object[] args){
                        for(Object obj:args)
                               System.out.print(obj+" ");
                        System.out.println();
                 }
           
                 public static void main(String[] args){
                        printArray(new Object[]{
                               new Integer(47),new Float(3.14),new Double(11.11) 
                        });
                        printArray(new Object[]{"one","two","three"});
                        printArray(new Object[]{new A1(),new A1(),new A1()});
                 }
          }
           
          結(jié)果:
          47 3.14 11.11
          one two three
          sample.A1@a90653 sample.A1@de6ced sample.A1@c17164
           
          這里printArray方法使用Object數(shù)組作為參數(shù),并使用foreach語(yǔ)法遍歷數(shù)組,打印每個(gè)對(duì)象。
           
          2.Java SE5實(shí)現(xiàn)可變參數(shù)列表
          同樣的方法,參數(shù)可以這樣定義,(Object…args),這樣達(dá)到同樣的效果。
           
          NewVarArgs.java
          package sample;
           
          class A{}
           
          public class NewVarArgs {
                 static void printArray(Object...args){
                        for(Object obj:args)
                               System.out.print(obj+" ");
                        System.out.println();
                 }
           
                 public static void main(String[] args){
                        printArray(new Integer(47),new Float(3.14),new Double(11.11));
                        printArray(47,
                        printArray("one","two","three");
                        printArray(new A(),new A(),new A());
                        printArray((Object[])new Integer[]{1,2,3,4});
                        printArray();
                 }
          }
           
          結(jié)果:
          47 3.14 11.11
          47 3.14 11.11
          one two three
          sample.A@a90653 sample.A@de6ced sample.A@c17164
          1 2 3 4
           
          這里沒(méi)有顯式的使用數(shù)組作為參數(shù),但編譯器實(shí)際上會(huì)為你填充數(shù)組。所以同樣可以使用foreach語(yǔ)法來(lái)遍歷之。
          注意倒數(shù)第二行,編譯器已經(jīng)發(fā)現(xiàn)它是一個(gè)數(shù)組,所以不會(huì)在其上執(zhí)行任何轉(zhuǎn)換。最后一行表明,可以傳遞零個(gè)參數(shù)。
          也可以指定所有可變參數(shù)的類(lèi)型,下面的程序指定所有可變參數(shù)都必須是String。
           
          OptionalArguments.java
          package sample;
           
          public class OptionalArguments {
                 static void f(String...trailing){
                        for(String s:trailing)
                               System.out.print(s+" ");
                        System.out.println();
                 }
           
                 public static void main(String[] args){
                        f("one");
                        f("two","three");
                        f();
                 }
          }
           
          結(jié)果:
          one
          two three
           
          可以在可變參數(shù)列表中同時(shí)使用原始類(lèi)型與包裝類(lèi)。
           
          AutoboxingVarargs.java
          package sample;
           
          public class AutoboxingVarargs {
                 public static void f(Integer...args){
                        for(Integer i:args)
                               System.out.print(i+" ");
                        System.out.println();
                 }
           
                 public static void main(String[] args){
                        f(new Integer(1),new Integer(2));
                        f(3,4,5,6,7,8,9);
                        f(10,new Integer(11),12);
                 }
          }
           
          結(jié)果:
          1 2
          3 4 5 6 7 8 9
          10 11 12
           
          3.可變參數(shù)列表的重載(Overloading)
          如果出現(xiàn)重載的情況,則編譯器會(huì)自動(dòng)調(diào)用最適合的方法匹配之。
           
          OverloadingVarargs.java
          package sample;
           
          public class OverloadingVarargs {
                 static void f(Character...args){
                        System.out.print("first ");
                        for(Character c:args)
                               System.out.print(c+" ");
                        System.out.println();
                 }
           
                 static void f(Integer...args){
                        System.out.print("second ");
                        for(Integer i:args)
                               System.out.print(i+" ");
                        System.out.println();
                 }
                
                 static void f(Long...args){
                        System.out.print("third ");
                        for(Long l:args)
                               System.out.print(l+" ");
                        System.out.println();
                 }
                
                 static void f(Double...args){
                        System.out.print("forth ");
                        for(Double d:args)
                               System.out.print(d+" ");
                        System.out.println();
                 }
                
                 public static void main(String[] args){
                        f('a','b','c');
                        f(1);
                        f(2,1);
                        f(0.1);
                        f(
                 }
          }
           
          結(jié)果:
          first a b c
          second 1
          second 2 1
          forth 0.1
          third 0
           
          但下面的情況是不允許的,即某個(gè)方法中增加一個(gè)非可變參數(shù)。
           
          OverloadingVarargs2.java
          package sample;
           
          public class OverloadingVarargs2 {
                 static void f(float i,Character...args){
                        System.out.println("first");
                 }
           
                 static void f(Character...args){
                        System.out.println("second");
                 }
                
                 public static void main(String[] args){
                        f(1,'a');
                        f('a','b');//Error
                 }
          }
           
          但可以這樣解決問(wèn)題。
          package sample;
           
          public class OverloadingVarargs2 {
                 static void f(float i,Character...args){
                        System.out.println("first");
                 }
           
                 static void f(char c,Character...args){
                        System.out.println("second");
                 }
                
                 public static void main(String[] args){
                        f(1,'a');
                        f('a','b');
                 }
          }
          即重載的方法必須保持一致的參數(shù)形式。

          posted on 2007-08-19 05:03 前方的路 閱讀(6785) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java技術(shù)
          主站蜘蛛池模板: 龙海市| 广德县| 巴东县| 乌兰县| 浦北县| 福贡县| 迁安市| 黔西| 桦甸市| 安阳县| 原阳县| 全椒县| 台州市| 阿荣旗| 阿鲁科尔沁旗| 贺兰县| 柘城县| 长春市| 商南县| 政和县| 商洛市| 临清市| 买车| 建宁县| 德江县| 天津市| 舒兰市| 灵宝市| 宜章县| 平武县| 太白县| 正宁县| 隆林| 海城市| 宝清县| 建平县| 遂昌县| 岱山县| 顺平县| 务川| 永定县|