繁体   English   中英

覆盖导入类中的特定方法

[英]Override specific method in imported class

我正在构建一个可以玩多种纸牌游戏的应用程序。 在最低级别,我有一个 Card 类,它将被每个游戏类导入,见下文。

class Card:

    def __init__(self, rank=None, suit=None):

        ranks = {'2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'}
        suits = {'Spades', 'Diamonds', 'Clubs', 'Hearts'}
        assert(rank in ranks), 'Not a valid rank'
        assert(suit in suits), 'Not a valid suit'

        self.rank = rank
        self.suit = suit

    @staticmethod
    def rank_to_numeric(rank):
        if rank == 'J':
            return 11
        if rank == 'Q':
            return 12
        if rank == 'K':
            return 13
        if rank == 'A':
            return 14
        else:
            return int(rank)

    def __repr__(self):
        return f'{self.rank} of {self.suit}'  # Example: 7 of Spades

    def __lt__(self, other):
        return self.rank_to_numeric(self.rank) < self.rank_to_numeric(other.rank)

    def __le__(self, other):
        return self.rank_to_numeric(self.rank) <= self.rank_to_numeric(other.rank)

    def __eq__(self, other):
        return (self.rank, self.suit) == (other.rank, other.suit)

    def __ne__(self, other):
        return not (self == other)

    def __gt__(self, other):
        return self.rank_to_numeric(self.rank) > self.rank_to_numeric(other.rank)

    def __ge__(self, other):
        return self.rank_to_numeric(self.rank) >= self.rank_to_numeric(other.rank)

在大多数游戏中,2 是最低的牌,而 A 是最高的。 但是,现在我正在尝试为荷兰纸牌游戏“Toepen”实现一个游戏类,其中排名为 JQKA-7-8-9-10(从低到高)。 本游戏不使用卡片 2 至 6。

我知道我可以将排名作为参数添加到 Cards 的构造中,但我想知道是否可以在导入 Card 类后覆盖 Toepen 类中的比较方法。 请参阅下面的示例

import Card

class Toepen:
    """
    class for the popular dutch card game Toepen.
    Rules according wikipedia: https://nl.wikipedia.org/wiki/Toepen
    The optional rules can be set in the settings
    """

    def __init__(self):
        # Set the round counter. A single game consists of 4 rounds
        self.round = 0

        # Set the trump (troef)
        self.trump = None

        # Set the highest card played so far
        self.incumbent = 0

        # Set the pile of cards so far played in this round
        self.pile = []

    # def Card.__lt__(self, other):
        # implement method here ???

是的,你可以这样做:

class A:
    def foo(self):
        print("foo")


class B:

    def new_foo(self):
        print("new_foo")

    def over_ride(self):
        A.foo = self.new_foo

    def __init__(self):
        self.over_ride()

这将重新分配A类的foo 如果您要从代码中的任何位置访问A ,那么从您创建B的实例的那一刻起,它就会拥有new_foo的主体。

在你的情况下,它会是这样的

class Toepen:
    """
    class for the popular dutch card game Toepen.
    Rules according wikipedia: https://nl.wikipedia.org/wiki/Toepen
    The optional rules can be set in the settings
    """

    def __init__(self):
        # Set the round counter. A single game consists of 4 rounds
        self.round = 0
        Card.__lt__ = self.new__lt__
    

        # Set the trump (troef)
        self.trump = None

        # Set the highest card played so far
        self.incumbent = 0

        # Set the pile of cards so far played in this round
        self.pile = []

    def new__lt__(self,other):
        pass
    

暂无
暂无

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

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