要編寫一個Student類,可以按照以下步驟進行:
以下是一個示例代碼:
public class Student {
private String name;
private int age;
private String studentId;
public Student(String name, int age, String studentId) {
this.name = name;
this.age = age;
this.studentId = studentId;
}
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 String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public void printInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Student ID: " + studentId);
}
public static void main(String[] args) {
Student student = new Student("John Doe", 20, "123456789");
student.printInfo();
}
}
上述代碼定義了一個Student類,具有姓名、年齡和學號等屬性。還提供了相應的訪問方法和一個打印學生信息的方法。在main方法中創建了一個Student對象,并調用printInfo方法打印學生信息。