【问题标题】:How can I use attributes from a parent class in a child class如何在子类中使用父类的属性
【发布时间】:2019-10-10 00:35:13
【问题描述】:

我正在编写一个国际象棋程序,但不太了解继承。如何在子类中使用父类的属性(empassant)? 我的父类是:

class Pieces():
    def __init__(self, empassant=(-5,-5)):
        super().__init__()
        self.empassant=empassant

我的子类是:

class White (Pieces):

    def __init__(self):
        #stuff
    def pawn(self, pieceposition):
        empassant=#empassant from the pieces class

【问题讨论】:

  • 如果您在浏览器中搜索“Python 类继承教程”,您会找到比我们在此处管理的更能解释这一点的参考资料。

标签: python python-3.x class inheritance tkinter


【解决方案1】:

你需要先在孩子的__init__中调用super().__init__()

class White (Pieces):

    def __init__(self):
        super().__init__()
        #others initialization here

    def pawn(self, pieceposition):
        print(self.empassant)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2020-11-08
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多