/**
* 书本:《Thinking In Java》
* 功能:以Object数组作为参数
* 文件:VarArgs.java
* 时间:2014年10月6日20:04:18
* 作者:cutter_point
*/
package Lesson5InitializationAndCleanUp;
class A{}
public class VarArgs
{
static void printArray(Object [] args)
{
for(Object obj : args) //逐个输出args里面的对象
System.out.println(obj+" "); //foreach语法
System.out.println(); //换行
}
static void printArray2(Object... args) //这种可变参数列表
{
for(Object obj : args) //逐个输出args里面的对象
System.out.println(obj+" "); //foreach语法
System.out.println(); //换行
}
public static void main(String [] args)
{
printArray(new Object[]{new Integer(47), new Float(3.14), new Double(11.11)}); //里面是对匿名Object数组进行初始化
printArray2(new Object[]{new Integer(47), new Float(3.14), new Double(11.11)}); //里面是对匿名Object数组进行初始化
printArray2(47, 3.14F, 11.11);
printArray(new Object[]{"one", "two", "three"}); //字符初始化
printArray2("one", "two", "three"); //字符初始化
printArray(new Object[]{new A(), new A(), new A()});
}
}
输出结果:
47
3.14
11.11
47
3.14
11.11
47
3.14
11.11
one
two
three
one
two
three
Lesson5InitializationAndCleanUp.A@5fcbc39b
Lesson5InitializationAndCleanUp.A@3a97263f
Lesson5InitializationAndCleanUp.A@19501026
【ThinkingInJava】5、以Object数组作为参数
原文:http://blog.csdn.net/cutter_point/article/details/45029621