4.定义一个表示学生的类student,包括域:学号、姓名、性别、年龄;方法:获得学号、姓名、性别、年龄;修改年龄,并书写Java程序创建student类的对象及测试其方法的功能。
时间: 2023-12-22 19:54:45 浏览: 33
以下是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
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)