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) {
}
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");
public guitar(string builder, string model, 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)
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) {
this.builder = builder;
this.model = model;
this.backsideswood = backsideswood;
this.topwood = topwood;
this.nutwidth = nutwidth;
this.fretboardinlay = fretboardinlay;
this.topinlay = topinlay;
}
}
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)
guitar guitar = new guitar("martin", "d-18");
public guitar(string builder, string model)
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);
}
}
public static int max(int first, int... rest) {
int max = first;
for (int i : rest) {
if (i > max)
max = i;
}
return max;
}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 string[] features;
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) {
this.builder = builder;
this.model = model;
this.backsideswood = backsideswood;
this.topwood = topwood;
this.nutwidth = nutwidth;
this.fretboardinlay = fretboardinlay;
this.topinlay = topinlay;
this.features = features;
}
}
//变量声明
private list features;
//在方法中或是构造器中的书写
this.features = java.util.arrays.aslist(features);
public static int max(int first, int... rest) {
int max = first;
for (int i : rest) {
if (i > max)
max = i;
}
return max;
}int max = mathutils.max(1, 4);
int max = mathutils.max(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int max = mathutils.max(18, 8, 4, 2, 1, 0);
int[] numbers = getlistofnumbers( );
public static int max(int... values) {
int max = integer.min_value;
for (int i : values) {
if (i > max)
max = i;
}
return//用这种方法来取得数字
int[] numbers = getlistofnumbers( );
int max = mathutils.max(numbers);
package com.oreilly.tiger.ch05;
public class mathutils {
public static int max(int... values) {
if (values.length == 0) {
throw new illegalargumentexception("no values supplied.");
}
int max = integer.min_value;
for (int i : values) {
if (i > max)
max = i;
}
return max;
}
}
int max = mathutils.max(myarray);
int max = mathutils.max(new int[] { 2, 4, 6, 8 });
int max = mathutils.max(2, 4, 6, 8);
int max = mathutils.max(0);
int max = mathutils.max( );
private string print(object... values) {
stringbuilder sb = new stringbuilder( );
for (object o : values) {
sb.append(o)
.append(" ");
}
return sb.tostring( );
}private string print(string... values) {
stringbuilder sb = new stringbuilder( );
for (object o : values) {
sb.append(o)
.append(" ");
}
return sb.tostring( );
}private string print(object... values) {
stringbuilder sb = new stringbuilder( );
for (object o : values) {
sb.append(o)
.append(" ");
}
return sb.tostring( );
}system.out.printf("the balance of %s's account is $%(,6.2f\n",account.getowner().getfullname( ),account.getbalance( ));
printstream printf(string format, object... args)
printstream printf(string format, object[] args)
object[] objectarray = getobjectarrayfromsomewhereelse( );
out.printf("description of object array: %s\n", obj);
public object[] getobjectarrayfromsomewhereelse( ) {
return new string[] {"hello", "to", "all", "of", "you"};
}out.printf("description of object array: %s\n", obj);
run-ch05:
[echo] running chapter 5 examples from java tiger: a developer's notebook
[echo] running varargstester...
[java] hello
out.printf("description of object array: %s\n", new object[] { obj });out.printf("description of object array: %s\n", (object)obj);
run-ch05:
[echo] running chapter 5 examples from java tiger: a developer's notebook
[echo] running varargstester...
[java] [ljava.lang.string;@c44b88
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器