在进行类继承时使用super.__ init __的问题

问题描述

我有这个简单的代码来计算定义3个类的矩形的高度和宽度:

  1. PointL使用x和y属性创建1个点。
  2. polyline:仅包含Point类中的4个点。
  3. Rectangle:应该从polyline调用点并返回heightwidth

但是我一直都在0.0height上获得width。为此,我必须使用super()方法

    class Point:
        def __init__(self,x=0.0,y=0.0):
            self.__x = x
            self.__y = y

        def set_x(self,x):
            self.__x = x

        def set_y(self,y):
            self.__y = y

        def set(self,x,y):
            self.__x = x
            self.__y = y

        def get_x(self):
            return self.__x

        def get_y(self):
            return self.__y

        def __str__(self): # <---- New __str__ method
            return "Point("+str(self.__x)+","+str(self.__y)+")"

        x = property(get_x,set_x)
        y = property(get_y,set_y)

    class polyline:
        def __init__(self,n=None):
            self.__p0 = Point()
            self.__p1 = Point()
            self.__p2 = Point()
            self.__p3 = Point()
            self.__n = n

        def get_p0(self):
            return self.__p0

        def get_p1(self):
            return self.__p1

        def get_p2(self):
            return self.__p2

        def get_p3(self):
            return self.__p3

        def set_p0(self,p):
            self.__p0 = p

        def set_p1(self,p):
            self.__p1 = p

        def set_p2(self,p):
            self.__p2 = p

        def set_p3(self,p):
            self.__p3 = p

        def __str__(self):
            return "Line 1 from: " + str(self.__p0) + " to " + str(self.__p2) + "\n" + "Line 2 from: " + str(self.__p1) + " to " + str(self.__p3) + "\n"

        p0 = property(get_p0,set_p0)
        p1 = property(get_p1,set_p1)
        p2 = property(get_p2,set_p2)
        p3 = property(get_p3,set_p3)

    class Rectangle(Point):
        def __init__(self,n=None,height=0.0,width=0.0):
            super().__init__(n) # <--- Call inherited constructor of Point-class
            self.__height = height
            self.__width = width

        def get_height(self):
            p0 = super().get_p0()
            p1 = super().get_p1()
            self.__height = p0.y - p1.y

        def get_width(self):
            p0 = super().get_p0()
            p1 = super().get_p1()
            self.__width = p0.x - p1.x

        def __str__(self):
            return "Rectangle dimensions: \n" + "Width: " + str(self.__width) + "," + "Height: " + str(self.__height) + "\n"

        width = property(get_width)
        height = property(get_height)

These are my inputs:

    polyline = polyline()
    polyline.p0.x = 0.0
    polyline.p0.y = 2.0
    polyline.p1.x = 3.0
    polyline.p1.y = 4.0
    print(polyline)
    rectangle = Rectangle()
    print(rectangle)

And these are my outputs:

    Line 1 from: Point(0.0,2.0) to Point(0.0,4.0)
    Line 2 from: Point(3.0,4.0) to Point(3.0,2.0)

    Rectangle dimensions:
    Width: 0.0,Height: 0.0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)