什么是多态:多态性一词意味着具有多种形式。在编程中, 多态意味着相同的函数名称(但签名不同)被用于不同的类型。
内置多态函数示例:
# Python program to demonstrate in-built poly-
# morphic functions
# len() being used for a string
print ( len ( "geeks" ))
# len() being used for a list
print ( len ([ 10 , 20 , 30 ]))
输出如下:
5
3
使用的已定义多态函数的示例:
# A simple Python function to demonstrate
# Polymorphism
def add(x, y, z = 0 ):
return x + y + z
# Driver code
print (add( 2 , 3 ))
print (add( 2 , 3 , 4 ))
输出如下:
5
9
类方法的多态性:
下面的代码显示python如何以相同的方式使用两种不同的类类型。我们创建一个for循环, 该循环遍历对象的元组。然后调用方法而不必担心每个对象是哪种类类型。我们假设这些方法实际上存在于每个类中。
class India():
def capital( self ):
print ( "New Delhi is the capital of India." )
def language( self ):
print ( "Hindi is the most widely spoken language of India." )
def type ( self ):
print ( "India is a developing country." )
class USA():
def capital( self ):
print ( "Washington, D.C. is the capital of USA." )
def language( self ):
print ( "English is the primary language of USA." )
def type ( self ):
print ( "USA is a developed country." )
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()
country. type ()
输出如下:
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
具有继承的多态性:
在Python中, 通过Polymorphism, 我们可以在子类中定义与父类中的方法同名的方法。在继承中, 子类从父类继承方法。但是, 可以修改从父类继承的子类中的方法。在从父类继承的方法不太适合子类的情况下, 这特别有用。在这种情况下, 我们将在子类中重新实现该方法。在子类中重新实现方法的过程称为
方法覆盖
.
class Bird:
def intro( self ):
print ( "There are many types of birds." )
def flight( self ):
print ( "Most of the birds can fly but some cannot." )
class sparrow(Bird):
def flight( self ):
print ( "Sparrows can fly." )
class ostrich(Bird):
def flight( self ):
print ( "Ostriches cannot fly." )
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()
obj_ost.intro()
obj_ost.flight()
输出如下:
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.
具有函数和对象的多态:
也可以创建一个可以接受任何对象的函数, 从而实现多态。在此示例中, 我们创建一个名为" func()"的函数, 该函数将使用一个我们称为" obj"的对象。尽管我们使用的是" obj"名称, 但是任何实例化的对象都可以在此函数中调用。接下来, 让函数使用我们传递给它的" obj"对象做些事情。在这种情况下, 让我们调用三种方法, 即capital(), language()和type(), 它们分别在"印度"和"美国"两个类别中定义。接下来, 让我们创建"印度"和"美国"类的实例化(如果我们还没有的话)。有了这些, 我们可以使用相同的func()函数调用它们的动作:
def func(obj):
obj.capital()
obj.language()
obj. type ()
obj_ind = India()
obj_usa = USA()
func(obj_ind)
func(obj_usa)
代码:
通过函数实现多态
class India():
def capital( self ):
print ( "New Delhi is the capital of India." )
def language( self ):
print ( "Hindi is the most widely spoken language of India." )
def type ( self ):
print ( "India is a developing country." )
class USA():
def capital( self ):
print ( "Washington, D.C. is the capital of USA." )
def language( self ):
print ( "English is the primary language of USA." )
def type ( self ):
print ( "USA is a developed country." )
def func(obj):
obj.capital()
obj.language()
obj. type ()
obj_ind = India()
obj_usa = USA()
func(obj_ind)
func(obj_usa)
输出如下:
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。