summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LectureNotesOnPython.rst157
1 files changed, 155 insertions, 2 deletions
diff --git a/LectureNotesOnPython.rst b/LectureNotesOnPython.rst
index 6abf332..0b0c4f2 100644
--- a/LectureNotesOnPython.rst
+++ b/LectureNotesOnPython.rst
@@ -1858,7 +1858,18 @@ Positional argument. Keyword argument.
继承(inheritance)
``````````````````````````````````````````````````
-继承父类中的属性与方法。
+继承用于从父类(parent)中继承属性与方法,创建一个子类 (child)。 如果
+子类中的方法与父类中的方法同名, 则在子类实例中用子类定义的方法
+(overrride)。 利于代码重用。 缺点是代码不易读懂。 并不是非要代码重用
+不可。
+
+类关系图(class diagram)。 两类关系, HAS-A 与 IS-A 。
+
+HAS-A 。 长方形中有点。
+
+IS-A 。 一手牌是一副牌。
+
+
.. code:: python
@@ -1885,12 +1896,154 @@ Positional argument. Keyword argument.
-扑克牌。Card, Deck, Hand(Deck)。
+扑克牌。Card 类, Deck 类, Hand(Deck) 类继承 Deck 类。 Hand 是一手牌, 可以用 Deck 中的拿牌与出牌。
+
+
+类属性(class attribute)与实例属性(instance attribute)。
+
+.. code:: python
+
+ """This module contains a code example related to
+
+ Think Python, 2nd Edition
+ by Allen Downey
+ http://thinkpython2.com
+
+ Copyright 2015 Allen Downey
+
+ License: http://creativecommons.org/licenses/by/4.0/
+ """
+
+
+ import random
+
+
+ class Card:
+ """Represents a standard playing card.
+
+ Attributes:
+ suit: integer 0-3
+ rank: integer 1-13
+ """
+
+ suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]
+ rank_names = [None, "Ace", "2", "3", "4", "5", "6", "7",
+ "8", "9", "10", "Jack", "Queen", "King"]
+
+ def __init__(self, suit=0, rank=2):
+ self.suit = suit
+ self.rank = rank
+
+ def __str__(self):
+ """Returns a human-readable string representation."""
+ return '%s of %s' % (Card.rank_names[self.rank],
+ Card.suit_names[self.suit])
+
+ def __eq__(self, other):
+ """Checks whether self and other have the same rank and suit.
+
+ returns: boolean
+ """
+ return self.suit == other.suit and self.rank == other.rank
+
+ def __lt__(self, other):
+ """Compares this card to other, first by suit, then rank.
+
+ returns: boolean
+ """
+ t1 = self.suit, self.rank
+ t2 = other.suit, other.rank
+ return t1 < t2
+
+
+ class Deck:
+ """Represents a deck of cards.
+
+ Attributes:
+ cards: list of Card objects.
+ """
+
+ def __init__(self):
+ """Initializes the Deck with 52 cards.
+ """
+ self.cards = []
+ for suit in range(4):
+ for rank in range(1, 14):
+ card = Card(suit, rank)
+ self.cards.append(card)
+
+ def __str__(self):
+ """Returns a string representation of the deck.
+ """
+ res = []
+ for card in self.cards:
+ res.append(str(card))
+ return '\n'.join(res)
+
+ def add_card(self, card):
+ """Adds a card to the deck.
+
+ card: Card
+ """
+ self.cards.append(card)
+
+ def remove_card(self, card):
+ """Removes a card from the deck or raises exception if it is not there.
+
+ card: Card
+ """
+ self.cards.remove(card)
+
+ def pop_card(self, i=-1):
+ """Removes and returns a card from the deck.
+
+ i: index of the card to pop; by default, pops the last card.
+ """
+ return self.cards.pop(i)
+
+ def shuffle(self):
+ """Shuffles the cards in this deck."""
+ random.shuffle(self.cards)
+
+ def sort(self):
+ """Sorts the cards in ascending order."""
+ self.cards.sort()
+
+ def move_cards(self, hand, num):
+ """Moves the given number of cards from the deck into the Hand.
+
+ hand: destination Hand object
+ num: integer number of cards to move
+ """
+ for i in range(num):
+ hand.add_card(self.pop_card())
+
+
+ class Hand(Deck):
+ """Represents a hand of playing cards."""
+
+ def __init__(self, label=''):
+ self.cards = []
+ self.label = label
+
+
+ if __name__ == '__main__':
+ deck = Deck()
+ deck.shuffle()
+
+ hand = Hand()
+
+ deck.move_cards(hand, 5)
+ hand.sort()
+ print(hand)
+
+
web 应用程序
````````````
+
在文本框输入文本
``````````````````````````````````````````````````