繁体   English   中英

如何在子 class 中使用父 class 的属性

[英]How can I use attributes from a parent class in a child class

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

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

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

class White (Pieces):

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

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

暂无
暂无

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

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