Polymorphism and polymorphism like Python basics

Source: Internet
Author: User
Tags python decorator

Many people like to confuse polymorphism with polymorphism, and then think about the solution, in fact, as long as the separate look, it will be very clear.

One polymorphic:

Polymorphism refers to a class of things that have many forms, (an abstract class has more than one subclass, so the concept of polymorphism relies on inheritance)

    1. There are several patterns of sequence types: string, list, tuple
    2. Animals have many forms: man, dog, pig
1234567891011121314151617181920212223242526 #多态:同一种事物的多种形态,动物分为人类,猪类(在定义角度)classAnimal:    def run(self):        raiseAttributeError(‘子类必须实现这个方法‘)classPeople(Animal):    defrun(self):        print(‘人正在走‘)classPig(Animal):    defrun(self):        print(‘pig is walking‘)classDog(Animal):    defrun(self):        print(‘dog is running‘)peo1=People()pig1=Pig()d1=Dog()peo1.run()pig1.run()d1.run()

1234567891011121314151617 importabcclassAnimal(metaclass=abc.ABCMeta): #同一类事物:动物    @abc.abstractmethod    deftalk(self):        passclassPeople(Animal): #动物的形态之一:人    deftalk(self):        print(‘say hello‘)classDog(Animal): #动物的形态之二:狗    deftalk(self):        print(‘say wangwang‘)classPig(Animal): #动物的形态之三:猪    deftalk(self):        print(‘say aoao‘)

There are several forms of files: files, text files, executable files

12345678910111213 importabcclassFile(metaclass=abc.ABCMeta): #同一类事物:文件    @abc.abstractmethod    defclick(self):        passclassText(File): #文件的形态之一:文本文件    defclick(self):        print(‘open file‘)classExeFile(File): #文件的形态之二:可执行文件    defclick(self):        print(‘execute file‘)
Two polymorphism

(1) What is polymorphism (note: Polymorphism and polymorphism are two concepts)

Polymorphism refers to functions with different functions that can use the same function name, so that functions with different contents can be called with a function name. In an object-oriented approach, it is common to express polymorphism: sending the same message to different objects, and different objects having 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.

12345678910111213141516171819202122 #多态性:一种调用方式,不同的执行效果(多态性)deffunc(obj):    obj.run()func(peo1)func(pig1)func(d1)# peo1.run()# pig1.run()# 多态性依赖于:继承##多态性:定义统一的接口,deffunc(obj): #obj这个参数没有类型限制,可以传入不同类型的值    obj.run() #调用的逻辑都一样,执行的结果却不一样func(peo1)func(pig1)func(d1)

1.

2.

123456789101112 >>> deffunc(animal): #参数animal就是对态性的体现...     animal.talk()...>>> people1=People() #产生一个人的对象>>> pig1=Pig() #产生一个猪的对象>>> dog1=Dog() #产生一个狗的对象>>> func(people1)say hello>>> func(pig1)say aoao>>> func(dog1)say wangwang

3.

123456789 >>>  def  func (f): ...     F.click () ... >>> T1 = text () >>> E1 = exefile () >>> func (T1) open  file >>> func (E1) execute  file

It can be said that polymorphism is an interface (function Func), a variety of implementations (such as F.click ())

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) Increased 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 scalability of the program
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

1234567891011121314 >>> classCat(Animal): #属于动物的另外一种形态:猫...     deftalk(self):...         print(‘say miao‘)...>>> deffunc(animal): #对于使用者来说,自己的代码根本无需改动...     animal.talk()...>>> cat1=Cat() #实例出一只猫>>> func(cat1) #甚至连调用方式也无需改变,就能调用猫的talk功能say miao‘‘‘这样我们新增了一个形态Cat,由Cat类产生的实例cat1,使用者可以在完全不需要修改自己代码的情况下。使用和人、狗、猪一样的方式调用cat1的talk方法,即func(cat1)‘‘‘

Note:

Polymorphism: Multiple forms of the same thing, animals divided into humans, pigs (in the definition of angle) Polymorphism: A method of invocation, different execution effect (polymorphism)

Polymorphism and polymorphism like Python basics

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.