Java1.5新功能可变长方法参数-varargs

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
<!-- 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"; //--> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9232855773311077&amp;dt=1110878202562&amp;format=336x280_as&amp;output=html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=10&amp;u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="65535"></iframe>

你可能感兴趣的:(jdk,html,Google)