4.定义一个表示学生的类student,包括域:学号、姓名、性别、年龄;方法:获得学号、姓名、性别、年龄;修改年龄,并书写Java程序创建student类的对象及测试其方法的功能。
时间: 2023-12-22 13:54:45 浏览: 85
一个 Student类:包括姓名、学号、性别、专业、家庭地址。
以下是Java代码实现:
```java
public class Student {
private String studentId;
private String name;
private String gender;
private int age;
public Student(String studentId, String name, String gender, int age) {
this.studentId = studentId;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getStudentId() {
return studentId;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class TestStudent {
public static void main(String[] args) {
Student student = new Student("1001", "张三", "男", 18);
System.out.println("学生编号:" + student.getStudentId());
System.out.println("学生姓名:" + student.getName());
System.out.println("学生性别:" + student.getGender());
System.out.println("学生年龄:" + student.getAge());
student.setAge(19);
System.out.println("修改后的学生年龄:" + student.getAge());
}
}
```
输出结果为:
```
学生编号:1001
学生姓名:张三
学生性别:男
学生年龄:18
修改后的学生年龄:19
```
阅读全文