This is the Student class
package com.ma_0002; /** * Create a student class and override the toString() method * @author TP * */ public class Student { /************Member variable area****************/ /**student name*/ String name; /**Student age*/ int age; /************Construction method area****************/ public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } /************get and set method area ****************/ 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; } /************Rewritten toString() method ****************/ @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
This is the test class StudentTest
package com.ma_0002; /** * Student test class * @author TP * */ public class StudentTest { /* * need: * Create several Student diagonals, put them into an array, and output * * analyze: * 1. Create an object with the Student constructor * 2. Create a Student[] array to put objects * 3. Output */ public static void main(String[] args) { //Create a student class object Student s = new Student("张大",1); Student s2 = new Student("张二",2); Student s3 = new Student("张三",3); Student s4 = new Student("Zhang Si",4); Student s5 = new Student("Zhang Wu",5); Student s6 = new Student("张六",6); //Create a Student[] array to hold the object Student[] stuArr = new Student[6]; //assign the array stuArr [0] = s; stuArr [1] = s2; stuArr[2] = s3; stuArr [3] = s4; stuArr [4] = s5; stuArr [5] = s6; // loop through the output array elements for (int i = 0; i < stuArr.length; i++) { System.out.println("s"+i+" : "+stuArr[i]); } } }
Results of the:
s0 : Student [name=Zhang Da, age=1]
s1 : Student [name=Zhang Er, age=2]
s2 : Student [name=Zhang San, age=3]
s3 : Student [name=Zhang Si, age=4 ]
s4 : Student [name=Zhang Wu, age=5]
s5 : Student [name=Zhang Liu, age=6]