#2题 定义OtherAthlete类为Athlete类的子类,重写top3方法(允许重复的时间) (5分)#代码1,定义OtherAthlete类继承AthleteclassOtherAthlete(Athlete):def__init__(self,a_name,a_bod,a_squat,a_times):
Athlete.__init__(self, a_name,a_bod,a_times)
self.squat=a_squat
deftop3(self):#代码2,定义无参数top3函数,对self.times属性应用统一化和排序功能returnsorted([self.sanitize(t)for t in self.times])[0:3]
第3题
defget_coach_data(filename):withopen(filename)as f:
line = f.readline()return line.strip().split(',')#3题 定义print_rugby函数,以多态的方式调用子类属性和方法 (5分)
loren = get_coach_data('mywork/loren.txt')
mark = get_coach_data('mywork/mark.txt')
loren = Rugby(loren.pop(0),loren.pop(0),loren.pop(0),loren)
mark = OtherAthlete(mark.pop(0),mark.pop(0),mark.pop(0),mark)defprint_rugby(athlete):print(athlete.name)#代码1,打印athlete的属性dob、squat和top3方法的返回值print(athlete.dob)print(athlete.squat)print(athlete.top3())#代码2,调用print_rugby函数,参数为loren
print_rugby(loren)#代码3,调用print_rugby函数,参数为mark
print_rugby(mark)