在Python中管理实例

1 投票
3 回答
1145 浏览
提问于 2025-04-15 23:29

我刚开始学习Python,这是我第一次在StackOverflow上提问,不过我已经看了很久了。我正在做一个简单的卡牌游戏,但在管理我的手牌类(Hand class)实例时遇到了麻烦。如果你看看下面的内容,你会发现手牌类只是一个简单的卡片容器(卡片其实就是一些整数值),而每个玩家类(Player class)都包含一个手牌类。然而,每当我创建多个玩家实例时,它们似乎都在操作同一个手牌实例。从我在C和Java的经验来看,感觉我不小心把手牌类变成了静态的。如果有人能帮我解决这个问题,我将非常感激。

谢谢,
Thad

为了更清楚地说明:这种情况的例子是

p = player.Player()
p1 = player.Player()
p.recieveCard(15)
p1.recieveCard(21)
p.viewHand()

这将导致结果是:
[15,21]
尽管实际上只有一张卡片被添加到了p的手牌中。

手牌类:

class Hand:

    index = 0
    cards = [] #Collections of cards

    #Constructor
    def __init__(self):
        self.index
        self.cards

    def addCard(self, card):
        """Adds a card to current hand"""
        self.cards.append(card)
        return card

    def discardCard(self, card):
        """Discards a card from current hand"""
        self.cards.remove(card)
        return card

    def viewCards(self):
        """Returns a collection of cards"""
        return self.cards

    def fold(self):
        """Folds the current hand"""
        temp = self.cards
        self.cards = []
        return temp

玩家类:

import hand
class Player:

 name = ""
 position = 0
 chips = 0
 dealer = 0
        pHand = []

 def __init__ (self, nm, pos, buyIn, deal):
            self.name = nm
            self.position = pos
            self.chips = buyIn
            self.dealer = deal

            self.pHand = hand.Hand()
            return

        def recieveCard(self, card):
            """Recieve card from the dealer"""
            self.pHand.addCard(card)
            return card

        def discardCard(self, card):
            """Throw away a card"""
            self.pHand.discardCard(card)
            return card

        def viewHand(self):
            """View the players hand"""
            return self.pHand.viewCards()

        def getChips(self):
            """Get the number of chips the player currently holds"""
            return self.chips

        def setChips(self, chip):
            """Sets the number of chips the player holds"""
            self.chips = chip
            return

        def makeDealer(self):
            """Makes this player the dealer"""
            self.dealer = 1
            return

        def notDealer(self):
            """Makes this player not the dealer"""
            self.dealer = 0
            return

        def isDealer(self):
            """Returns flag wether this player is the dealer"""
            return self.dealer

        def getPosition(self):
            """Returns position of the player"""
            return self.position

        def getName(self):
            """Returns name of the player"""
            return self.name

3 个回答

0

正如其他回答所提到的,你对类变量和实例变量有些混淆。我建议你回顾一下Python类的基本知识。这里有一个我为另一个问题写的回答,读一读可能会对你有帮助。

如何在Python中定义一个类

3

你的问题其实很简单。

如果你把列表直接放在 Python 类的主体里,当你往里面添加东西时,这些东西会被存储在类级别,而不是在实例级别。

你可以通过添加以下这一行来解决这个问题:

def __init__(self):
    self.cards = []

这是一个非常常见的 Python 陷阱,我建议你看看这篇文章:

http://zephyrfalcon.org/labs/python_pitfalls.html

4

根据我在C和Java中的经验,似乎我在某种程度上把我的Hand类变成了静态的。

其实,你的确是在这么做。嗯,严格来说不是把类变成静态的,而是把变量变成了静态。

当你写出这样的声明时:

class Hand:
    cards = []

这个变量(cards)是和关联的,而不是和实例关联的。用Java来做个类比,Python类中所有不属于该类方法的语句,基本上都在一个静态初始化器中运行。你可以这样理解:

class Hand {
    static {
        cards = new object[];
    }
}

(当然这只是个粗略的类比)

要在Python中创建一个实例变量,你必须把它设置为实例的一个属性,这就需要等到你有了对实例的引用。实际上,这意味着你在构造函数中初始化它,像这样:

class Hand:
    def __init__(self):
        self.cards = []

撰写回答