The polymorphism and polymorphism of python--object-oriented

Source: Internet
Author: User

One, polymorphic

Polymorphism refers to a class of things that have many forms

Animals have many forms: man, dog, pig

ImportABCclassAnimal (METACLASS=ABC. Abcmeta):#The same kind of things: animals@abc. AbstractmethoddefTalk (self):PassclassPeople (Animal):#one of the forms of animals: Man    defTalk (self):Print('Say hello')classDog (Animal):#Animal Form Two: the Dog    defTalk (self):Print('say Wangwang')classPig (Animal):#animal Form three: Pig    defTalk (self):Print('say Aoao')

There are several forms of files: text files, executables

ImportABCclassFile (METACLASS=ABC. Abcmeta):#Same Kind of thing: file@abc. AbstractmethoddefClick (self):PassclassText (File):#one of the forms of the file: text file    defClick (self):Print('Open File')classExefile (File):#form Two of the file: executable file    defClick (self):Print('Execute File')
Ii. polymorphism

1. What is a polymorphic dynamic binding (sometimes polymorphic when used in an inherited context)

Polymorphism refers to the use of instances without regard to instance types

In the object-oriented approach, it is generally said that polymorphism: sending the same message to different objects (!!!) Obj.func (): Is the method func that called obj, also known as a message Func sent to obj, and different objects have different behavior (that is, methods) when they are received. In other words, each object can respond to a common message in its own way. The so-called message, is called the function, the different behavior refers to the different implementation, namely executes the different function. For example: Teacher. The bell rang (), the student. The bell rang (), the teacher performed the off-duty operation, the students performed the school operation, although the two messages, but the effect of the implementation of different
Detailed explanation

Polymorphism is divided into static polymorphism and dynamic polymorphism

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

Dynamic polymorphism: The following

peo=people () dog=Dog () pig=Pig ()#peo, dog, pig are all animals, as long as the animal must have talk method  # So we don't have to think about what the specific type of the three are, but the direct use of Peo.talk () dog.talk () Pig.talk()# goes Further, We can define a unified interface to use def  func (obj):    obj.talk ()

2. Why use

The polymorphism and polymorphism of python--object-oriented

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.