demo1 对象数组,静态初始化
class Person {
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getInfo(){
return "姓名:" + this.name + ",年龄:" + this.age;
}
// setter,getter 略
}
public class HelloWorld {
public static void main(String[] args){
Person per[] = new Person[3];// 对象数组
per[0] = new Person("张三",10);
per[1] = new Person("李四",20);
per[2] = new Person("王武",30);
for(int x = 0;x < per.length;x++){
System.out.println(per[x].getInfo());
}
}
}
demo2 对象数组,动态初始化
class Person {
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getInfo(){
return "姓名:" + this.name + ",年龄:" + this.age;
}
// setter,getter 略
}
public class HelloWorld {
public static void main(String[] args){
Person per[] = new Person[] {
new Person("张三",10),new Person("李四",20),new Person("王武",30)};// 对象数组,动态初始化
for(int x = 0;x < per.length;x++){
System.out.println(per[x].getInfo());
}
}
}
标签:name,对象,age,6.10,per,Person,数组,new,String
From: https://www.cnblogs.com/pansidong/p/17458437.html