简体   繁体   English

Python,覆盖父函数

[英]Python, overriding a parent function

I've read a bunch of things here and on other sites about how to override functions, but none of them fit my needs. 在这里和其他站点上,我已经阅读了很多有关如何覆盖函数的内容,但是都没有满足我的需求。

I only need to change one line of code, so using super(Class, self).function(parameter) doesn't seem to help. 我只需要更改一行代码,所以使用super(Class, self).function(parameter)似乎无济于事。 I may be doing it completely wrong. 我可能做的完全错误。

Anyway, here's the parent: 无论如何,这是父母:

class Platformer(Entity):
    def __init__(self, color, width, height, x, y):
        Entity.__init__(self)
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        self.xvel = 0
        self.yvel = 0
        self.onGround = False

    def update(self, up, down, left, right, platforms):
        if up:
            if self.onGround:
                self.yvel -= 8
                bounceSound.play()
        if down:
            self.yvel += 5
        if left:
            self.xvel = -3
        if right:
            self.xvel = 3
        if not self.onGround:
            self.yvel += 0.5
            if self.yvel > 15:
                self.yvel = 15
        if not(left or right):
            if self.onGround:
                self.xvel = 0

        self.rect.left += self.xvel
        self.collide(self.xvel, 0, platforms)
        self.rect.top += self.yvel
         self.onGround = False
        self.collide(0, self.yvel, platforms)

    def collide(self, xvel, yvel, plats):
        for p in platforms:
                if sprite.collide_rect(self, p) and isinstance(p, Block):
                    if xvel > 0:
                        self.rect.right = p.rect.left
                    if xvel < 0:
                        self.rect.left = p.rect.right
                    if yvel > 0:
                        self.rect.bottom = p.rect.top
                        self.onGround = True
                        self.yvel = 0
                    if yvel < 0:
                        self.rect.top = p.rect.bottom
                        self.yvel = 0

And this is the child that I wrote: 这是我写的孩子:

class Bird(Platformer):

    def update(self, up, down, left, right, platforms):
        if up:
            self.yvel -= 8
        if down:
            self.yvel += 5
        if left:
            self.xvel = -3
        if right:
            self.xvel = 3
        if not self.onGround:
            self.yvel += 0.5
            if self.yvel > 15:
                self.yvel = 15
        if not(left or right):
            if self.onGround:
                self.xvel = 0

Notice that all I want to change is the if statement in the "up" part of update . 注意,我要更改的只是update的“ up”部分中的if语句。 When running the program, everything works fine (bird gets drawn where it needs to be, its the right color, etc) except that update doesnt work. 运行该程序时,除更新无效外,其他所有程序都工作正常(将鸟绘制在需要的位置,正确的颜色等)。

Can someone help me to understand this? 有人可以帮我理解吗? I really can't find the right syntax for this. 我真的找不到适合的语法。

I would change the structure as follows: 我将更改结构如下:

In the parent add an extra parameter parentBehaviour : 在父级中添加一个额外的参数parentBehaviour

def update(self, up, down, left, right, platforms, parentBehaviour=True):
    if up:
        if parentBehaviour and self.onGround:
            self.yvel -= 8
            bounceSound.play()
    #rest of method...

In the child: 在孩子中:

def update(self, up, down, left, right, platforms):
    super(Bird, self).update(up, down, left, right, platforms, False)
    #end method

Reason for this structure, is generally you generally want to avoid duplicating your code. 之所以采用这种结构,通常是因为您通常希望避免重复代码。 See Wikipedia for the DRY principle . 有关DRY原理,请参见Wikipedia。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM