|
public class ObjectArrayTest {
public static void main(String[] args)
{
PersonCls p[] = {new PersonCls("张三",15),new PersonCls("李四",30),new PersonCls("王五",35)} ;
for(int i=0;i<p.length;i++)
{
System.out.println(p[i].talk()) ;
}
//第二种迭代方法
for (PersonCls pinst:p) {
System.out.println(pinst.talk());
}
}
}
class PersonCls
{
String name ;
int age ;
public PersonCls()
{
}
public PersonCls(String name,int age)
{
this.name = name ;
this.age = age ;
}
public String talk()
{
return this.name + ",今年:"+this.age+"岁" ;
}
}
显示:
张三,今年:15岁
李四,今年:30岁
王五,今年:35岁
张三,今年:15岁
李四,今年:30岁
王五,今年:35岁
|
|