黑马程序员技术交流社区

标题: 对象数组 [打印本页]

作者: 安静的喝会奶    时间: 2016-6-15 09:39
标题: 对象数组
class  Arrobject
{
        public static void main(String[] args)
        {
        Student[] students = new Student[4];
            students[0] = new Student("lin",24);
            students[1] = new Student("wu",24);
            students[2] = new Student("huang",21);
            students[3] = new Student("zhou",21);
            System.out.println("姓名"+"\t\t"+"年龄");
            getmassage(students);
        }
        public static void getmassage(Student[] students)
        {
           for(int i=0;i<students.length;i++)
           {
                Student s = students[i];
                int age = s.getAge();
                String name = s.getName();
                System.out.println(name+"\t\t"+age);
           }
        }
}
class Student
{
    private int age;
    private String name;
    public Student(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    public int getAge()
    {
        return age;
    }
    public String getName()
    {
        return name;
    }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2