Java定义一个Student类,其中包括学号、姓名、性别、出生年月等属性以及init(?)——初始化各属性、display(?)——显示各属性、modify(?)——修改姓名等方法。实现并测试这个类。
时间: 2024-01-13 15:04:55 浏览: 10
好的,这是一个关于Java编程语言的问题,我可以回答。以下是一个可能的实现:
```
public class Student {
private String number;
private String name;
private String gender;
private String birthday;
public void init(String number, String name, String gender, String birthday) {
this.number = number;
this.name = name;
this.gender = gender;
this.birthday = birthday;
}
public void display() {
System.out.println("学号:" + number);
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("出生年月:" + birthday);
}
public void modify(String newName) {
this.name = newName;
}
}
```
你可以在main方法中测试这个类的功能,例如:
```
public static void main(String[] args) {
Student student = new Student();
student.init("001", "张三", "男", "1990-01-01");
student.display();
student.modify("李四");
student.display();
}
```
这将输出:
```
学号:001
姓名:张三
性别:男
出生年月:1990-01-01
学号:001
姓名:李四
性别:男
出生年月:1990-01-01
```
希望这个回答对你有所帮助!