要编写一个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方法打印学生信息。