【发布时间】:2022-01-20 23:34:21
【问题描述】:
我正在尝试弄清楚如何大致执行以下操作。有些东西只有foo,有些东西同时有foo和bar,还有一些东西有foo、bar和baz。我希望能够调用Child.calc(10) 或GrandChild.calc(10) 并根据需要正确设置属性。
在构建 Initial、Child 和 Grandchild 的多个实例的最后,我想对它们中的任何一个调用 calc 并让它工作,不管它们是什么类。一开始我打算做 calc_initial, calc_child, calc_grandchild,但是好像错了。
初始代码
class Initial:
def __init__(self, foo):
self.foo = foo
def Calc(self, amount):
self.TotalFoo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
Initial.__init__(self, foo)
self.bar = bar
def Calc(self, amount):
self.TotalBar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
Child.__init__(self, foo, bar)
self.baz = baz
def Calc(self, amount):
self.TotalBaz = self.baz * amount
GrandChild(10, 20, 30)
GrandChild.Calc(10)
print(GrandChild.TotalFoo) # Want this to be 100
print(GrandChild.TotalBar) # Want this to be 200
print(GrandChild.TotalBaz) # Want this to be 300
感谢 mkrieger1 编辑代码
class Initial:
def __init__(self, foo):
self.foo = foo
def calc(self, amount):
self.total_foo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
Initial.__init__(self, foo)
self.bar = bar
def calc(self, amount):
Initial.calc(self, amount)
self.total_bar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
Child.__init__(self, foo, bar)
self.baz = baz
def calc(self, amount):
Child.calc(self, amount)
self.total_baz = self.baz * amount
child = Child(2,4)
grandchild = GrandChild(10, 20, 30)
child.calc(10)
grandchild.calc(10)
print(child.total_foo) # Want this to be 20
print(child.total_bar) # Want this to be 40
print(grandchild.total_foo) # Want this to be 100
print(grandchild.total_bar) # Want this to be 200
print(grandchild.total_baz) # Want this to be 300
试图合并 super 但感觉有些不对劲。结果正确但不确定。
class Initial:
def __init__(self, foo):
self.foo = foo
def calc(self, amount):
self.total_foo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
Initial.__init__(self, foo)
self.bar = bar
def calc(self, amount):
super().calc(amount)
self.total_bar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
Child.__init__(self, foo, bar)
self.baz = baz
def calc(self, amount):
super().calc(amount)
self.total_baz = self.baz * amount
child = Child(2,4)
grandchild = GrandChild(10, 20, 30)
child.calc(10)
grandchild.calc(10)
print(child.total_foo) # Want this to be 20
print(child.total_bar) # Want this to be 40
print(grandchild.total_foo) # Want this to be 100
print(grandchild.total_bar) # Want this to be 200
print(grandchild.total_baz) # Want this to be 300
当试图做长辈的回应时
class Initial:
def __init__(self, foo):
self.foo = foo
def calc(self, amount):
self.total_foo = self.foo * amount
class Child(Initial):
def __init__(self, foo, bar):
super().__init__(foo)
self.bar = bar
def calc(self, amount):
self.total_bar = self.bar * amount
class GrandChild(Child):
def __init__(self, foo, bar, baz):
super().__init__(foo, bar)
self.baz = baz
def calc(self, amount):
self.total_baz = self.baz * amount
child = Child(2,4)
grandchild = GrandChild(10, 20, 30)
child.calc(10)
grandchild.calc(10)
print(child.total_foo) # Want this to be 20
print(child.total_bar) # Want this to be 40
print(grandchild.total_foo) # Want this to be 100
print(grandchild.total_bar) # Want this to be 200
print(grandchild.total_baz) # Want this to be 300
我明白了
26 child.calc(10)
27 grandchild.calc(10)
---> 28 print(child.total_foo) # Want this to be 20
29 print(child.total_bar) # Want this to be 40
30 print(grandchild.total_foo) # Want this to be 100
AttributeError: 'Child' object has no attribute 'total_foo'
【问题讨论】:
-
我重命名了方法和属性以遵循更惯用的 Python 风格。
-
谢谢你,写得很快,但你是 100% 正确的,我需要努力。
-
你的问题是为什么在尝试调用方法时会出错?
-
我试图弄清楚为什么会发生丢失位置参数的错误,第二,只是找出大致完成我想要完成的事情的正确方法。不知道我是不是做错了,或者我只是稍微走错了一步。
-
你需要用类的实例来调用方法,而不是用类本身。
标签: python inheritance methods