External Exam Download Resources Web Applications Games Recycle Bin

Card Deck

  1. OOP = Object-oriented programming = programming paradigm. some definitions:
    1. class = blueprint in code (e.g. a bicycle class might have a "speed" variable and a speedUp() method)
    2. object = instance of a class (e.g. Ronnie's bike)
    3. abstraction = means hiding what isn't important when working with classes / OOP
    4. encapsulation = techniques to bundle and hide methods and properties within classes (e.g. using public and private keywords in C++). this is one way to enable abstraction
    5. inheritance = when new objects to take on the properties of existing objects (e.g. a circle object inherits the properties of a shape object)
    6. polymorphism = ability for an object to take on many forms (e.g. a method draw() could be used with triangles, stars and boxes)

class.py

class Card:
  deckCounter = 0

  def __init__(self, title, suit):
      self.title = title
      self.suit = suit
      Card.deckCounter += 1

  def flipCard(self):
      print(self.title, "of", self.suit)
  
card1 = Card("Ace", "Spades")
card2 = Card("Seven", "Diamonds")

card1.flipCard()
card2.flipCard()

print(Card.deckCounter, "cards in deck")


override methods.py

#Joker overrides parent class methods:

class Card:
    deckCounter = 0

    def __init__(self, title, suit):
        self.title = title
        self.suit = suit
        Card.deckCounter += 1

    def flipCard(self):
        print(self.title, "of", self.suit)

class Joker(Card):
    def __init__(self):
        Card.deckCounter += 1

    def flipCard(self):
        print("Joker")
  
card1 = Card("Ace", "Spades")
card2 = Card("Seven", "Diamonds")
card3 = Joker()
card1.flipCard()
card2.flipCard()
card3.flipCard()
print(Card.deckCounter, "cards in deck")