Python Object-oriented: polymorphism and polymorphism

Source: Internet
Author: User

Polymorphic

Polymorphism refers to a class of things that have many forms, such as

Animals have many forms: man, dog, pig

Import Abcclass Animal (METACLASS=ABC. Abcmeta): #同一类事物: Animal    @abc. Abstractmethod    def talk (self):        passclass People (Animal): #动物的形态之一: People    def Talk (self):        print (' Say hello ') class Dog (Animal): #动物的形态之二: The dog    def talk (self):        print (' Say Wangwang ') class Pig (Animal): #动物的形态之三: Pig    def talk (self):        print (' Say Aoao ')

  There are several forms of files: text files, executables

Import Abcclass File (METACLASS=ABC. Abcmeta): #同一类事物: File    @abc. Abstractmethod    def click (self):        passclass text (file): #文件的形态之一: Text file    def Click (self):        print (' Open file ') class Exefile (file): #文件的形态之二: Executable    def click (self):        print (' Execute file ' ‘)

  

Polymorphism

I. What is polymorphic dynamic binding (sometimes called polymorphism when used in an inherited context)

Polymorphism refers to the use of instances without regard to instance types, and polymorphism is classified as static polymorphism and dynamic polymorphism.

Static polymorphism: If any type can be operated with operator +

Dynamic polymorphism is as follows:

Peo=people () Dog=dog () Pig=pig () #peo, dog, pig are animals, as long as the animal must have talk method # so we can not consider the specific type of the three, and directly use Peo.talk () Dog.talk () Pig.talk () #更进一步, we can define a unified interface to use def func (obj):    obj.talk ()

  

Second, why to use polymorphism (polymorphism of the benefits)

In fact, we can see from the above polymorphism example, we do not add the above new knowledge, that is, Python itself is to support polymorphism, so what is the advantage of doing so?

1, increase the flexibility of the program

 Status quo, regardless of the ever-changing object, the user is the same form to invoke, such as func (animal)

2. Increased program Extensibility

By inheriting the animal class, a new class is created, and the user does not have to change their own code or use Func (animal) to invoke the

>>> class Cat (Animal): #属于动物的另外一种形态: Cat     ... Def talk (self): ...         Print (' Say Miao ') ... >>> def func (animal): #对于使用者来说, your code doesn't have to be changed     at all ... Animal.talk () ... >>> cat1=cat () #实例出一只猫 >>> func (CAT1) #甚至连调用方式也无需改变, you can call the cat talk function say Miao " In this way we have added a new form of cat, which is generated by the cat class CAT1, and the user can do so without having to modify their own code at all. Call Cat1 's Talk method in the same way as humans, dogs, and pigs, called Func (CAT1) "

  

Duck type

Tease moment:

Python advocates duck type, that is, ' if it looks like, call the pan and walk like a duck, then it's a duck '

Python programmers typically write programs based on this behavior, such as: If you want to write a custom version of an existing object, you can inherit the object, or you can create a new object that looks and behaves like, but has nothing to do with it, which is typically used to preserve the loose coupling of program components.

Example 1: Use the various ' file-like ' objects defined in the standard library, although they work like files, but they do not inherit the method of the built-in file object

#二者都像鸭子, both look like files, so you can use the same class txtfile as a file:    def read:        pass    def write (self):        Passclass Diskfile:    def read (self):        pass    def write (self):        Pass

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

#str, list,tuple are sequence types s=str (' Hello ') l=list ([]) t=tuple ((4,5,6)) #我们可以在不考虑三者类型的前提下使用s, l,ts.__len__ () l.__len__ () t.__len__ () Len (s) Len (l) Len (t)

  

Python Object-oriented: polymorphism and polymorphism

Related Article

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.