简体   繁体   English

Python 中的多态性 VS JAVA 中的多态性

[英]Polymorphism in Python VS Polymorphism in JAVA

I'm trying to understand the polymorphism in python. Gone through lot of article but there is one doubt still there in my mind.我正在尝试了解 python 中的多态性。浏览了很多文章,但我仍然有一个疑问。 When I compare with java it is little bit confusing for me in Python.当我与 java 进行比较时,我对 Python 有点困惑。

As per my knowledge polymorphism is "one thing in many forms".据我所知,多态性是“多种形式的一件事”。 Polymorphism can be demonstrated using operator overloading, and method overloading.可以使用运算符重载和方法重载来演示多态性。 Let's take method overloading as a means to demonstrate Polymorphism concept.让我们以方法重载作为演示多态性概念的一种方式。 In java we can write it without use of inheritance. See below code.在 java 中,我们可以不使用 inheritance 来编写它。请参见下面的代码。

 public class Main{
    public static void main(String[] args) {
        System.out.println(add(1,2));;
        System.out.println(add(1,2,3));
    }
    public static int add(int a,int b){
        return a+b;
    }
    public static int add(int a,int b,int c){
        return a+b+c;
    }   
}

Python code: Python 代码:

class TestPolymorphism:
    def add(self,a,b):
        return (a+b)
    def add(self,a,b,c):
        return (a+b+c)

obj = TestPolymorphism()
print(obj.add(1,2)) #will get an error
print(obj.add(1,2,3))

The same method overloading works in java but not in python. Why there is difference?.相同的方法重载在 java 中有效,但在 python 中无效。为什么会有差异?。 If I want it to work then I have to change my python code as below:如果我想让它工作,那么我必须更改我的 python 代码,如下所示:

class TestPolymorphism:
    def add(self,a,b,c=None):
        if c ==None:
            sum  = a+b
            return sum
        else:
            sum = a+b+c
            return sum

obj = TestPolymorphism()
print(obj.add(1,2))
print(obj.add(1,2,3))

I'm not getting convinced that above the code is an example of Polymorphism.我不相信上面的代码是多态性的一个例子。 There are article like this not giving me convincing point.有这样的文章没有给我说服力。

can anyone give me theory behind the polymorphism in python?谁能告诉我 python 中多态性背后的理论?

The way I have always understood this:我一直这样理解:

Polymorphism is a concept that basically says it looks like the same but does things differently, depending on context.多态性是一个概念,基本上说它看起来相同,但做事不同,具体取决于上下文。

  1. if you have a method of the same name which has a different amount of arguments, this is method overloading.如果您有一个具有不同数量的 arguments 的同名方法,这就是方法重载。

  2. if you have a method of the same name with the same amount of arguments, but does a different thing depending on the class, this is method overriding.如果您有一个具有相同数量的 arguments 的同名方法,但根据 class 执行不同的操作,则这是方法覆盖。

Note: I say amount of arguments, as for dynamically typed languages, the type does only matter at run time.注意:我说的是 arguments 的数量,对于动态类型语言,类型只在运行时才重要。 For Java, for instance, it is not only the number of arguments, but also the type, of course.以 Java 为例,它不仅是 arguments 的数量,当然还有类型。

Both are concrete ways to deal with polymorphism.两者都是处理多态性的具体方法。 In detail, depending on the language you are using, the concept might be implemented and sometimes even interpreted differently.详细地说,根据您使用的语言,该概念可能会被实现,有时甚至会以不同的方式解释。

By what i understand in java even you have the same name of the method, you have different method, while in python when you have:根据我在 java 中的理解,即使您具有相同的方法名称,您也有不同的方法,而在 python 中,当您有:

class TestPolymorphism:
def add(self,a,b):
    return (a+b)
def add(self,a,b,c):
    return (a+b+c)

the last add that you write override the last ones.您编写的最后一个添加覆盖了最后一个。

In conclusion, when you have add with different arguments the best option add(self, a,b,c==None, ..., n=None) and if...总之,当您添加不同的 arguments 时,最佳选项 add(self, a,b,c==None, ..., n=None) 和 if...

Well from my understanding of polymorphism, it means one thing in many forms, and that can be illustrated using user app interface.好吧,根据我对多态性的理解,它意味着许多 forms 中的一件事,并且可以使用用户应用程序界面来说明。 A python code could be implemented such that at runtime, a draw method could be used to change the user app interface from drop-down list to textbox.可以实现 python 代码,以便在运行时可以使用绘制方法将用户应用程序界面从下拉列表更改为文本框。 etc. def draw(controls): for control in controls: control.draw I hope you get the point.等等 def draw(controls): for control in controls: control.draw 我希望你明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM