Python中的多态性
什么是多态性? 多态性是指具有多种形式。多态性是一个编程术语,指的是使用相同的函数名称,但具有不同的签名,用于多种类型。
内置多态函数的示例:
# Python program for demonstrating the in-built poly-morphic functions
# len() function is used for a string
print (len("Javatpoint"))
# len() function is used for a list
print (len([110, 210, 130, 321]))
输出:
10
4
用户定义的多态函数的示例:
# here, is a simple Python function
# for demonstrating the Polymorphism
def add(p, q, r = 0):
return p + q + r
# Driver code
print (add(6, 23))
print (add(22, 31, 544))
输出:
29
597
使用类方法的多态性
以下是Python如何以相同的方式使用不同类型的类的示例。创建了循环遍历多个对象。然后,调用方法而不关心每个对象属于哪个类。假定这些方法在每个类中都存在。
示例:
class xyz():
def websites(self):
print("Javatpoint is a website out of many availabe on net.")
def topic(self):
print("Python is out of many topics about technology on Javatpoint.")
def type(self):
print("Javatpoint is an developed website.")
class PQR():
def websites(self):
print("Pinkvilla is a website out of many availabe on net. .")
def topic(self):
print("Celebrities is out of many topics.")
def type(self):
print("pinkvilla is a developing website.")
obj_jtp = xyz()
obj_pvl = PQR()
for domain in (obj_jtp, obj_pvl):
domain.websites()
domain.topic()
domain.type()
输出:
Javatpoint is a website out of many availabe on net.
Python is out of many topics about technology on Javatpoint.
Javatpoint is an developed website.
Pinkvilla is a website out of many availabe on net.
Celebrities is out of many topics.
pinkvilla is a developing website.
继承中的多态性
多态性允许我们在Python中定义与父类中的方法相同的方法。在继承中,父类的方法会被传递给子类。可以修改子类从父类继承的方法。当从父类继承的方法不适合子类时,这是非常有用的。我们在子类中重新实现这些方法。这就是 方法重写 。
示例:
class Birds:
def intro1(self):
print("There are multiple types of birds in the world.")
def flight1(self):
print("Many of these birds can fly but some cannot.")
class sparrow1(Birds):
def flight1(self):
print("Sparrows are the bird which can fly.")
class ostrich1(Birds):
def flight1(self):
print("Ostriches are the birds which cannot fly.")
obj_birds = Birds()
obj_spr1 = sparrow1()
obj_ost1 = ostrich1()
obj_birds.intro1()
obj_birds.flight1()
obj_spr1.intro1()
obj_spr1.flight1()
obj_ost1.intro1()
obj_ost1.flight1()
输出:
There are multiple types of birds in the world.
Many of these birds can fly but some cannot.
There are multiple types of birds in the world.
Sparrows are the bird which can fly.
There are multiple types of birds in the world.
Ostriches are the birds which cannot fly.
使用函数和对象的多态
我们还可以创建可以接受任何对象的函数。这允许多态性。让我们看下面的示例:我们创建一个名为“func()”的函数,它将接受一个我们称之为“obj”的对象。即使我们使用名字“obj”,任何被实例化的对象都可以调用这个函数。接下来,我们给函数赋予一个可以对传递给它的“obj”对象进行操作的任务。让我们称这三个方法为websites(),topic()和type()。它们分别在“xyz”和“PQR”类中定义。如果我们还没有“xyz”和“PQR”类的实例化,让我们创建它们。然后我们可以使用相同的func()函数调用它们的动作。
示例:
def func(obj):
obj.websites()
obj.topic()
obj.type()
obj_jtp = xyz()
obj_pvl = PQR()
func(obj_jtp)
func(obj_pvl)
输出:
Javatpoint is a website out of many availabe on net.
Python is out of many topics about technology on Javatpoint.
Javatpoint is a developed website.
Pinkvilla is a website out of many availabe on net. .
Celebrities is out of many topics.
pinkvilla is a developing website.
代码:使用函数实现多态性
class xyz():
def websites(self):
print("Javatpoint is a website out of many availabe on net.")
def topic(self):
print("Python is out of many topics about technology on Javatpoint.")
def type(self):
print("Javatpoint is an developed website.")
class PQR():
def websites(self):
print("Pinkvilla is a website out of many availabe on net. .")
def topic(self):
print("Celebrities is out of many topics.")
def type(self):
print("pinkvilla is an developing website.")
def func(obj):
obj.websites()
obj.topic()
obj.type()
obj_jtp = xyz()
obj_pvl = PQR()
func(obj_jtp)
func(obj_pvl)
输出:
Javatpoint is a website out of many availabe on net.
Python is out of many topics about technology on Javatpoint.
Javatpoint is a developed website.
Pinkvilla is a website out of many availabe on net. .
Celebrities is out of many topics.
pinkvilla is a developing website.