Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet81/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   77   78   79   80   81   82   83   84   ...   183
Bog'liq
python3-oop

[
 75 
]
Polymorphism
We were introduced to polymorphism in 
Chapter 1

Object-oriented Design
. It is a 
fancy name describing a simple concept: different behaviors happen depending on 
which subclass is being used, without having to explicitly know what the subclass 
actually is. As an example, imagine a program that plays audio files. A media player 
might need to load an 
AudioFile
object and then 
play
it. We'd put a 
play()
method 
on the object, which is responsible for decompressing or extracting the audio and 
routing it to the sound card and speakers. The act of playing an 
AudioFile
could 
feasibly be as simple as:
audio_file.play()
However, the process of decompressing and extracting an audio file is very different 
for different types of files. The 
.wav
files are stored uncompressed, while 
.mp3

.wma

and 
.ogg
files all have totally different compression algorithms.
We can use inheritance with polymorphism to simplify the design. Each type of 
file can be represented by a different subclass of 
AudioFile
, for example, 
WavFile

MP3File
. Each of these would have a 
play()
method, but that method would be 
implemented differently for each file to ensure the correct extraction procedure is 
followed. The media player object would never need to know which subclass of 
AudioFile
it is referring to; it just calls 
play()
and polymorphically lets the object 
take care of the actual details of playing. Let's look at a quick skeleton showing how 
this might look:
class AudioFile:
def __init__(self, filename):
if not filename.endswith(self.ext):
raise Exception("Invalid file format")
self.filename = filename
class MP3File(AudioFile):

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   77   78   79   80   81   82   83   84   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish