Object-oriented state as much python

Polymorphism

Polymorphism refers to a class of things have a variety of forms

There are a variety of animal forms: human, dog, pig

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')

File has a variety of 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

What is a multi-state dynamic binding (when used in the context of inheritance, sometimes called polymorphisms)

Polymorphism refers to the use examples without considering Examples of types of

In object-oriented approach is generally expressed polymorphism:
send the same message to a different object (!!! obj.func (): func is called obj method, also known as a message sent to the func obj) , when receiving different objects will have different behavior (i.e., method).
That is, each object can be on your way to respond to a common message. The so-called news is call the function, different behavior refers to different implementations that perform different functions.

For example: The teacher bell rang (), student bell rang (), a teacher from work is performed by the operation, performed by the students after school operations, although the two messages the same, but different effects of the implementation of

Polymorphism

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

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

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

Ducks type

Tease than time:

  Python advocating duck type, that is, 'If it looks like, sounds like and walks like a duck, then it is 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, the object can inherit

You can also create a look and act like, but it has no relationship with the new object, which is typically used to save the program of loosely coupled components.

Example 1: using a variety of criteria defined in the library 'with files like' objects, although these works objects like files, but they did not inherit the built-in file object methods

Example 2: There are several types of sequence forms: strings, lists, tuples, but they have no direct inheritance relationship directly

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

    def write(self):
        pass

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

Guess you like

Origin www.cnblogs.com/Hybb/p/11518951.html