我试图在python3中定义和实例化一个子类,但是当在子类上调用一个方法时,它会抛出一个AttributeError,就像我将对象实例化为超类的一个实例一样;该方法无法找到。据我所知,我已经适当地编写了子类构造函数,调用了没有参数的super()方法,如图所示。任何帮助将不胜感激。
用于实例化子类和调用方法的代码:
class Player:
hands = []
...
def __init__(self):
...
def __deal(self, twocards):
self.hands = [Hand(showfirst=True)]
self.hands[0] += twocards
self.hands[0].showcards()
...
手类的定义:
class Hand(pydealer.Stack):
def __init__(self, showfirst, **kwargs):
super().__init__(**kwargs)
stay = False
self.showfirstcard = showfirst
...
def showcards(self):
for i, card in enumerate(self):
if i == 0:
if self.showfirstcard == False:
print('***FACEDOWN CARD***')
else:
print(card)
Stacktrace和错误消息:
Traceback (most recent call last):
File "/root/python/django_webapp/mysite/webapp/blockjack/src/test.py", line 8, in <module>
table.start()
File "/root/python/django_webapp/mysite/webapp/blockjack/src/table.py", line 23, in start
self.dealer.deal(self.shoe.deal(2))
File "/root/python/django_webapp/mysite/webapp/blockjack/src/dealer.py", line 15, in deal
self.seecards(0)
File "/root/python/django_webapp/mysite/webapp/blockjack/src/player.py", line 39, in seecards
self.hands[whichhand].showcards()
AttributeError: 'Stack' object has no attribute 'showcards'
TL; DR:当我使用Hand()显式定义有问题的对象时,为什么我认为我试图在父类对象(Stack)上调用方法?
编辑:添加了以下未删除的所有相关代码:
test.py
import table
from pydealer import Deck
table = table.Table()
table.start()
table.py
from pydealer import Deck
from player import Player
from dealer import Dealer
class Table:
shoe = None
dealer = None
player = None
def __init__(self):
self.shoe = Deck()
self.shoe.rebuild = True
self.shoe.shuffle()
self.player = Player()
self.dealer = Dealer()
def start(self):
self.player.bet()
self.dealer.deal(self.shoe.deal(2))
self.player.deal(self.shoe.deal(2))
dealer.py
from player import Player
from hand import Hand
class Dealer(Player):
def __init__(self):
super().__init__()
def deal(self, twocards):
self.hands = [Hand(showfirst=False)]
self.hands[0] += twocards
self.seecards(0)
player.py
from hand.py import Hand
class Player:
hands = []
__chips = None
__betamount = None
def __init__(self):
self.__chips = 5000
def bet(self):
type(self.__betamount)
def __deal(self, twocards):
self.hands = [Hand(showfirst=True)]
self.hands[0] += twocards
self.hands[0].showcards()
def hit(self, card, whichhand):
self.hands[whichhand] += card
def stay(self, whichhand):
self.hands[whichhand].setstay()
def printscore(self, whichhand):
print(self.hands[whichhand].stackscore())
hand.py
import pydealer
class Hand(pydealer.Stack):
stay = None
showfirstcard = None
bust = None
def __init__(self, showfirst, **kwargs):
super().__init__(**kwargs)
stay = False
self.showfirstcard = showfirst
def stackscore(self):
total = 0
for card in self:
total += self.__bjval(card)
if total > 21:
for card in self:
if card.value == "Ace":
total -= 10
if total <= 21:
break
return total
def __bjval(self, card):
return {
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'Jack': 10,
'Queen': 10,
'King': 10,
'Ace':11
}[card.value]
#def receive(self, cards):
#self += cards
def setstay(self):
self.stay = True
def getsize(self):
return self.size()
def showcards(self):
for i, card in enumerate(self):
if i == 0:
if self.showfirstcard == False:
print('***FACEDOWN CARD***')
else:
print(card)
答案 0 :(得分:0)
想出来。该对象未被实例化为Stack,但实际上是在+ =运算符的结果上转换为堆栈。此运算符的代码实例化并返回新的堆栈对象,并且对Hand对象的引用完全丢失。在stack.py中:
def __add__(self, other):
try:
new_stack = Stack(cards=(list(self.cards) + list(other.cards)))
except:
new_stack = Stack(cards=(list(self.cards) + other))
return new_stack
我想唯一的另一种方法是让Hand类包含一个Stack,因为以这种方式覆盖超类的基本功能似乎完全破坏了继承的目的?