用for循环创建对象

以下代码Demo:


public class TestDemo {
public static void main(String[] args) {
//以创建5个student为例
int count = 6;//student数量+1
for (int i = 1; i < count; i++) {
STUDENT[] student = new STUDENT[count];
student[i] = new STUDENT();
student[i].setAge(i);
student[i].setName("学生"+i);
System.out.println(student[i]);
}
}
}


class STUDENT {
String name;
int 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;
}
public String toString() {
return "student[name=" + name + ",age=" + age + "]";
}
}


打印如下:

student[name=学生1,age=1]
student[name=学生2,age=2]
student[name=学生3,age=3]
student[name=学生4,age=4]
student[name=学生5,age=5]

没有看懂原理,希望有大神可以指点一二

原文地址:https://www.cnblogs.com/xinxin-ting/p/7007776.html