请改进上面的代码,我希望用Student类来管理属性,同时属性的变量名依次为sno,sname,class,college,phone,gardon,floor,room
时间: 2024-06-09 18:08:43 浏览: 64
创建学生类Student,继承自SchoolPerson类, 添加额外两个属性:班级class、学号sno。 添加方法:打印学生的所有信息print_info方法。
5星 · 资源好评率100%
好的,以下是改进后的代码:
```python
class Student:
def __init__(self, sno, sname, class_name, college, phone, garden, floor, room):
self.sno = sno
self.sname = sname
self.class_name = class_name
self.college = college
self.phone = phone
self.garden = garden
self.floor = floor
self.room = room
def display_info(self):
print("学号:", self.sno)
print("姓名:", self.sname)
print("班级:", self.class_name)
print("学院:", self.college)
print("电话:", self.phone)
print("公寓:", self.garden)
print("楼层:", self.floor)
print("房间号:", self.room)
def change_phone(self, new_phone):
self.phone = new_phone
def change_room(self, new_garden, new_floor, new_room):
self.garden = new_garden
self.floor = new_floor
self.room = new_room
```
使用示例:
```python
# 创建一个学生对象
s = Student("001", "张三", "计算机科学与技术1班", "计算机学院", "18888888888", "东区", "10", "101")
# 显示学生信息
s.display_info()
# 修改电话号码
s.change_phone("16666666666")
s.display_info()
# 修改住宿信息
s.change_room("西区", "5", "501")
s.display_info()
```
阅读全文