Python使用super覆盖属性,传递参数时出现意外的参数错误

标签 python oop inheritance

我有一个国际象棋程序的棋子类,我希望其他棋子继承自:

class Piece:
    def __init__(self, player, diag, straight, opponent, marker, unicode):

        self.player = player
        self.diag = diag
        self.straight = straight
        self.opponent = opponent
        self.marker = marker
        self.unicode = unicode

例如,一个国王类必须设置特定于自身但属于基类属性的属性:

from Piece import Piece

class King(Piece):

    def __init__(self, player, opposition, unicode):
        super.__init__(player,False, False, opposition,"K", unicode)
        self.has_moved = False
        self.casle_king = True
        self.castle_queen = True

我的问题是,当我将它们传递给 super 函数时,它们会抛出意外的参数错误,即使我已经在文章中定义了它们。

King 从 Piece 继承的正确方法是什么,同时设置它们共享的不同属性的正确方法是什么?

最佳答案

你忘记了 super 上的括号:

super().__init__(player,False, False, opposition,"K", unicode)

或者:

Piece.__init__(self, player,False, False, opposition,"K", unicode)

关于Python使用super覆盖属性,传递参数时出现意外的参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53171502/

相关文章:

c# - 在 WPF C# 中将值从一个窗口传递到另一个窗口时出现问题

python - 从字符串中提取值

java - 为什么要在 Java 中编写自定义异常类

java - 员工薪资计算

unit-testing - 起订量和访问调用的参数

java - 子类中的空方法有解决办法吗?

c++ - 自定义派生的 std::exception 类的 'what' 函数返回神秘的废话

python - 如何在 ../src/x86/ffi64.c :158 处修复 Python 函数 "classify_argument"中的段错误

python - 如何忽略列表中的高偏差

python - Django python manage.py 迁移