设计一个学生通讯录,可以添加、删除、修改里面的信息,信息分别为学号、姓名、密码、地址、联系电话
时间: 2023-12-22 22:20:28 浏览: 48
好的,这是一个基础的学生通讯录设计,主要使用Python语言,具体代码如下:
```python
Student:
def __init__(self, student_id, name, password, address, phone_number):
self.student_id = student_id
self.name = name
self.password = password
self.address = address
self.phone_number = phone_number
class StudentDirectory:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
print("学生信息已添加")
def delete_student(self, student_id):
for student in self.students:
if student.student_id == student_id:
self.students.remove(student)
print("学生信息已删除")
return
print("学生信息不存在")
def update_student(self, student_id, name, password, address, phone_number):
for student in self.students:
if student.student_id == student_id:
student.name = name
student.password = password
student.address = address
student.phone_number = phone_number
print("学生信息已更新")
return
print("学生信息不存在")
def display_students(self):
for student in self.students:
print("学号:", student.student_id)
print("姓名:", student.name)
print("密码:", student.password)
print("地址:", student.address)
print("联系电话:", student.phone_number)
print("")
# 测试代码
directory = StudentDirectory()
# 添加学生信息
student1 = Student("001", "张三", "123456", "北京市海淀区", "18888888888")
directory.add_student(student1)
student2 = Student("002", "李四", "654321", "上海市浦东区", "17777777777")
directory.add_student(student2)
# 显示学生信息
directory.display_students()
# 更新学生信息
directory.update_student("001", "王五", "111111", "广东省深圳市", "16666666666")
# 显示学生信息
directory.display_students()
# 删除学生信息
directory.delete_student("002")
# 显示学生信息
directory.display_students()
```
在这个示例中,我们定义了两个类:`Student`和`StudentDirectory`。`Student`表示一个学生信息,包含学号、姓名、密码、地址和联系电话等属性。`StudentDirectory`表示学生通讯录,包含一个学生列表并提供添加、删除、更新和显示学生信息的方法。
在测试代码中,我们创建了一个`StudentDirectory`对象,并添加了两个`Student`对象。然后,我们显示了学生信息,并更新了一个学生的信息。最后,我们删除了一个学生的信息,并再次显示了学生信息来验证删除操作是否成功。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)