使用java基础写 存储学生信息:姓名、性别、班级、电话
时间: 2023-09-20 20:08:28 浏览: 88
可以使用Java中的类来存储学生信息,下面是一个示例:
```java
public class Student {
private String name;
private String gender;
private String className;
private String phoneNumber;
public Student(String name, String gender, String className, String phoneNumber) {
this.name = name;
this.gender = gender;
this.className = className;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getClassName() {
return className;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setClassName(String className) {
this.className = className;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
```
然后,在程序中可以创建一个学生对象并设置相应属性,如下所示:
```java
Student student = new Student("张三", "男", "一班", "1234567890");
```
如果需要存储多个学生信息,可以使用容器类如List或Map,将多个学生对象存储起来。例如,使用List:
```java
List<Student> students = new ArrayList<>();
students.add(new Student("张三", "男", "一班", "1234567890"));
students.add(new Student("李四", "女", "二班", "2345678901"));
students.add(new Student("王五", "男", "三班", "3456789012"));
```
这样就可以将多个学生信息存储在一个List中了。
阅读全文