public class ExerTest2 {
public static void main(String[] args) {
Student5[] stus = new Student5[20];
for(int i = 0;i < stus.length;i++) {
//给数组元素赋值
stus[i] = new Student5();
System.out.println(stus[i]);
}
}
}
class Student5{
int number;
int state;
int score;
public String info() {
return"学号" + number +",年级"+ state + ",成绩" + score;
}
}
stus[i] = new Student5();
这句代码执行后,就会分配地址空间,所以后面输出的是对应地址的值。
如果没有创建对象,就直接输出的话,输出的就是空null 了。
new Student5[20]分配了5个学生信息地址。用new Student5产生学生对象
你不是在for循环里new了吗?
你这不是赋值了吗
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!