python--polymorphism

Source: Internet
Author: User

The "" "polymorphic feature is that invoking different subclasses will produce different behaviors without having to know exactly what the subclass actually is," "Class audiofile:    def __init__ ( Self, filename):         if not filename.endswith ( Self.ext):  #检测来自子类的ext变量是否以按指定的格式结尾, if not, throws an exception              raise exception ("Invalid file format")          self.filename = filenameclass mp3file (AudioFile):    ext =  "MP3"     def play (self):         print ("Play  %s music ... " % self.filename) Class wavfile (AudioFile):     ext  =  "WAV"     def play (self):         Print ("Play %s music ..."  % self.filename) class oggfile (AudioFile):     ext =  "Ogg"    def play (self):         print (" play  %s music ... " % self.filename) if __name__ ==  ' __main__ ':     Ogg = oggfile ("Myfile.ogg")     ogg.play ()     mp3 =  mp3file ("Myfile.mp3")     mp3.play ()     wav = wavfile (" Myfile.wav ")     wav.play ()      #看起来好像都是调用了同一个play () method, but the functions implemented are different


python--polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.