如何将父类的变量从子类更改为子类(在python中)?

rnmwe5a2  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(397)

这个问题在这里已经有了答案

子类-来自超类的参数(1个答案)
两小时前关门了。
在python中实践继承
我正在python上练习继承。我不确定如何更改父类的变量名。


# Parent Class

class Family_Member():
    def __init__(self, name):
        self.name = name
    def catch_phrase(self):
        print("I am a family member")

# Child Class

class Mum(Family_Member):
    def __init__(self):
        Family_Member.__init__(self)

# Attempting to change variable of parent class from child class

My_Mum = Mum("Kerry")

这给了我

TypeError: __init__() takes 1 positional argument but 2 were given

问题
为什么会发生这种情况?如果没有这个错误,我如何命名我的成员kerry
为什么在子类中不包含init函数时不会发生此错误。e、 g该代码有效


# Attempt 2

class Family_Member():
     def __init__(self, name):
         self.name = name
     def catch_phrase(self):
         print("I am a family member")
 class Mum(Family_Member): # THIS CLASS IS NOW EMPTY
    pass
  My_Mum = Mum("Kerry")
     print(My_Mum.name)
i34xakig

i34xakig1#

class Family_Member():
    def __init__(self, name):
        self.name = name
    def catch_phrase(self):
        print("I am a family member")

class Mum(Family_Member):
    def __init__(self, name):
        Family_Member.__init__(self, name)

My_Mum = Mum("Kerry")

应该有用吧?

相关问题