java定义对象数组及其使用

定义一个student类,在主类中定义一个student数组stu[10];需要对每个对象stu[i]都new Student();

public class Student {
    int number;
}
public class Example4_21 {
    public static void main(String[] args){
        Student stu[] = new Student[10];
        for (int i = 0; i <stu.length ; i++){
            stu[i] = new Student();
            stu[i].number = 101+i;
        }
        for (int i = 0; i<stu.length ; i++)
        {
            System.out.print(" "+stu[i].number);
        }
    }
}
发布了29 篇原创文章 · 获赞 12 · 访问量 6882

猜你喜欢

转载自blog.csdn.net/Zheng_lan/article/details/102242452