報(bào)錯(cuò)了,請(qǐng)大神們幫我看看哪里錯(cuò)了唄!
class?Person: ????def?__init__(self,name,gender):#2 ????????self.name=name ????????self.gender=gender class?Student(Person): ????def?__init__(self,name,gender,score):#2+1 ????????super(Student,?self).__init__(name,gender) ????????self.score=score class?Teacher(Person): ????def?__init__(self,name,gender,subject):#2+1 ????????super(Teacher,?self).__init__(name,?gender) ????????self.subject=subject class?SkillMixin: ????def?__init__(self,skill):#1 ????????self.skill=skill class?BasketballMixin(SkillMixin): ????def?__init__(self,skill,basketball):#1+1 ????????super(BasketballMixin,?self).__init__(skill) ????????self.basketball=basketball class?FootballMixin(SkillMixin): ????def?__init__(self,skill,football):#1+1 ????????super(FootballMixin,?self).__init__(skill) ????????self.football=football class?BasStudent(Student,BasketballMixin): ????def?__init__(self,name,gender,score,skill,basketball):#3+2 ????????super(BasStudent,?self).__init__(name,gender,score) ????????super(BasStudent,?self).__init__(skill,basketball) ????????print("我叫?%s,我會(huì)打%s?"%(self.name,self.basketball)) class?FootballTeacher(Teacher,FootballMixin): ????def?__init__(self,name,gender,subject,skill,football): ????????super(FootballTeacher,?self).__init__(name,gender,subject) ????????super(FootballTeacher,?self).__init__(skill,football) ????????print("我叫?%s,我會(huì)打%s?"%(self.name,self.football)) a=Student('jiji','male',93) b=Teacher('kiki','female','English') c=BasketballMixin('skill','籃球') d=FootballMixin('ski','足球') BasStudent(a.name,a.gender,a.score,c.skill,c.basketball) FootballTeacher(b.name,b.gender,b.subject,d.skill,d.football)
Traceback (most recent call last):
? File "/Users/lihui/IdeaProjects/laern/Python j進(jìn)階/3章 類的繼承.py", line 278, in <module>
? ? BasStudent(a.name,a.gender,a.score,c.skill,c.basketball)
? File "/Users/lihui/IdeaProjects/laern/Python j進(jìn)階/3章 類的繼承.py", line 266, in __init__
? ? super(BasStudent, self).__init__(skill,basketball)
TypeError: __init__() missing 1 required positional argument: 'score'
2022-04-06
你在BasStudent里面兩次調(diào)用了super方法,一廂情愿地認(rèn)為會(huì)分別調(diào)用兩個(gè)父類的init方法,但實(shí)際上不是,所以程序報(bào)錯(cuò)認(rèn)為缺參數(shù)(可能是兩次調(diào)用了同一個(gè)三參數(shù)的父類init方法)。具體原因我也沒搞清楚,不過網(wǎng)上的忠告:不惜一切代價(jià)地避免多重繼承,它帶來(lái)的麻煩比能解決的問題都多。如果你非要用,那你得準(zhǔn)備好專研類的層次結(jié)構(gòu),以及花時(shí)間去找各種東西的來(lái)龍去脈吧!
2022-03-31
score需要必填的呀