From f7d909bc6c3a58aeace785d9d9b924725dd408e5 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Mon, 1 Sep 2014 23:53:58 +0200 Subject: [PATCH] new code example audiofile.py showing inhiterance with polymorphism --- python_3_oop/chapter03/audiofile.py | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 python_3_oop/chapter03/audiofile.py diff --git a/python_3_oop/chapter03/audiofile.py b/python_3_oop/chapter03/audiofile.py new file mode 100644 index 0000000..d1856ba --- /dev/null +++ b/python_3_oop/chapter03/audiofile.py @@ -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)) \ No newline at end of file