首页 > 解决方案 > 多重继承python问题

问题描述

所以我目前正在为我的一个类学习 python 的继承,并且任务让我们对 ScientificSwimmer 类使用多重继承。当您尝试运行代码而不创建所述类的对象时,程序运行。但是,在创建该类的对象时,出现以下错误。任何建议或解决方案将不胜感激。(帖子中添加的行号)

#creation of Human class
class Human:  

    def __init__(self, name, age): #talks a name and balance and creates a instance of class
        self.age = age
        self.name = name
    
    def hobby(self):#Hobby method
        print("Likes to watch Netflix")
        
    def info(self):#info method
        print(self.name, " is ", self.age, " years old.")
        

#creation of the Scientist class
class Scienctist(Human):
    
    def __init__(self, name, age, lab):
        super().__init__(name, age)
        self.lab = lab
        
    def hobby(self):
        print("Likes watching Bill Nye the science guy.")
        
    def labName(self):
        print("Works at the ", self.lab , "laboratory.")
        
#Creation of Swimmer Class
class Swimmer(Human):
    
    def __init__(self, name, age, hours):
(line 33)super().__init__(name, age)
        self.hours = hours
        
    def hobby(self):
        print("Likes to go swimming in the lake.")
        
    def SwimmingHours(self):
        print("Swims ", self.hours, "hours per week.")
        
#Creation of the ScientificSwimmer
class ScientificSwimmer(Scienctist, Swimmer):
    def __init__(self, name, age, lab, hours):
(line 58)Scienctist.__init__(self, name, age, lab)
        Swimmer.__init__(self, name, age, hours)

(line 66) person5 = ScientificSwimmer("John Smith", "30", "nuclear", "100")

错误:

File "E:\Python\CS112\lab7\People.py", line 66, in <module>
    person5 = ScientificSwimmer("John Smith", "30", "nuclear", "100")

  File "E:\Python\CS112\lab7\People.py", line 58, in __init__
    Scienctist.__init__(self, name, age, lab)

  File "E:\Python\CS112\lab7\People.py", line 33, in __init__
    super().__init__(name, age)

TypeError: __init__() missing 1 required positional argument: 'hours'

标签: pythonclassmultiple-inheritance

解决方案


我会更改类以为每个类添加一个 **kwargs 参数。这是处理不同参数的最简单方法。

#creation of Human class
class Human:

    def __init__(self, name, age, **kwargs): #talks a name and balance and creates a instance of class
        self.age = age
        self.name = name

    def hobby(self):#Hobby method
        print("Likes to watch Netflix")

    def info(self):#info method
        print(self.name, " is ", self.age, " years old.")


#creation of the Scientist class
class Scienctist(Human):

    def __init__(self, name, age, lab, **kwargs):
        super().__init__(name, age, **kwargs)
        self.lab = lab

    def hobby(self):
        print("Likes watching Bill Nye the science guy.")

    def labName(self):
        print("Works at the ", self.lab , "laboratory.")

#Creation of Swimmer Class
class Swimmer(Human):

    def __init__(self, name, age, hours, **kwargs):
        super().__init__(name, age, **kwargs)
        self.hours = hours

    def hobby(self):
        print("Likes to go swimming in the lake.")

    def SwimmingHours(self):
        print("Swims ", self.hours, "hours per week.")

#Creation of the ScientificSwimmer
class ScientificSwimmer(Scienctist, Swimmer):
    def __init__(self, name, age, lab, hours, **kwargs):
        super().__init__(name=name, age=age, lab=lab, hours=hours, **kwargs)


输出

>>> person5 = ScientificSwimmer("John Smith", "30", "nuclear", "100")
>>> person5
<__main__.ScientificSwimmer at 0x248ca7fb730>

>>> vars(person5)
{'age': '30', 'name': 'John Smith', 'hours': '100', 'lab': 'nuclear'}



推荐阅读