编写一个封装学生的类Student,能够描述学生的“学号”、“ 姓名”、“性别”、 “年龄”、“平均成绩”等基本属性,及获取属性、修改属性的方法和打印学生基本信息的 print()方法。要求生成两个学生对象,在构造方法中进行初始化,并打印每个学生的基本信 息。
时间: 2024-01-22 19:02:11 浏览: 93
class Student:
def __init__(self, id, name, sex, age, avg_score):
self.id = id
self.name = name
self.sex = sex
self.age = age
self.avg_score = avg_score
def get_id(self):
return self.id
def get_name(self):
return self.name
def get_sex(self):
return self.sex
def get_age(self):
return self.age
def get_avg_score(self):
return self.avg_score
def set_id(self, id):
self.id = id
def set_name(self, name):
self.name = name
def set_sex(self, sex):
self.sex = sex
def set_age(self, age):
self.age = age
def set_avg_score(self, avg_score):
self.avg_score = avg_score
def print_info(self):
print("学号:%s,姓名:%s,性别:%s,年龄:%d,平均成绩:%.2f" % (self.id, self.name, self.sex, self.age, self.avg_score))
stu1 = Student("001", "张三", "男", 20, 89.5)
stu2 = Student("002", "李四", "女", 19, 92.5)
stu1.print_info()
stu2.print_info()
阅读全文