关于(基本类型/引用类型)... (变量名/对象名)的使用笔记
今天做程序发现一个问题,就是关于不定参数的形参传入方法中的方式,分别是testInt和testVoid两个方法:
package com.lgq.servlet; import java.util.ArrayList; import javax.print.attribute.IntegerSyntax; /** * * @author 作者 MichaelLee * * @version 创建时间:2019年5月6日 上午11:50:39 * * 类说明 * */ public class Hello { public static void main(String[] args) { String string = null; // System.out.println(string); string = "hello"; // System.out.println(string); ArrayList<Person> pList = new ArrayList<Person>(); pList.add(new Person("LI", 13)); // System.out.println(pList.toString()); pList.add(new Person("sun", 12)); // System.out.println(pList.toString()); Person per1 = new Person("LI", 13), // System.out.println(per1); per2 = new Person("sun", 15); // per2 = new Person("sun", 12); // System.out.println(per1.toString()); // System.out.println(per2.toString()); // System.out.println(per1); testVoid(per1, per2); testInt(1, 2, 3, 4); } private static void testInt(int... a) { // TODO Auto-generated method stub for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } //测试String... public static void testVoid(Person... a) { for (int i = 0; i < a.length; i++) { System.out.println(a[i].toString()); } } } class Person { String name; int age; Person() { } public Person(String name, int age) { // TODO Auto-generated constructor stub this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { // TODO Auto-generated method stub return "[" + getName() + "," + getAge() + "]"; } }