[java]代码库
package com.etc;
/*
* 有五个学生,请把这5个学生的信息存到数组中,并遍历数组,获得每个学生的信息。
* 学生:Student
* 成员变量:name,age
* 构造方法:无参,带参
* 成员方法:getXxx()/setXxx()
*
* 分析:
* A:创建学生类
* B:创建学生数组
* C:创建5个学生对象,并赋值
* D:把五个学生对象放到数组中,
* E:遍历学生数组。
*
*/
public class Test4{
public static void main(String[] args) {
//创建数组
Student [] arr = new Student[5];
//创建五个学生对象
Student s1 = new Student("林青霞",28);
Student s2 = new Student("风清扬",29);
Student s3 = new Student("赵雅芝",30);
Student s4 = new Student("孙允珠",25);
Student s5 = new Student("黄婷婷",24);
//将对象赋给数组
arr[0] = s1;
arr[1] = s2;
arr[2] = s3;
arr[3] = s4;
arr[4] = s5;
for(int i=0;i<5;i++){
//System.out.println(arr[i]);
System.out.println(arr[i].getName()+","+arr[i].getAge());
}
}
}
class Student{
private String name;
private 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 Student(String name, int age) {
super();
this.name = name;
this.setAge(age);
}
}
初级程序员
by: 雨落冬夜 发表于:2017-04-19 12:02:26 顶(2) | 踩(1) 回复
bucuo
回复评论