在Java1.5中创建可变参数[Varargs]
2008-02-23 08:01:40来源:互联网 阅读 ()
方法重载是Java和其他面向对象语言最具特色的特性之一。当许多人可能认为Java的优势是它的类型,或者是它所带的API库,其实让相同的方法名与各种各样可接受的参数搭配也是一件很好的事。
Guitar guitar = new Guitar("Bourgeois", "Country Boy Deluxe",
GuitarWood.MAHOGANY, GuitarWood.ADIRONDACK,1.718);
Guitar guitar = new Guitar("Martin", "HD-28");
Guitar guitar = new Guitar("Collings", "CW-28"
GuitarWood.BRAZILIAN_ROSEWOOD, GuitarWood.ADIRONDACK,1.718,
GuitarInlay.NO_INLAY, GuitarInlay.NO_INLAY);
This code calls three versions of the constructor of a (fictional) Guitar class, meaning that information can be supplied when it’s available,rather than forcing a user to know everything about their guitar at one time (many professionals couldn’t tell you their guitar’s width at the nut).
Here are the constructors used:
public Guitar(String builder, String model) {
}
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,
float nutWidth) {
}
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,
float nutWidth,
GuitarInlay fretboardInlay, GuitarInlay topInlay) {
}
这段代码调用了Guitar类中三个版本的构造器,意味着当信息可见时,这些信息会被支持,而不是迫使每一个使用者每一次都要去了解关于Guitar类的所有知识。许多专家不会在关键时候告诉你他们的Guitar的内容。下面是用到的构造器:
public Guitar(String builder, String model) {
}
public Guitar(String builder, String model,GuitarWood backSidesWood, GuitarWood topWood,float nutWidth) {
}
public Guitar(String builder, String model,GuitarWood backSidesWood, GuitarWood topWood,float nutWidth,
GuitarInlay fretboardInlay, GuitarInlay topInlay) {
}
然而,当你想要去增加无限的信息时,事情开始变得有一点不是那么有用了。例如:假设你想允许在这个构造器中增加额外的未指明的特性。下面就是一些可能的调用的例子:
Guitar guitar = new Guitar("Collings", "CW-28"
GuitarWood.BRAZILIAN_ROSEWOOD, GuitarWood.ADIRONDACK,1.718,
GuitarInlay.NO_INLAY, GuitarInlay.NO_INLAY,"Enlarged Soundhole", "No Popsicle Brace");
Guitar guitar = new Guitar("Martin", "HD-28V","Hot-rodded by Dan Lashbrook", "Fossil Ivory Nut","Fossil Ivory Saddle", "Low-profile bridge pins");
对于这两个单独的情况,你不得不去增加一个构造器来接受两个额外的字符串,另外一个构造器来接受四个额外的字符串。试图将这些相似的版本应用于早已重载的构造器。根据这样的话,你最终会得到20或30个那样愚蠢的构造器的版本!
原因在于我们常称做的可变参数。可变参数是Tiger的增加的另一个特性,它用一种相当巧妙的方法彻底地解决了这儿提出的问题。这一章讲述了这种相对简单的特性的各个方面。这将会使你迅速写出更好、更整洁、更灵活的代码。
创建一个可变长度的参数列表
可变参数使得你可以指定某方法来接受多个同一类型的参数,而且并不要求事先确定参数的数量(在编译或运行时)。
这就是Tiger的一个集成部分。事实上,正是因为Java语言的一些新特性组合在一起才表现出了可变参数的特性。
我如何去实现呢?
首先,你要习惯的书写省略号(……)。这三个小点是可变参数的关键,你将会经常键入它们。下面是Guitar类的构造器使用可变参数来接受不确定数量字符串的一个例子:
public Guitar(String builder, String model, String……features);
参数String…… features 表明任何数量的字符串都可能被接受。 所以,下面所有的调用都合法的。
Guitar guitar = new Guitar("Martin", "HD-28V","Hot-rodded by Dan Lashbrook", "Fossil Ivory Nut","Fossil Ivory Saddle", "Low-profile bridge pins");
Guitar guitar = new Guitar("Bourgeois", "OMC","Incredible flamed maple bindings on this one.");
Guitar guitar = new Guitar("Collings", "OM-42","Once owned by Steve Kaufman--one of a kind");
You could add the same variable-length argument to the other constructors:
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,float nutWidth, String... features)
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,float nutWidth,
GuitarInlay fretboardInlay,GuitarInlay topInlay,String... features)
例5-1描写了一个把所有的这些特性放在一起的简单类,甚至使用XX来一起传递一些可变参数。
Example 5-1. Using varargs in constructors
package com.oreilly.tiger.ch05;
public class Guitar {
private String builder;
private String model;
private float nutWidth;
private GuitarWood backSidesWood;
private GuitarWood topWood;
private GuitarInlay fretboardInlay;
private GuitarInlay topInlay;
private static final float DEFAULT_NUT_WIDTH = 1.6875f;
public Guitar(String builder, String model, String... features) {
this(builder, model, null, null, DEFAULT_NUT_WIDTH, null, null, features);
}
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,
float nutWidth, String... features) {
this(builder, model, backSidesWood, topWood, nutWidth, null, null, features);
}
public Guitar(String builder, String model,
GuitarWood backSidesWood, GuitarWood topWood,float nutWidth,
GuitarInlay fretboardInlay, GuitarInlay topInlay,String... features) {
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash