Python polymorphic instances

Source: Internet
Author: User

python polymorphic instances

# Coding:utf-8
"""
Polymorphism (English: polymorphism), refers to object-oriented programs run, the same message may be sent to many different classes of objects,
and the system can be based on the class of the object, the corresponding class of methods, and have different behavior.
In simple terms, the so-called polymorphism means that the same message given to different objects can cause different actions to be called.
In object-oriented programming, polymorphism is generally referred to as subtype polymorphism (subtype polymorphism).

the definition above is a bit confusing for beginners, and Huanggo uses the "open" action to describe object-oriented polymorphism.
Open the door, open the window, open the book, and so on. "Open" This action, encounter different object doors, windows, books, there are different patterns of behavior.
this is polymorphic.
This article was trained by Huanggo Python, written by Huanggo
Huanggo python Remote video training course
Https://github.com/pythonpeixun/article/blob/master/index.md

Huanggo python Training preview video playback address
Https://github.com/pythonpeixun/article/blob/master/python_shiping.md
"""
# example 1


class Door (object):

def Open (self):
print "Open Door"


class Windows (object):

def Open (self):
print "Open Window"


class Book (object):

def Open (self):
print "Open book"

lst = [Door (), Windows (), book ()]

For item in LST:
Item.open ()

# example 2 general use of inheritance to illustrate polymorphic examples


class Animal:

def __init__ (self, name):
self.name = name

def talk (self):
Raise Notimplementederror ("subclass must implement abstract method")


class Cat (Animal):

def talk (self):
return ' meow! '


class Dog (Animal):

def talk (self):
return ' woof! woof! '

animals = [Cat (' Missy '),
Cat (' Mr. Mistoffelees '),
Dog (' Lassie ')]

For animal in animals:
print Animal.name + ': ' + animal.talk ()

# example 3 python has many polymorphic applications built into it
# The same + number can be added to different objects, reflecting (similar: adding this thing) the polymorphic function.
Print 1 + 2
print "Hello" + ' brother Huang '

# The Len function passes different parameters and also embodies the polymorphic function.
print len ("Huanggo python training")
Print Len ([2, 4, 5, 7])

# Engineering Applications
# A simple logging function, implemented by judgment, is implemented by the object-oriented polymorphism.
#如果有大量的判断语句, you can use polymorphism to achieve it.


def log_msg (log_type):
msg = ' Operation successful '
if Log_type = = ' file ':
log_file.write (msg)
elif Log_type = = ' database ':
Cursor.execute (' INSERT into log_table (msg) VALUES ('? ') ', msg)

#重构


class FileLogger (object):

def log (self, msg):
log_file.write (msg)


class Dblogger (object):

def log (self, msg):
Cursor.execute (' INSERT into log_table (msg) VALUES ('? ') ', msg)


def log_msg (obj):
msg = ' Operation successful '
Obj.log (msg)


Python polymorphic instances

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.