First, Abstractmethod
- Subclasses must all implement the Abstractmethod method of overriding the parent class
- Non-Abstractmethod methods can be overridden without implementation
- A class with a Abstractmethod method cannot be instantiated
From ABC import Abstractmethod, Abcmeta
1 classBettingstrategy (metaclass=Abcmeta):2 3 @abstractmethod4 defbet (self):5 Print('0')6 7 defRecord_win (self):8 Print('win')9 Ten defRecord_loss (self): One Print('Loss') A - - classBet (bettingstrategy): the defbet (self): - Print('1')
Extension: ABC module
Second, Staticmethod: static function
An object can be called without instantiating a function
1 classHand4:2 def __init__(Self, Dealer_card, *cards):3Self.dealer_card =Dealer_card4Self.cards =Cards5 6 @staticmethod7 defFreeze (Other):8Hand = Hand4 (Other.dealer_card, *other.cards)9 returnHandTen One @staticmethod A defSplit (Other, card0, card1): -Hand0 =Hand4 (Other.dealer_card, other.cards[0], card0) -Hand1 = Hand4 (Other.dealer_card, other.cards[1], Card1) the returnhand0, Hand1 - - def __str__(self): - return ','. Join (Map (str, self.cards))
1 h41 = Hand4 (Deck3.pop (), Deck3.pop (), Deck3.pop ())2 s1, s2 = hand4.split (H41, Deck3.pop (), Deck3.pop ())3 s3 = Hand4.freeze (H41)
called
Python object-oriented programming--some class definitions (miscellaneous)