首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python:从多个父类继承时出现问题

Python:从多个父类继承时出现问题
EN

Stack Overflow用户
提问于 2018-07-13 04:54:45
回答 2查看 69关注 0票数 0

我有一个子类,它需要从两个父类继承,只是方法和单个属性不同。当实例化这个类时,我得到一个错误,声明我正在使用许多参数。当删除一个参数时,它会显示我使用的不够多。

代码语言:javascript
复制
class Ball:
    """
    base class for bouncing objects
    """
    def __init__(self, bounds, position, velocity, color, radius):
        self.position = position
        self.velocity = velocity
        self.bounds = bounds
        self.color = color
        self.radius = radius

    def update(self):
        # bounce at edges.  TODO: Fix sticky edges
        if self.position.x < 0 + self.radius or self.position.x > self.bounds[0] - self.radius: # screen width
            self.velocity.x *= -1
        if self.position.y < 0 + self.radius or self.position.y > self.bounds[1] - self.radius: # screen height
            self.velocity.y *= -1
        self.position += self.velocity

    def draw(self, screen, pygame):
        # cast x and y to int for drawing
        pygame.draw.circle(screen, self.color, [int(self.position.x), int(self.position.y)], self.radius)

BouncingBall

代码语言:javascript
复制
class BouncingBall(Ball):
    """
    ball effected by gravity
    """
    def __init__(self, bounds, position, velocity, color, radius, weight):
        super().__init__(bounds, position, velocity, color, radius)
        self.weight = weight

    def update(self):

KineticBall

代码语言:javascript
复制
class KineticBall(Ball):
    """
    A ball that collides with other collidable balls using simple elastic circle collision
    """
    def __init__(self, bounds, position, velocity, color, radius, object_list):
        super().__init__(bounds, position, velocity, color, radius)
        self.object_list = object_list

KineticBouncing

代码语言:javascript
复制
class KineticBouncing(BouncingBall, KineticBall):
    def __init__(self, bounds, position, velocity, color, radius, weight, object_list):
        super().__init__(bounds, position, velocity, color, radius, weight, object_list)

ball => KineticBouncing

代码语言:javascript
复制
# super().__init__(bounds, position, velocity, color, radius, weight, object_list)
TypeError: __init__() takes 7 positional arguments but 8 were given

ball = KineticBouncing(SCREEN_SIZE, Vector2(50, 50), Vector2(3, 3), [255, 0, 255], 10, -1, object_list)

让我们试试别的..。

所以这很让人困惑..相反,我找到了Python3 Multiple Inheritance,我相信它会解决我的问题。只需使用父级名称+初始化,而不是super(),对吗?

KineticBouncing

代码语言:javascript
复制
class KineticBouncing(BouncingBall, KineticBall):
    def __init__(self, bounds, position, velocity, color, radius, weight, object_list):
        BouncingBall.__init__(self, bounds, position, velocity, color, radius, weight)
        KineticBall.__init__(self, bounds, position, velocity, color, radius, object_list)

ball => KinetBouncing

代码语言:javascript
复制
#Traceback (most recent call last):
#  File "draw.py", line 99, in <module>
#    main()
#  File "draw.py", line 61, in main
#    debug_create_balls(object_list)
#  File "draw.py", line 43, in debug_create_balls
#    ball = KineticBouncing(SCREEN_SIZE, Vector2(50, 50), Vector2(3, 3), [255, 0, 255], 10, -1, object_list)
#  File "/home/adam/Desktop/web_dev/lambda_school/python/Python-OOP-Toy/src/ball.py", line 115, in __init__
#    BouncingBall.__init__(self, bounds, position, velocity, color, radius, weight)
#  File "/home/adam/Desktop/web_dev/lambda_school/python/Python-OOP-Toy/src/ball.py", line 33, in __init__
#    super().__init__(bounds, position, velocity, color, radius)
#TypeError: __init__() missing 1 required positional argument: 'object_list'

ball = KineticBouncing(SCREEN_SIZE, Vector2(50, 50), Vector2(3, 3), [255, 0, 255], 10, -1, object_list)

那么,我到底应该如何从这两个父类继承呢?

EN

回答 2

Stack Overflow用户

发布于 2018-07-13 05:25:14

正如其他人所指出的,你应该重新设计你的类的使用。直接的问题是,super()解析为有问题的对象的第一个父类(self),而不是您当时所在的__init__方法的父类。

当您尝试初始化KineticBouncing对象时,将调用KineticBouncing.super().__init__()。它显式地调用它的两个父类的__init__方法。当它第一次调用BouncingBall中的语句时,第一个活动语句是

代码语言:javascript
复制
    super().__init__(bounds, position, velocity, color, radius)

我相信您期望这会调用Ball.__init__;这不是super的工作方式。相反,它基于对象解析super,该对象属于KineticBouncing类。所以..。对KineticBouncing来说,super是什么?请注意,在您编写的两个__init__调用之间,至少有一次是错误的。

我将留给您阅读评论中提供的链接。这些将帮助您从Python的继承结构角度进行思考。根据您发布的内容,我认为您在处理切换时会遇到一些问题;您只是从其他地方选择了一个层次结构模型。

票数 0
EN

Stack Overflow用户

发布于 2018-07-13 05:58:06

在你父母的所有初始化中使用*args

代码语言:javascript
复制
class Ball(object):
    def __init__(self, *args):
        print ('IN Ball')

class BouncingBall(Ball):
    """
    ball effected by gravity
    """ 
    def __init__(self, bounds, position, velocity, color, radius, weight, *args):
        print ('IN BouncingBall')
        super().__init__(bounds, position, velocity, color, radius, *args)
        self.weight = weight

class KineticBall(Ball):
    """
    A ball that collides with other collidable balls using simple elastic circle collision
    """ 
    def __init__(self, bounds, position, velocity, color, radius, object_list, *args):
        print ('IN KineticBall') 
        super().__init__(bounds, position, velocity, color, radius, *args)
        self.object_list = object_list

class KineticBouncing(BouncingBall, KineticBall):
    def __init__(self, bounds, position, velocity, color, radius, weight, object_list):
        print ('IN KineticBouncing')
        super().__init__(bounds, position, velocity, color, radius, weight, object_list)

现在开始创建一个新的动态弹跳球

代码语言:javascript
复制
ball = KineticBouncing('SCREEN_SIZE', 'Vector2(50, 50)', 'Vector2(3, 3)', [255, 0, 255], 10, -1, 'object_list')
IN KineticBouncing
IN BouncingBall
IN KineticBall
IN Ball
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51314305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档