用java 定义一个表示学生的类Student,包括属性“学号”、“班号”、“姓名”、“性别”、“年龄”;方法“获得学号”、“获得班号”、 “获得姓名”、“获得性别”、 “获得年龄”、 “修改年龄”。在main()中创建Student类对象,测试自定义类。
时间: 2023-11-11 16:06:22 浏览: 167
JAVA学生类Student
好的,以下是用Java定义一个表示学生的类Student的代码:
```java
public class Student {
private String studentId;
private String classId;
private String name;
private String gender;
private int age;
public Student(String studentId, String classId, String name, String gender, int age) {
this.studentId = studentId;
this.classId = classId;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getStudentId() {
return studentId;
}
public String getClassId() {
return classId;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
在main()中创建Student类对象并测试:
```java
public static void main(String[] args) {
Student student = new Student("001", "ClassA", "Tom", "Male", 18);
System.out.println("Student ID: " + student.getStudentId());
System.out.println("Class ID: " + student.getClassId());
System.out.println("Name: " + student.getName());
System.out.println("Gender: " + student.getGender());
System.out.println("Age: " + student.getAge());
student.setAge(19);
System.out.println("New Age: " + student.getAge());
}
```
输出结果为:
```
Student ID: 001
Class ID: ClassA
Name: Tom
Gender: Male
Age: 18
New Age: 19
```
阅读全文