我正在尝试使用Card类中的[identityNumberValidator] = "element == 1 ? '' : null"
和card_number(
方法来确定Rule类中的last_card西装和号码。
card_suit()
当我访问该功能时,它们会保存卡并仅适用于卡变量,也适用于最后一张卡
现在我得到:
class Card:
def __init__(self, card):
self.card = card
self.number = self.card_number(card)
self.suit = self.card_suit(card)
def card_number(self, card):
card = self.card
number = card[0]
return number
def card_suit(self, card):
card = self.card
suit = card[-1]
return suit
class Rules(Card):
def __init__(self, last_card, card):
Card.__init__(self, card)
self.last_card = last_card
self.last_card_number = self.card_number(self.last_card)
self.last_card_suit = self.card_suit(self.last_card)
我期望:
a = Rules(last_card="As", card = "5c")
print(a.card) -> "5c"
print(a.number)-> "5"
print(a.suit) -> "c"
print("last_card")
print(a.last_card) -> "As"
print(a.last_card_number) -> "5"
print(a.last_card_suit) -> "c"
答案 0 :(得分:1)
正如我在评论中提到的那样,当您需要Rules
仅保存两个Card
实例时,您不需要Rules
成为Card
的超类。 / p>
您可以按照以下步骤修改您的班级
class Card:
#Instantiating instance attributes
def __init__(self, card):
self.card = card
self.number = self.card[0]
self.suit = self.card[-1]
class Rules:
#Two instances of Card class
def __init__(self, last_card, card):
self.last_card = Card(last_card)
self.card = Card(card)
a = Rules(last_card="As", card = "5c")
print(a.card.card)
print(a.card.number)
print(a.card.suit)
print(a.last_card.card)
print(a.last_card.number)
print(a.last_card.suit)
输出将为
5c
5
c
As
A
s
您甚至可以摆脱Rules
类,并制作两个Card
类的实例,并改为使用它们
last_card = Card('As')
card = Card('5c')
答案 1 :(得分:0)
这里的问题是您要覆盖card
类中方法的参数Card
:
def card_number(self, card):
card = self.card
number = card[0]
return number
def card_suit(self, card):
card = self.card
suit = card[-1]
return suit
在此处删除card = self.card
行,它应该可以正常工作。它应该看起来像这样:
def card_number(self, card):
number = card[0]
return number
def card_suit(self, card):
suit = card[-1]
return suit
答案 2 :(得分:0)
以这种方式更新代码,
class Card:
def __init__(self, card):
self.card = card
self.number = self.card_number(card)
self.suit = self.card_suit(card)
@staticmethod
def card_number(card):
# card = self.card
number = card[0]
return number
@staticmethod
def card_suit(card):
# card = self.card
suit = card[-1]
return suit
class Rules(Card):
def __init__(self, last_card, card):
Card.__init__(self, card)
self.last_card = last_card
self.last_card_number = self.card_number(self.last_card)
self.last_card_suit = self.card_suit(self.last_card)