The so-called polymorphism : The type at definition time is not the same as the type at run time , This is called polymorphism , The concept of polymorphism is applied to Java and C# In this class of strongly typed languages , and Python advocate “ Duck type ”.

Duck type : Although I want one " duck ", But you gave me a bird . But as long as the bird walks like a duck , It sounds like a duck , Swimming is like a duck , I think it's a duck .

Python Polymorphism of , It's the weakening type , The key is whether the object parameters have specified properties and methods , If there is, consider it appropriate , It doesn't care if the object is of the right type .
Python Pseudo code implementation Java or C# Polymorphism of
class F1(object): def show(self): print('') class S1(F1): def show(self)
: print('') class S2(F1): def show(self): print('')
# Because in Java or C# When defining function parameters in , The type of the parameter must be specified # In order to make Func Function can be executed S1 Object show method , It can also be implemented S2 Object show method ,#
So in def Func In the formal parameters of obj What is the type of S1 and S2 The parent of the F1# # The actual parameter passed in is :S1 Objects and S2 object def Func(F1 obj): ""
"Func Function needs to receive a F1 Type or F1 Types of subclasses """ print( s1_obj = S1() Func(s1_obj) #
stay Func Function S1 Class s1_obj, implement S1 Of show method , result s2_obj = S2() Func(s2_obj) #
stay Func Function Ss Class ss_obj, implement Ss Of show method , result
Let's understand it in a popular way : definition obj This variable is of type :F1 Types of , But in the real call Func Function is not necessarily passed F1 Class , It may be an instance object of its subclass ,
This situation is called polymorphism

Python “ Duck type ”
class F1(object): def show(self): print('') class S1(F1): def show(self)
: print('') class S2(F1): def show(self): print('') def Func(obj):
# python It's a weak type , That is, no matter what is passed ,obj Variables can point to it , There is no polymorphism ( It weakens the concept ) print(
s1_obj= S1() Func(s1_obj) s2_obj = S2() Func(s2_obj)
Get more information can be private letter Oh ~~

©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?