简体   繁体   English

如何正确调用方法?

[英]How do I call a method properly?

I am working on a game and have met a problem. 我正在开发游戏,遇到了问题。 I am getting a error when trying to call the draw_red_team_one() method but I can't see the reason why. 尝试调用draw_red_team_one()方法时出现错误,但我看不出原因。

It says missing 1 required positional argument: self so I checked it out and found out that I had to write RT = red and then self.draw_red_team_one() so I fixed that but I still get the same error. 它说缺少1个必需的位置参数: self所以我检查了一下,发现我必须先写RT = red ,然后再写self.draw_red_team_one()所以我解决了这个问题,但仍然遇到相同的错误。

class red:
    def __init__(self):
        self.x_y = [130, 290]
        self.height_width = [10, 3]
        self.red = [255, 0, 0]

    def draw_salt(self, surface, color, x, y, height, width):
        pygame.draw.rect(surface, color, ((x, y), (height, width)))

    def draw_red_team_one(self):
        self.draw_salt(screen, red, x_y[0], x_y[1], height_width[0], height_width[1])
running = True

while running:
    RT = red
    RT.draw_red_team_one()
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

I expect the full program to open a pygame window and print a white horizontal line with a red box on the top left side. 我希望整个程序可以打开pygame窗口,并在左上角打印带有红色方框的白色水平线。 The program just throws me a error. 该程序只是抛出一个错误。

With RT = red you assigned the class to RT, but draw_red_team_one is an instance method and as such it requires the positional argument self as declared in your method. 使用RT = red您将类分配给RT,但是draw_red_team_one是一个实例方法,因此它需要在方法中声明的位置self变量self If you call this method on an instance of class red , the argument is implicitely passed, so you don't notice it, but if you call it using red.draw_red_team_one() you have to pass it yourself, otherwise you get the error message, you mentioned. 如果在类red的实例上调用此方法,则隐式传递参数,因此您不会注意到它,但如果使用red.draw_red_team_one()调用它,则必须自己传递它,否则会收到错误消息,您提到过。 To put it in another way: 换句话说:

obj.draw_red_team_one()

is the same as: 是相同的:

red.draw_red_team_one(obj)

The second variant is just what Python implicitly does, when the first variant is executed. 第二个变体就是执行第一个变体时Python隐式执行的操作。

But anyways, you will not need to reference the method in the style of red.draw_red_team_one very often. 但是无论如何,您将不需要经常使用red.draw_red_team_one样式引用该方法。 You normally only need this, if you have to pass the method as callable somewhere. 通常,只有在必须将方法作为可调用的地方传递时,才需要此方法。 Eg if you have a class with a method, that returns some info of the objects and you want to use methods like map to get the information for each object in a list. 例如,如果您有一个带有方法的类,则该方法返回对象的一些信息,并且您想使用诸如map方法来获取列表中每个对象的信息。 In that case, you would do something like: 在这种情况下,您将执行以下操作:

map(PersonClass.name, list_of_people)

Which is the same as (in the sense that both are iterables returning the same elements): 相同(在某种意义上,这两个都是返回相同元素的可迭代对象):

[person.name() for person in list_of_people]

Btw. 顺便说一句。 I think your code should rather look like this (haven't checked if it runs though): 我认为您的代码应该看起来像这样(尽管没有运行):

class red:
    def __init__(self):
        self.x_y = [130, 290]
        self.height_width = [10, 3]
        self.red = [255, 0, 0]

    def draw_salt(self, surface, color, x, y, height, width):
        pygame.draw.rect(surface, color, ((x, y), (height, width)))

    def draw_red_team_one(self):
        # you need to add self. to reference the instance variables
        self.draw_salt(screen, self.red, self.x_y[0], self.x_y[1], self.height_width[0], self.height_width[1])

running = True

while running:
    # create an object (this creates an instance and implicitely calls __init__ which performs your initialization code)
    RT = red()
    RT.draw_red_team_one()
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

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

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