标签:
1.可变参数列表的简单实现
package sample;
class A1{}
public class VarArgs {
static void printArray(Object[] args){
for(Object obj:args)
System.out.print(obj+" ");
System.out.println();
}
public static void main(String[] args){
printArray(new Object[]{
new Integer(47),new Float(3.14),new Double(11.11)
});
printArray(new Object[]{"one","two","three"});
printArray(new Object[]{new A1(),new A1(),new A1()});
}
}
|
package sample;
class A{}
public class NewVarArgs {
static void printArray(Object...args){
for(Object obj:args)
System.out.print(obj+" ");
System.out.println();
}
public static void main(String[] args){
printArray(new Integer(47),new Float(3.14),new Double(11.11));
printArray(47,
printArray("one","two","three");
printArray(new A(),new A(),new A());
printArray((Object[])new Integer[]{1,2,3,4});
printArray();
}
}
|
package sample;
public class OptionalArguments {
static void f(String...trailing){
for(String s:trailing)
System.out.print(s+" ");
System.out.println();
}
public static void main(String[] args){
f("one");
f("two","three");
f();
}
}
|
package sample;
public class AutoboxingVarargs {
public static void f(Integer...args){
for(Integer i:args)
System.out.print(i+" ");
System.out.println();
}
public static void main(String[] args){
f(new Integer(1),new Integer(2));
f(3,4,5,6,7,8,9);
f(10,new Integer(11),12);
}
}
|
package sample;
public class OverloadingVarargs {
static void f(Character...args){
System.out.print("first ");
for(Character c:args)
System.out.print(c+" ");
System.out.println();
}
static void f(Integer...args){
System.out.print("second ");
for(Integer i:args)
System.out.print(i+" ");
System.out.println();
}
static void f(Long...args){
System.out.print("third ");
for(Long l:args)
System.out.print(l+" ");
System.out.println();
}
static void f(Double...args){
System.out.print("forth ");
for(Double d:args)
System.out.print(d+" ");
System.out.println();
}
public static void main(String[] args){
f(‘a‘,‘b‘,‘c‘);
f(1);
f(2,1);
f(0.1);
f(
}
}
|
package sample;
public class OverloadingVarargs2 {
static void f(float i,Character...args){
System.out.println("first");
}
static void f(Character...args){
System.out.println("second");
}
public static void main(String[] args){
f(1,‘a‘);
f(‘a‘,‘b‘);//Error
}
}
|
package sample;
public class OverloadingVarargs2 {
static void f(float i,Character...args){
System.out.println("first");
}
static void f(char c,Character...args){
System.out.println("second");
}
public static void main(String[] args){
f(1,‘a‘);
f(‘a‘,‘b‘);
}
}
|
标签:
原文地址:http://www.cnblogs.com/xuecanmeng/p/5203399.html