可变个数的形参
package com.wdl.day09;/** 可變個數(shù)形參的方法** 1.jdk 5.0新增的內(nèi)容* 2.具體使用:* 2.1 可變個數(shù)形參的格式:數(shù)據(jù)類型 ... 變量名* 2.2 當調(diào)用可變個數(shù)形參的方法時,傳入的參數(shù)個數(shù)可以是:0個,1個,2個,。。。* 2.3 可變個數(shù)形參的方法與本類中方法名相同,形參不同的方法之間構(gòu)成重載* 2.4 可變個數(shù)形參的方法與本類中方法名相同,形參類型也相同的數(shù)組之間不構(gòu)成重載。換句話說,二者不能共存。* 2.5 可變個數(shù)形參在方法的形參中,必須聲明在末尾* 2.6 可變個數(shù)形參在方法的形參中,最多只能聲明一個可變形參。**/
public class MethodArgsTest {public static void main(String[] args) {MethodArgsTest test = new MethodArgsTest();test.show(12);
// test.show("hello");
// test.show("hello","world");
// test.show();test.show(new String[]{"AA","BB","CC"});}public void show(int i){}public void show(String s){System.out.println("show(String)");}public void show(String ... strs){System.out.println("show(String ... strs)");for(int i = 0;i < strs.length;i++){System.out.println(strs[i]);}}
// //不能與上一個方法同時存在
// public void show(String[] strs){
//
// }//The variable argument type String of the method//show must be the last parameter
// public void show(String ...strs,int i){
//
// }}
總結(jié)
- 上一篇: 匿名对象的使用
- 下一篇: 直接输出数组的名字不一定是地址值