Skip to main content
 首页 » 编程设计

python之在 Python 中管理实例

2025年05月04日15dudu

我是 Python 的新手,这是我第一次提出 stackOverflow 问题,但我已经是老读者了。我正在开发一款简单的纸牌游戏,但在管理 Hand 类实例时遇到问题。如果您看下面,您会发现 hand 类是纸牌的简单容器(只是 int 值),每个 Player 类都包含一个 hand 类。但是,每当我创建 Player 类的多个实例时,它们似乎都在操纵 Hand 类的单个实例。根据我在 C 和 Java 方面的经验,我似乎以某种方式使我的 Hand 类成为静态的。如果有人可以帮助解决这个问题,我将不胜感激。

谢谢, 泰德

澄清一下:这种情况的一个例子是

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 

请您参考如下方法:

From my experience in C and Java it seems that I am somehow making my Hand class static.

实际上,这基本上就是您正在做的。好吧,不是真正使类成为静态的,而是使变量成为静态的。

当你这样写声明时:

class Hand: 
    cards = [] 

该变量 (cards) 与类相关联,而不是与实例相关联。与 Java 类比,Python 类中不属于该类方法的每个语句基本上都在静态初始化程序中运行。你几乎可以这样想:

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

(当然只是粗略的类比)

要在 Python 中创建一个实例变量,您必须将其设置为实例的一个属性,这需要您等待,直到您拥有对该实例的引用。实际上,这意味着您在构造函数中对其进行初始化,如下所示:

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