class polymorphism

polymorphism

Polymorphism refers to a class of things having multiple forms, such as

Animals come in many shapes: people, dogs, pigs

import abc
class Animal(metaclass=abc.ABCMeta): #同一类事物:动物
    @abc.abstractmethod
    def talk(self):
        pass

class People(Animal): #动物的形态之一:人
    def talk(self):
        print('say hello')

class Dog(Animal): #动物的形态之二:狗
    def talk(self):
        print('say wangwang')

class Pig(Animal): #动物的形态之三:猪
    def talk(self):
        print('say aoao')

Files come in many forms: text files, executable files

import abc
class File(metaclass=abc.ABCMeta): #同一类事物:文件
    @abc.abstractmethod
    def click(self):
        pass

class Text(File): #文件的形态之一:文本文件
    def click(self):
        print('open file')

class ExeFile(File): #文件的形态之二:可执行文件
    def click(self):
        print('execute file')

polymorphism

1. What is polymorphic dynamic binding (sometimes called polymorphism when used in the context of inheritance)

Polymorphism refers to the use of instances without regard to the instance type, and polymorphism is divided into static polymorphism and dynamic polymorphism

Static polymorphism: For example, any type can be operated with operator +

Dynamic polymorphism: as follows

peo=People()
dog=Dog()
pig=Pig()

#peo、dog、pig都是动物,只要是动物肯定有talk方法
#于是我们可以不用考虑它们三者的具体是什么类型,而直接使用
peo.talk()
dog.talk()
pig.talk()

#更进一步,我们可以定义一个统一的接口来使用
def func(obj):
    obj.talk()

2. Why use polymorphism (the benefits of polymorphism)

In fact, you can see from the above example of polymorphism that we have not added any new knowledge, that is to say, python itself supports polymorphism. What are the benefits of doing so?

1. Increased program flexibility

  In order to keep the same, regardless of the ever-changing object, the user calls it in the same form, such as func(animal)

2. Increased program scalability

 A new class is created by inheriting the animal class, and users do not need to change their own code, or use func(animal) to call   

>>> class Cat(Animal): #属于动物的另外一种形态:猫
...     def talk(self):
...         print('say miao')
... 
>>> def func(animal): #对于使用者来说,自己的代码根本无需改动
...     animal.talk()
... 
>>> cat1=Cat() #实例出一只猫
>>> func(cat1) #甚至连调用方式也无需改变,就能调用猫的talk功能
say miao

'''
这样我们新增了一个形态Cat,由Cat类产生的实例cat1,使用者可以在完全不需要修改自己代码的情况下。使用和人、狗、猪一样的方式调用cat1的talk方法,即func(cat1)
'''

duck type

Funny moment:

Python embraces duck typing, i.e. 'if it looks like, quacks and walks like a duck, then it's a duck'

Python programmers usually write programs based on this behavior. For example, if you want to write a custom version of an existing object, you can inherit from that object

It is also possible to create a completely new object that looks and behaves like, but has nothing to do with it, which is often used to preserve the loose coupling of program components.

Example 1: Take advantage of various 'file-like' objects defined in the standard library, although these objects work like files, they do not inherit the methods of the built-in file objects

#二者都像鸭子,二者看起来都像文件,因而就可以当文件一样去用
class TxtFile:
    def read(self):
        pass

    def write(self):
        pass

class DiskFile:
    def read(self):
        pass
    def write(self):
        pass

Example 2: Sequence types have many forms: strings, lists, tuples, but they have no direct inheritance relationship

#str,list,tuple都是序列类型
s=str('hello')
l=list([1,2,3])
t=tuple((4,5,6))

#我们可以在不考虑三者类型的前提下使用s,l,t
s.__len__()
l.__len__()
t.__len__()

len(s)
len(l)
len(t)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325224463&siteId=291194637