在Java中,類的數組可以通過以下步驟定義和使用:
public class Student {
private String name;
private int age;
// 構造函數
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// getter和setter方法
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Student[] students = new Student[3];
students[0] = new Student("Alice", 20);
students[1] = new Student("Bob", 21);
students[2] = new Student("Charlie", 22);
for (int i = 0; i < students.length; i++) {
System.out.println("Student " + (i+1) + ":");
System.out.println("Name: " + students[i].getName());
System.out.println("Age: " + students[i].getAge());
System.out.println();
}
以上是在Java中定義和使用類的數組的基本步驟。請根據實際需求修改類和數組的大小,并根據需要使用其他操作。