码迷,mamicode.com
首页 > 其他好文 > 详细

Inheritance

时间:2014-10-06 20:32:10      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

The language feature most often associated with object-oriented programming is inheritance. Inheritance is the ability to define a new class that is a modified version of an existing class. It is called ‘inheritance’ because the new class inherits the methods of the existing class. Extending this metaphor, the existing class is called the parent class and the new class is called the child.

As an example, let’s say we want a class to represent a ‘hand’, that is, the set of cards held by one player. A hand is similar to a deck: both are made up of a set of cards, and both require operations like adding and removing cards. A hand is also different from a deck; there are operations we want for hands don’t make sense for a deck. For example, in poker we might compare two hands to see which wins. In bridge, we might compute a score for a hand in order to make a bid. This relationship between classes – similar, but different – lends itself to inheritance.

The definition of a child class is like other class definitions, but the name of the parent class appears in parentheses. The definition indicates that Hand inherits from Deck; that means we can use methods like pop_card and add_card for Hands as well as Decks. Hand also inherits the init method from Deck, but it doesn’t really do what we want: instead of populating the hand with 52 new cards, the init method for hands should initialize cards with an empty list. If we provide an init method in the Hand class, it overrides the one in the Deck class:

class Hand(Deck):
    """represents a hand of playing cards"""

    def __init__(self,label=‘‘):
        self.cards = []
        self.label = label

bubuko.com,布布扣

The next natural step is to encapsulate this code in a method called move_cards inside class Deck:

def move_card(self,hand,num):
        for i in range(num):
            hand.add_card(self.pop_card())

move_cards takes two arguments, a Hand object and the number of cards to deal. It modifies both self and hand, and returns None.

In some games, cards are moved from one hand to another, or form a hand back to the deck. You can use move_cards for any of these operations: self can be either a Deck or a Hand, and hand, despite the name, can also be a Deck.

Write a Deck method called deal hands that takes two parameters, the number of hands and the number of cards per hand, and that creates new Hand objects, deals the appropriate number of cards per hand, and returns a list of Hand objects.

def deal_hands(self,num_hands,num_cards):
        hands = list()
        for i in range(num_hands):
            hand = Hand()
            self.move_card(hand,num_cards)
            hands.append(hand)
        return hands

bubuko.com,布布扣

 

from Thinking in Python

 

 

 

Inheritance

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/ryansunyu/p/4008521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!