.利用类的相关知识,编程实现如下功能: -定义类Student; -定义Student类的数据成员:学号、姓名、性别、专业、班级; -定义Student类的构造方法,实现对上述成员的赋值; -在Student类中定义所有数据成员的get和set方法。 -定义Student类的对象,测试上述函数功能。用idle写
时间: 2024-03-20 09:39:11 浏览: 85
好的,以下是实现代码:
```python
class Student:
def __init__(self, stu_id, name, gender, major, classroom):
self.stu_id = stu_id
self.name = name
self.gender = gender
self.major = major
self.classroom = classroom
def get_stu_id(self):
return self.stu_id
def set_stu_id(self, stu_id):
self.stu_id = stu_id
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def get_gender(self):
return self.gender
def set_gender(self, gender):
self.gender = gender
def get_major(self):
return self.major
def set_major(self, major):
self.major = major
def get_classroom(self):
return self.classroom
def set_classroom(self, classroom):
self.classroom = classroom
# 测试代码
s = Student('20210001', '张三', '男', '计算机科学与技术', '计科1班')
print(s.get_stu_id())
print(s.get_name())
print(s.get_gender())
print(s.get_major())
print(s.get_classroom())
s.set_classroom('计科2班')
print(s.get_classroom())
```
以上代码定义了一个名为`Student`的类,并在其中定义了学生对象的数据成员和相关的get和set方法。在测试代码中,我们创建了一个`Student`对象,并使用get方法获取了各个数据成员的值,然后使用set方法修改了班级,并再次使用get方法获取了修改后的班级值。
阅读全文
相关推荐
















