Browse Source

new code example audiofile.py showing inhiterance with polymorphism

master
T. Meissner 10 years ago
parent
commit
f7d909bc6c
1 changed files with 32 additions and 0 deletions
  1. +32
    -0
      python_3_oop/chapter03/audiofile.py

+ 32
- 0
python_3_oop/chapter03/audiofile.py View File

@ -0,0 +1,32 @@
# small example for inhiterance with polymorphism
class AudioFile:
def __init__(self, filename):
if not filename.endswith(self.ext):
raise Exception("Invalid file format")
self.filename = filename
class MP3File(AudioFile):
ext = "mp3"
def play(self):
print("Playing {} as mp3".format(self.filename))
class WavFile(AudioFile):
ext = "wav"
def play(self):
print("Playing {} as wav".format(self.filename))
class OggFile(AudioFile):
ext = "ogg"
def play(self):
print("Playing {} as ogg".format(self.filename))

Loading…
Cancel
Save