【问题标题】:Trying to use the same method in multiple inherited classes with different attributes尝试在具有不同属性的多个继承类中使用相同的方法
【发布时间】:2022-01-20 23:34:21
【问题描述】:

我正在尝试弄清楚如何大致执行以下操作。有些东西只有foo,有些东西同时有foobar,还有一些东西有foobarbaz。我希望能够调用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


【解决方案1】:
    class Initial:
    def __init__(self, foo):
        self.total_foo = None
        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.total_bar = None
        self.bar = bar

    def calc(self, amount):
        self.total_bar = self.bar * amount
        return self.total_bar


class GrandChild(Child):
    def __init__(self, foo, bar, baz):
        super().__init__(foo, bar)
        self.total_baz = None
        self.baz = baz

    def calc(self, amount):
        self.total_baz = self.baz * amount
        return self.total_baz


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)

使用super().__init__(values) 将值传递给父类。

【讨论】:

  • 在使其正常工作时遇到问题。您能否通过在 Child(2,4) 和 GrandChild(10,20,30) 上调用 calc 来使其更清楚。 AttributeError: 'Child' 对象没有属性 'total_foo'
  • 我已经更新了答案。由于您计算价值的方式,您不会得到您想要的结果。您正在覆盖 Child 和 Grandchild 类中的 calc 方法。
猜你喜欢
  • 1970-01-01
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多