对象组合,扩展类,方法重写

#对象的组合
class Card(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
class Hand(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
    def clear(self):
        self.cards = []
    def add(self,card):
        self.cards.append(card)
    def give(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>
#通过继承扩展类
class Card(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
class Hand(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
    def clear(self):
        self.cards = []
    def add(self,card):
        self.cards.append(card)
    def give(self,card,other_hand):
        self.cards.remove(card)
        other_hand.add(card)

class Deck(Hand): #Deck拥有Hand的所有方法
    """A deck of playing cards."""
    def populate(self):
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                self.add(Card(rank,suit))
    def shuffle(self):
        import random
        random.shuffle(self.cards)
    def deal(self,hands,per_hand = 1):
        for rounds in range(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 
#改变继承方法的行为——重写
class Card(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    
#重写基类方法   
class Unprintable_Card(Card):
    """A Card that won't reveal its rank or suit when printed."""
    def __str__(self):  
        return "<unprintable>"
#调用基类函数super
class Positionable_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
    def filp(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)
Ac
<unprintable>
Ah
XX

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值