Last active
July 29, 2017 15:02
-
-
Save cdennison/a2dc54c3d71c82133aa32c911b28d6ae to your computer and use it in GitHub Desktop.
Polymorphism examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Document: | |
def __init__(self, name): | |
self.name = name | |
def show(self): | |
raise NotImplementedError("Subclass must implement abstract method") | |
class Pdf(Document): | |
def show(self): | |
return 'Show pdf contents!' | |
class Word(Document): | |
def show(self): | |
return 'Show word contents!' | |
documents = [Pdf('Document1'), | |
Pdf('Document2'), | |
Word('Document3')] | |
print(type(documents)) | |
for document in documents: | |
print (document.name + ': ' + document.show(),type(document)) | |
<class 'list'> | |
Document1: Show pdf contents! <class '__main__.Pdf'> | |
Document2: Show pdf contents! <class '__main__.Pdf'> | |
Document3: Show word contents! <class '__main__.Word'> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment