Java1.5提供了一个叫varargs的新功能,就是可变长度的参数。
在以前的jdk版本中,对象中一个方法的入参个数在写好后就是固定的,而varargs提供了可变长度的功能,有点类似于main方法的参数String[] args,我们在命令行运行时args可数是可变的。
使用varargs写法是这样的 public Guitar(String builder, String model, String... features); 用省略号...声明features为可变长度的参数。
你申明如下方法
public Guitar(String builder, String model, String... features)
编译时将被解释为:
public Guitar(String builder, String model, String[] features)
但是他是有一些限制的.首先你在一个方法中只能使用一个省略号定义,也就是只能定义一个可变长的参数。 下面的定义是不合法的 public Guitar(String builder, String model,
String... features, float... stringHeights)
我们可以用下面的方法取得可变长参数:
public Guitar(String builder, String model,GuitarWood backSidesWood, GuitarWood topWood,
float nutWidth,GuitarInlay fretboardInlay, GuitarInlay topInlay,String... features) {
this.builder = builder;
this.model = model;
this.backSidesWood = backSidesWood;
this.topWood = topWood;
this.nutWidth = nutWidth;
this.fretboardInlay = fretboardInlay;
this.topInlay = topInlay;
for (String feature : features) {
System.out.println(feature);
}
}
我们也可以这样获取可变长参数值,把features直接赋给String[],或者其他集合类型
// Variable declaration
private List features;
// Assignment in method or constructor body
this.features = java.util.Arrays.asList(features);
以上内容摘自:
http://www.onjava.com/pub/a/onjava/excerpt/javaadn_chap5/index.html
http://www.onjava.com/catalog/javaadn/excerpt/javaadn_ch05.pdf
<script type="text/javascript"> <!-- google_ad_client = "pub-9232855773311077"; google_ad_width = 336; google_ad_height = 280; google_ad_format = "336x280_as"; google_ad_channel =""; google_color_border = "336699"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "008000"; google_color_text = "000000"; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9232855773311077&dt=1110878202562&format=336x280_as&output=html&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=336699&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=10&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="65535">
在以前的jdk版本中,对象中一个方法的入参个数在写好后就是固定的,而varargs提供了可变长度的功能,有点类似于main方法的参数String[] args,我们在命令行运行时args可数是可变的。
使用varargs写法是这样的 public Guitar(String builder, String model, String... features); 用省略号...声明features为可变长度的参数。
你申明如下方法
public Guitar(String builder, String model, String... features)
编译时将被解释为:
public Guitar(String builder, String model, String[] features)
但是他是有一些限制的.首先你在一个方法中只能使用一个省略号定义,也就是只能定义一个可变长的参数。 下面的定义是不合法的 public Guitar(String builder, String model,
String... features, float... stringHeights)
我们可以用下面的方法取得可变长参数:
public Guitar(String builder, String model,GuitarWood backSidesWood, GuitarWood topWood,
float nutWidth,GuitarInlay fretboardInlay, GuitarInlay topInlay,String... features) {
this.builder = builder;
this.model = model;
this.backSidesWood = backSidesWood;
this.topWood = topWood;
this.nutWidth = nutWidth;
this.fretboardInlay = fretboardInlay;
this.topInlay = topInlay;
for (String feature : features) {
System.out.println(feature);
}
}
我们也可以这样获取可变长参数值,把features直接赋给String[],或者其他集合类型
// Variable declaration
private List features;
// Assignment in method or constructor body
this.features = java.util.Arrays.asList(features);
以上内容摘自:
http://www.onjava.com/pub/a/onjava/excerpt/javaadn_chap5/index.html
http://www.onjava.com/catalog/javaadn/excerpt/javaadn_ch05.pdf
<script type="text/javascript"> <!-- google_ad_client = "pub-9232855773311077"; google_ad_width = 336; google_ad_height = 280; google_ad_format = "336x280_as"; google_ad_channel =""; google_color_border = "336699"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "008000"; google_color_text = "000000"; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9232855773311077&dt=1110878202562&format=336x280_as&output=html&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=008000&color_border=336699&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=10&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="65535">