External Exam Download Resources Web Applications Games Recycle Bin

Document Types

The theory embedded in the source code below:
  1. Encapsulation - grouping, hiding or protecting methods or variables
  2. Abstraction - hiding implementation details from the user, e.g.: self.somethingElse = AnotherClass.someOtherMethod("who knows")
  3. Polymorphism - create an abstract or virtual implementation, which must be redefined
  4. Polymorphism - assign a different meaning or usage to something in different contexts
  5. Inheritance - the child class inherits this variable from the parent
  6. Recursion - a function that calls itself

doc types.py

class Document:
  def __init__(self, filename):
      
      #!!!!!!!!
      #Encapsulation - grouping, hiding or protecting methods or variables:
      self.publicVariable = 5
      self._protectedVariable = filename
      self.__privateVariable = "hidden from child class with the __"

      #!!!!!!!!
      #Abstraction - hiding implementation details from the user (dont try and run this next line):
      #self.somethingElse = AnotherClass.someOtherMethod("who knows")

  #!!!!!!!!
  #Polymorphism - create an abstract or virtual implementation, which must be redefined:
  def show(self):
      raise NotImplementedError("Subclass must implement abstract method")

class Pdf(Document):

  #!!!!!!!!
  #Polymorphism - assign a different meaning or usage to something in different contexts:
  def show(self):
      
      #!!!!!!!!
      #Inheritance - the child class inherits this variable from the parent:
      return self._protectedVariable

class Word(Document):
  def show(self):
      return 'i refuse to participate in this rubbish class activity'

  #!!!!!!!!
  #Recursion: a function that calls itself:
  def playRecursiveGame(self, X, Y=None):
      if Y==None:
          Y=self.publicVariable
      if X < Y:
          print("your number:", X, "my number:", Y, "..haha i always win!")
      else:
          #!!!!!!!!
          #Recursion: this is the recursive function call:
          print("your number:", X, "my number:", Y)
          return self.playRecursiveGame(X-1, Y+1)

  
#-----------MAIN:
documents = [Pdf('Document1'),
           Pdf('Document2'),
           Word('Document3')]
anotherDoc = Word('Document4')

for document in documents:
  print(document.show())

anotherDoc.playRecursiveGame(21)