#对象的组合classCard(object):"""A playing card."""
RANKS =["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
SUIT =["c","d","h","s"]def__init__(self,rank,suit):
self.rank = rank
self.suit = suit
def__str__(self):
rep = self.rank + self.suit
return rep
classHand(object):"""A hand of playing cards."""def__init__(self):
self.cards =[]def__str__(self):if self.cards:
rep =""for card in self.cards:
rep +=str(card)+" "else:
rep ="<empty>"return rep
defclear(self):
self.cards =[]defadd(self,card):
self.cards.append(card)defgive(self,card,other_hand):
self.cards.remove(card)
other_hand.add(card)
card1 = Card(rank ="A",suit ="c")print("Printing a card object:")print(card1)
card2 = Card(rank ="2",suit ="c")
card3 = Card(rank ="3",suit ="c")
card4 = Card(rank ="4",suit ="c")
card5 = Card(rank ="5",suit ="c")print("\nPrinting the rest of the objects invidually:")print(card2)print(card3)print(card4)print(card5)
my_hand = Hand()print("\nPrinting my hand I add any card:")print(my_hand)
my_hand.add(card1)
my_hand.add(card2)
my_hand.add(card3)
my_hand.add(card4)
my_hand.add(card5)print("\nPrinting my hand before I add any cards:")print(my_hand)
your_hand =Hand()
my_hand.give(card1,your_hand)
my_hand.give(card2,your_hand)print("\nGave the first two cards from my hands to your hand.")print("Your hand:")print(your_hand)print("My hand:")print(my_hand)
my_hand.clear()print("\nMy hand after clearing it:")print(my_hand)
Printing a card object:
Ac
Printing the rest of the objects invidually:
2c
3c
4c
5c
Printing my hand I add any card:
<empty>
Printing my hand before I add any cards:
Ac 2c 3c 4c 5c
Gave the first two cards from my hands to your hand.
Your hand:
Ac 2c
My hand:
3c 4c 5c
My hand after clearing it:
<empty>
#通过继承扩展类classCard(object):"""A playing card."""
RANKS =["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
SUITS =["c","d","h","s"]def__init__(self,rank,suit):
self.rank = rank
self.suit = suit
def__str__(self):
rep = self.rank + self.suit
return rep
classHand(object):"""A hand of playing cards."""def__init__(self):
self.cards =[]def__str__(self):if self.cards:
rep =""for card in self.cards:
rep +=str(card)+" "else:
rep ="<empty>"return rep
defclear(self):
self.cards =[]defadd(self,card):
self.cards.append(card)defgive(self,card,other_hand):
self.cards.remove(card)
other_hand.add(card)classDeck(Hand):#Deck拥有Hand的所有方法"""A deck of playing cards."""defpopulate(self):for suit in Card.SUITS:for rank in Card.RANKS:
self.add(Card(rank,suit))defshuffle(self):import random
random.shuffle(self.cards)defdeal(self,hands,per_hand =1):for rounds inrange(per_hand):for hand in hands:if self.cards:
top_card = self.cards[0]
self.give(top_card,hand)else:print("Can't continue deal.out of cards!")
deck1 = Deck()print("Created a new deck.")print("Deck:")print(deck1)
deck1.populate()print("\nPoulated the deck.")print("Deck:")print(deck1)
deck1.shuffle()print("\nShuffled the deck.")print("Deck:")print(deck1)
my_hand = Hand()
your_hand = Hand()
hands =[my_hand,your_hand]
deck1.deal(hands,per_hand =5)print(my_hand)print(your_hand)print(deck1)
Created a new deck.
Deck:
<empty>
Poulated the deck.
Deck:
Ac 2c 3c 4c 5c 6c 7c 8c 9c 10c Jc Qc Kc Ad 2d 3d 4d 5d 6d 7d 8d 9d 10d Jd Qd Kd Ah 2h 3h 4h 5h 6h 7h 8h 9h 10h Jh Qh Kh As 2s 3s 4s 5s 6s 7s 8s 9s 10s Js Qs Ks
Shuffled the deck.
Deck:
9s 5h 10h 7h 10s 2s 10c 7d Qc Jd 4s Kc 7s Qh 2d 8c Ad Ac As 3h 2c 10d 6s 5d Jc 9d 2h 6h 9h Ks 8s 7c 3d 8h Js 4d Kd 8d 6d 5s Qs 5c 3s 6c 9c Jh 4h Ah Kh 3c 4c Qd
9s 10h 10s 10c Qc
5h 7h 2s 7d Jd
4s Kc 7s Qh 2d 8c Ad Ac As 3h 2c 10d 6s 5d Jc 9d 2h 6h 9h Ks 8s 7c 3d 8h Js 4d Kd 8d 6d 5s Qs 5c 3s 6c 9c Jh 4h Ah Kh 3c 4c Qd
#改变继承方法的行为——重写classCard(object):"""A playing card"""
RANKS =["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
SUITS =["c","d","h","s"]def__init__(self,rank,suit):
self.rank = rank
self.suit = suit
def__str__(self):
rep = self.rank + self.suit
return rep
#重写基类方法 classUnprintable_Card(Card):"""A Card that won't reveal its rank or suit when printed."""def__str__(self):return"<unprintable>"#调用基类函数superclassPositionable_Card(Card):"""A Card that can be face up or face down"""def__init__(self,rank,suit,face_up =True):super(Positionable_Card,self).__init__(rank,suit)#super函数调用基类的方法
self.is_face_up = face_up
def__str__(self):if self.is_face_up:
rep =super(Positionable_Card,self).__str__()else:
rep ="XX"return rep
deffilp(self):
self.is_face_up =not self.is_face_up
card1 = Card("A","c")
card2 = Unprintable_Card("A","c")
card3 = Positionable_Card("A","h")print(card1)print(card2)print(card3)
card3.filp()print(card3)