标签:方法 tar 成员变量 array this nta public 赋值 nts
//把5个学生的信息放到数组中,遍历出每个学生的信息
第一部分
//创建学生类
public class Student {
//成员变量
private String name;
private int age;
//构造方法
public Student(){
super();
}
public Student(String name,int age){
this.name = name;
this.age = 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;
}
@Override
public String toString() {
return "Student{" +
"name=‘" + name + ‘\‘‘ +
", age=" + age +
‘}‘;
}
}
第二部分
public class StudentArray {
public static void main(String[] args){
//创建学生数组(对象数组)
Student[] students = new Student[5];
//创建5个对象并赋值
Student s1 = new Student("张玲玉", 50);
Student s2 = new Student("蒋介石", 35);
Student s3 = new Student("张无忌", 60);
Student s4 = new Student("张国荣", 45);
Student s5 = new Student("张大大", 30);
//把对象添加到数组中去
students[0]=s1;
students[1]=s2;
students[2]=s3;
students[3]=s4;
students[4]=s5;
//遍历输出
for(int x=0;x<students.length;x++){
//System.out.println(students[x]);
Student s = students[x];
System.out.println(s.getName()+"--------"+s.getAge());
}
}
}
标签:方法 tar 成员变量 array this nta public 赋值 nts
原文地址:https://www.cnblogs.com/WTBK/p/9400364.html