无法从子类调用父类方法

时间:2018-11-05 02:44:32

标签: python class subclass

在使用Java一年之后,我开始尝试学习Python,并决定从事已经完成的几个班级项目并用Python编写它们。我在第102行收到AttributeError,表示我的超级对象没有属性“ area”。

我遍历了不同的帖子,了解它是如何完成的,在遵循其他解决方案之后,我似乎无法弄清楚为什么这对我来说无法正常工作。

这是一个简单的程序,可以接收用户的输入,进行解析,然后根据形状类型,调用适当对象类型的方法,然后进行计算并打印出来。例如,输入应为“ R 3/4 5-7”(应称重8.37)或“ P 1/4 5-6 2-3”(应称重126.07)。

import math

class Shipment:
    _weight = 0

    def weight(self):
        return self._weight

    def frac_in_feet(self, frac):
        thick_frac = frac.replace('/', "")
        print("number is: ", thick_frac)
        numerator = int(thick_frac[0])
        denominator = int(thick_frac[1])
        fraction_of_an_inch = numerator / denominator

        return self.in_feet(0, fraction_of_an_inch)

    def feet_and_inches_in_feet(self, feet_and_inches):
        a = feet_and_inches.replace('-', "")
        print("number is: ", a)
        feet = int(a[0])
        inches = int(a[1])

        return self.in_feet(feet, inches)

    def in_feet(self, feet, inches):
        inches /= 12
        print(feet + inches)
        return feet + inches

    def get_value_in_feet(self, str):
        i = str.find('/')
        j = str.find('-')

        if i == -1:
            value = self.feet_and_inches_in_feet(str)

        if j == -1:
            value = self.frac_in_feet(str)

        return value

    def add_item(self, quantity, description):
        desc_values = description.replace(" ", "")
        values = []
        shape_letter = desc_values[0]
        i = 1
        j = 4
        for r in range(int(len(desc_values) / 3)):
            print("r is: ", r)
            values.append(self.get_value_in_feet(desc_values[i:j]))

            i += 3
            j += 3

        if shape_letter == 'P':
            if len(values) != 3:
                raise ValueError("Plate needs three dimensions. ")

            shape = Plate(values[0], values[1], values[2])

        elif shape_letter == 'R':
           if len(values) != 2:
               raise ValueError("Rod needs two dimensions")

           shape = Rod(values[0], values[1])

        else:
            raise ValueError("Shape letter ", shape_letter, " not recognized.")

        self._weight += quantity * shape.weight()
        return shape





class SteelShape:
    _length = 0
    _weight = 0

    def length(self, length):
        _length = length

    def length(self):
        return self._length

    def weight(self, weight):
        self._weight = weight

    def weight(self):
        return self._weight

class CalcShape(SteelShape):
    _area = 0

    def area(self, area):
        self._area = area

    def weight(self):
        return self._area * self.length() * 489





class Rod(CalcShape):
    def __init__(self, diameter, length):
        radius = diameter / 2
        super(CalcShape, self).area(math.pi * radius**2)
        super(CalcShape, self).length





class Plate(CalcShape):
    def __init__(self, thick, width, length):
        super(CalcShape, self).area(thick * width)
        super(SteelShape, self).length(length)

values = []
shipment = Shipment()
shape = SteelShape()
line = input("Enter shape and dimensions: ")
shape = shipment.add_item(1, line)
if isinstance(shape, Plate):
    print("Plate weighs ", shape.weight())

elif isinstance(shape, Rod):
    print("Rod weighs ", shape.weight())


print("Total shipment weight: ", shipment.weight())
# R 3/4 5-7: supposed to weigh 8.37
# P 1/4 5-6 2-3: supposed to weigh 126.07

1 个答案:

答案 0 :(得分:0)

我认为替换super而不是使用staticmethod是更好的选择。看到这里:

https://stackoverflow.com/a/735978/10416716

希望这会有所帮助。

相关问题