class Demo
{
public static void main(String[] args)
{
Student stu1=new Student("张三",20);//依据Student类的构造函数创建学生对象
Student stu2=new Student("李四",21);
Student stu3=new Student("王五",23);
Student stu4=new Student("赵六",19);
Student stu5=new Student("孙七",22);
Student [] stu={stu1,stu2,stu3,stu4,stu5};//创建一个含学生姓名、年龄的数组
System.out.println("学生信息"+":");
for(int n=0;n<stu.length;n++)
{
System.out.println(stu[n].getname()+","+stu[n].getage());}
}
}
class Student
{
private String name;
private int age;
Student(String name,int age)//构造函数给对象初始化
{
this.name=name;
this.age=age;
}
public String getname()//获取姓名
{
return name;
}
public int getage()//获取年龄
{
return age;
}
} |