首页 > 解决方案 > 如何在另一个类中使用一个类中的变量而不将类作为参数传递给构造函数

问题描述

我想在课堂上使用and self.numbersfrom self.suitsthe class。我知道这样做的唯一方法是如下所示。在 class的方法中作为参数传递。这是问题所在。为了然后使用该类,我还必须传递一个参数,我不需要或不想这样做。希望我能很好地解释自己。我在尝试从类和类中传递变量时遇到同样的问题。CardsDeckclass.cardDeck__init__DeckCardDeckPlayer

这是代码:

import random


class Card:

    def __init__(self):
        self.numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king', 'ace']
        self.suits = ['hearts', 'diamonds', 'clubs', 'spades']

    # print the current card
    def __str__(self):
        print(f'{self.numbers} of {self.suits}')


class Deck(Card):
    def __init__(self, class_card):
        super().__init__()
        self.deck = []
        self.numbers = class_card.numbers
        self.suits = class_card.suits

    # create a new deck of cards
    def create_deck(self):
        for suit in self.suits:
            for number in self.numbers:
                self.deck.append([number, suit])
                return self.deck

    # shuffle the deck of cards before the start of the game
    def shuffle_deck(self, deck):
        random.shuffle(deck)

class Player(Deck):
    def __init__(self, class_deck):
        super().__init__()
        self.hand = []
        self.count_cards = 0
        self.score = 0

    # draw a card from the deck
    def draw_card(self):
        drawn_card = self.hand.append(self.deck[-1])
        return drawn_card

    # count the cards in the players hand
    def count_hand(self):
        self.count_cards = len(self.deck)
        return self.count_cards

    # calculate the score each time the player wins
    def calculate_score(self):
        pass

标签: pythonclassoop

解决方案


当您运行super().__init__()甲板将继承卡的数字和西装属性。请参阅以下内容:

import random


class Card:

    def __init__(self):
        self.numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king', 'ace']
        self.suits = ['hearts', 'diamonds', 'clubs', 'spades']

    # print the current card
    def __str__(self):
        print(f'{self.numbers} of {self.suits}')


class Deck(Card):
    def __init__(self):
        super().__init__()
        self.deck = []

    # create a new deck of cards
    def create_deck(self):
        for suit in self.suits:
            for number in self.numbers:
                self.deck.append([number, suit])
        return self

    # shuffle the deck of cards before the start of the game
    def shuffle_deck(self):
        random.shuffle(self.deck)

    def __len__(self):
        return len(self.deck)

    def pop(self):
        return self.deck.pop()


class People:
    def __init__(self, name):
        self.name = name


class Player(People):
    def __init__(self, name, deck):
        super().__init__(name)
        self.hand = []
        self.count_cards = 0
        self.score = 0
        self.deck = deck

    # draw a card from the deck
    def draw_card(self):
        drawn_card = self.hand.append(self.deck.pop())
        return drawn_card

    # count the cards in the players hand
    def count_hand(self):
        self.count_cards = len(self.hand)
        return self.count_cards

    # calculate the score each time the player wins
    def calculate_score(self):
        pass


def main():
    deck = Deck().create_deck()
    players = [Player(name, deck) for name in ('Jim', 'Ben', 'Paul', 'Wang')]
    deck.shuffle_deck()
    i = 0
    while len(deck):
        players[i].draw_card()
        i = (i + 1) % len(players)
    for p in players:
        print(p.name, p.count_hand(), p.calculate_score())


if __name__ == '__main__':
    main()

输出:

Jim 14 None
Ben 14 None
Paul 14 None
Wang 14 None

但是,我推荐的解决方案是这样的:

import random


class Card:
    numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king', 'ace']
    suits = ['hearts', 'diamonds', 'clubs', 'spades']

    def __init__(self, number, suit):
        self.number = number
        self.suit = suit
        self.score = self.numbers.index(number) * 10 + self.suits.index(suit)

    # print the current card
    def __str__(self):
        print(f'{self.number} of {self.suit}')

    def __eq__(self, other):
        return self.score == other.score

    def __lt__(self, other):
        return self.score < other.score


class Deck:
    def __init__(self):
        self.deck = [
            Card(num, suit) for num in Card.numbers for suit in Card.suits
        ]

    # shuffle the deck of cards before the start of the game
    def shuffle(self):
        random.shuffle(self.deck)
        return self

    def __len__(self):
        return len(self.deck)

    def pop(self):
        return self.deck.pop()


class People:
    def __init__(self, name):
        self.name = name


class Player(People):
    def __init__(self, name, deck):
        super().__init__(name)
        self.hand = []
        self.count_cards = 0
        self.score = 0
        self.deck = deck

    # draw a card from the deck
    def draw_card(self):
        drawn_card = self.hand.append(self.deck.pop())
        return drawn_card

    # count the cards in the players hand
    def count_hand(self):
        self.count_cards = len(self.hand)
        return self.count_cards

    # calculate the score each time the player wins
    def calculate_score(self):
        return sum(card.score for card in self.hand)


def main():
    deck = Deck().shuffle()
    players = [Player(name, deck) for name in ('Jim', 'Ben', 'Paul', 'Wang')]
    i = 0
    while len(deck):
        players[i].draw_card()
        i = (i + 1) % len(players)
    for p in players:
        print(p.name, p.count_hand(), p.calculate_score())


if __name__ == '__main__':
    main()

推荐阅读