多态性 Python 举例说明

什么是多态?

多态性可以定义为以多种不同形式出现的条件。它是 Python 编程中,在 Python 可以以不同的方式使用。它允许程序员在派生类中定义多个方法,并且它具有与父类中相同的名称。此类场景支持方法重载 Python.

多态性 Opera职权范围

中的运算符 Python 有助于执行数学和其他一些编程任务。例如,“+”运算符有助于在两个整数类型之间执行加法 Python,同样地,相同的运算符也有助于连接字符串 Python 节目。

让我们以 + (加号)为例 运算符 Python 显示多态性的应用 Python 如下图所示:

Python 代码:

p = 55
q = 77
r = 9.5
g1 = "Guru"
g2 = "99!"
print("the sum of two numbers",p + q)
print("the data type of result is",type(p + q))
print("The sum of two numbers",q + r)
print("the data type of result is", type (q + r))
print("The concatenated string is", g1 + g2)
print("The data type of two strings",type(g1 + g2))

输出:

the sum of two numbers 132
the data type of result is <class 'int'>

The sum of the two numbers 86.5
the data type of result is <class 'float'>

The concatenated string is Guru99!
The data type of two strings <class 'str'>

上面的例子也可以看作是运算符重载的例子。

用户定义方法中的多态性

用户定义的方法 Python 编程语言是用户创建的方法,使用关键字def和函数名来声明。

多态性 Python 编程语言是通过方法重载和重写来实现的。 Python 在子类和父类中用 def 关键字定义具有相同名称的方法。

让我们来看以下示例:-

Python 代码:

from math
import pi
class square:
    def __init__(self, length):
    self.l = length
def perimeter(self):
    return 4 * (self.l)
def area(self):
    return self.l * self.l
class Circle:
    def __init__(self, radius):
    self.r = radius
def perimeter(self):
    return 2 * pi * self.r
def area(self):
    return pi * self.r * * 2
# Initialize the classes
sqr = square(10)
c1 = Circle(4)
print("Perimeter computed for square: ", sqr.perimeter())
print("Area computed for square: ", sqr.area())
print("Perimeter computed for Circle: ", c1.perimeter())
print("Area computed for Circle: ", c1.area())

输出:

Perimeter computed for square:  40
Area computed for square:  100
Perimeter computed for Circle:  25.132741228718345
Area computed for Circle:  50.26548245743669

在上面的代码中,有两个用户定义的方法,perimeter和area,分别定义在circle和square类中。

如上所示,圆形类和方形类都调用相同的方法名,显示出多态性的特征来提供所需的输出。

函数中的多态性

内置函数 Python 被设计并兼容执行多种数据类型。 Python, Len() 是关键的内置函数之一。

它适用于多种数据类型:列表、元组、字符串和字典。Len() 函数返回与这些数据类型对应的明确信息。

下图展示了如何将多态性应用于 Python 与内置函数相关:–

函数中的多态性

以下程序有助于说明多态性在 Python -

Python 代码:

print ("The length of string Guru99 is ",len("Guru99"))
print("The length of list is ",len(["Guru99","Example","Reader"]))
print("The length of dictionary is ",len({"Website name":"Guru99","Type":"Education"}))

输出:

The length of string Guru99 is 6
The length of the list is 3
The length of the dictionary is 2

函数中的多态性

在上面的例子中,Len()函数的 Python 分别对字符串、列表和字典数据类型执行多态性。

多态性和继承

继承 Python 可以定义为编程概念,其中定义的子类从另一个基类继承属性 Python.

有两个关键 Python 称为方法覆盖和方法重载的概念。

  • 在方法重载中, Python 提供创建具有相同名称的方法的功能,以在给定的代码段中执行或执行不同的功能。它允许重载方法并使用它们以更简单的方式执行不同的任务。
  • 在方法重写中, Python 覆盖父类和子类中共享相似名称的值。

让我们来看看以下多态性和继承的例子,如下所示:-

Python 代码:

class baseclass:
    def __init__(self, name):
    self.name = name
def area1(self):
    pass
def __str__(self):
    return self.name
class rectangle(baseclass):
    def __init__(self, length, breadth):
    super().__init__("rectangle")
self.length = length
self.breadth = breadth
def area1(self):
    return self.length * self.breadth
class triangle(baseclass):
    def __init__(self, height, base):
    super().__init__("triangle")
self.height = height
self.base = base
def area1(self):
    return (self.base * self.height) / 2
a = rectangle(90, 80)
b = triangle(77, 64)
print("The shape is: ", b)
print("The area of shape is", b.area1())
print("The shape is:", a)
print("The area of shape is", a.area1())

输出:

The shape is: a triangle
The area of a shape is 2464.0

The shape is: a rectangle
The area of a shape is 7200

在上面的代码中,方法具有相同的名称,定义为 init 方法和 area1 方法。然后使用 square 和 rectangle 类的对象调用这两个方法来执行不同的任务,并提供正方形和矩形面积的输出。

类方法的多态性

这款 Python 编程使程序员能够使用类方法实现多态性和方法重载。不同的类 Python 可以有跨同名声明的方法 Python 码。

In Python,可以定义两个不同的类。一个是子类,它从另一个定义的类(称为父类)派生出属性。

下面的例子说明了类方法的多态性概念:-

Python 代码:

class amazon:
    def __init__(self, name, price):
    self.name = name
self.price = price
def info(self):
    print("This is product and am class is invoked. The name is {self.name}. This costs {self.price} rupees.")
class flipkart:
    def __init__(self, name, price):
    self.name = name
self.price = price
def info(self):
    print(f "This is product and fli class is invoked. The name is {self.name}. This costs {self.price} rupees.")
FLP = flipkart("Iphone", 2.5)
AMZ = amazon("Iphone", 4)
for product1 in (FLP, AMZ):
    product1.info()

输出:

This is a product, and fli class is invoked. The name is iPhone, and this costs 2.5 rupees.
This is a product, and am class is invoked. The name is iPhone, and this costs 4 rupees.

在上面的代码中,两个不同的类,分别为 flipkart 和 amazon,使用相同的方法名称 info 和 init 来提供产品的相应价格报价,并进一步说明了多态性的概念 Python.

方法重载和编译时多态性之间的区别

在编译时多态性中, Python 程序解析调用。编译时多态性是通过方法重载实现的。

这款 Python 编译器不会在运行时解析多态性调用。它也被归类为方法覆盖,其中相同的方法带有相似的签名或属性,但它们构成不同类的一部分。

总结

  • 多态性可以定义为以多种不同形式出现的情况。
  • 中的运算符 Python 有助于执行数学和其他一些编程任务。
  • 用户定义的方法 Python 编程语言是用户创建的方法,使用关键字def和函数名来声明。
  • 多态性 Python 提供了几个理想的品质,例如它促进了为不同类和方法编写的代码的可重用性。
  • 子类是派生类,它从父类中获取其属性。
  • 多态性也是通过运行时方法重写和编译时方法重载实现的。
  • 多态性 Python 也可以通过运算符重载和类方法实现。