python中的多态是什么_Python中的多态

python中的多态是什么

Polymorphism is a concept of Object Oriented Programming, which means multiple forms or more than one form. Polymorphism enables using a single interface with input of different datatypes, different class or may be for different number of inputs.

多态是面向对象编程的概念,它表示多种形式或一种以上形式。 多态性使单个接口可以使用不同数据类型,不同类别或可能用于不同数量输入的输入。

In python as everything is an object hence by default a function can take anything as an argument but the execution of the function might fail as every function has some logic that it follows.

在python中,由于所有事物都是对象,因此默认情况下,函数可以将任何事物作为参数,但是由于每个函数都有其遵循的逻辑,因此函数的执行可能会失败。

For example,

例如,

len("hello")      # returns 5 as result
len([1,2,3,4,45,345,23,42])     # returns 8 as result

In this case the function len is polymorphic as it is taking string as input in the first case and is taking list as input in the second case.

在这种情况下,函数len是多态的,因为在第一种情况下函数以字符串为输入,在第二种情况下函数以list为输入。

In python, polymorphism is a way of making a function accept objects of different classes if they behave similarly.

在python中,多态是一种使函数接受行为不同的类的方法。

Method overriding is a type of polymorphism in which a child class which is extending the parent class can provide different definition to any function defined in the parent class as per its own requirements.

方法重写是一种多态性,其中扩展父类的子类可以根据其自身的要求为父类中定义的任何函数提供不同的定义。

方法重载 (Method Overloading)

Method overloading or function overloading is a type of polymorphism in which we can define a number of methods with the same name but with a different number of parameters as well as parameters can be of different types. These methods can perform a similar or different function.

方法重载或函数重载是一种多态性,其中我们可以定义许多名称相同但参数数量不同的方法,并且参数可以具有不同的类型。 这些方法可以执行相似或不同的功能。

Python doesn't support method overloading on the basis of different number of parameters in functions.

Python不支持基于函数中不同数量参数的方法重载。

定义多态类 (Defining Polymorphic Classes)

Imagine a situation in which we have a different class for shapes like Square, Triangle etc which serves as a resource to calculate the area of that shape. Each shape has a different number of dimensions which are used to calculate the area of the respective shape.

想象一下一种情况,我们对于正方形,三角形等形状有不同的类,可作为计算该形状面积的资源。 每个形状都有不同数量的尺寸,用于计算各个形状的面积。

Now one approach is to define different functions with different names to calculate the area of the given shapes. The program depicting this approach is shown below:

现在一种方法是用不同的名称定义不同的函数,以计算给定形状的面积。 下面显示了描述此方法的程序:

class Square:
    side = 5     
    def calculate_area_sq(self):
        return self.side * self.side

class Triangle:
    base = 5
    height = 4
    def calculate_area_tri(self):
        return 0.5 * self.base * self.height

sq = Square()
tri = Triangle()
print("Area of square: ", sq.calculate_area_sq())
print("Area of triangle: ", tri.calculate_area_tri())

Area of square: 25 Area of triangle: 10.0

正方形面积:25三角形面积:10.0

The problem with this approach is that the developer has to remember the name of each function separately. In a much larger program, it is very difficult to memorize the name of the functions for every small operation. Here comes the role of method overloading.

这种方法的问题在于,开发人员必须分别记住每个函数的名称。 在更大的程序中,很难记住每个小操作的功能名称。 这就是方法重载的作用。

Now let's change the name of functions to calculate the area and give them both same name calculate_area() while keeping the function separately in both the classes with different definitions. In this case the type of object will help in resolving the call to the function. The program below shows the implementation of this type of polymorphism with class methods:

现在,让我们更改函数的名称以计算面积,并为它们赋予相同的名称calculate_area()同时在具有不同定义的两个类中分别保留函数。 在这种情况下,对象的类型将有助于解决对该函数的调用。 下面的程序使用类方法显示了这种多态性的实现:

class Square:
    side = 5     
    def calculate_area(self):
        return self.side * self.side

class Triangle:
    base = 5
    height = 4
    def calculate_area(self):
        return 0.5 * self.base * self.height

sq = Square()
tri = Triangle()
print("Area of square: ", sq.calculate_area())
print("Area of triangle: ", tri.calculate_area())

Area of square: 25 Area of triangle: 10.0

正方形面积:25三角形面积:10.0

As you can see in the implementation of both the classes i.e. Square as well as Triangle has the function with same name calculate_area(), but due to different objects its call get resolved correctly, that is when the function is called using the object sq then the function of class Square is called and when it is called using the object tri then the function of class Triangle is called.

正如您在两个类(即SquareTriangle的实现中所看到的那样,该函数具有相同的名称calculate_area() ,但是由于对象不同,因此其调用可以正确解析,也就是说,使用对象sq调用该函数时,调用Square类的函数,并且使用对象tri调用该函数时,将调用Triangle类的函数。

类方法的多态 (Polymorphism with Class Methods)

What we saw in the example above is again obvious behaviour. Let's use a loop which iterates over a tuple of objects of various shapes and call the area function to calculate area for each shape object.

在上面的示例中,我们再次看到了明显的行为。 让我们使用一个循环,该循环遍历各种形状的对象的元组,并调用area函数来计算每个形状对象的面积。

sq = Square()
tri = Triangle()

for(obj in (sq, tri)):
    obj.calculate_area()

Now this is a better example of polymorphism because now we are treating objects of different classes as an object on which same function gets called.

现在,这是一个更好的多态示例,因为现在我们将不同类的对象视为在其上调用相同函数的对象。

Here python doesn't care about the type of object which is calling the function hence making the class method polymorphic in nature.

此处python不在乎调用函数的对象类型,因此实际上使类方法具有多态性。

具有功能的多态 (Polymorphism with Functions)

Just like we used a loop in the above example, we can also create a function which takes an object of some shape class as input and then calls the function to calculate area for it. For example,

就像在上面的示例中使用循环一样,我们还可以创建一个函数,该函数将某个形状类的对象作为输入,然后调用该函数为其计算面积。 例如,

find_area_of_shape(obj):
    obj.calculate_area()

sq = Square()
tri = Triangle()

# calling the method with different objects
find_area_of_shape(sq)
find_area_of_shape(tri)

In the example above we have used the same function find_area_of_shape to calculate area of two different shape classes. The same function takes different class objects as arguments and executes perfectly to return the result. This is polymorphism.

在上面的示例中,我们使用了相同的函数find_area_of_shape计算两个不同形状类的面积。 相同的函数采用不同的类对象作为参数,并完美执行以返回结果。 这是多态性。

翻译自: https://www.studytonight.com/python/python-polymorphism

python中的多态是什么

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值