英文:
Polymorphism in Python VS Polymorphism in JAVA
问题
以下是您要翻译的内容:
我正在尝试理解Python中的多态性。我已经阅读了很多文章,但我的脑海中仍然有一个疑问。与Java进行比较时,在Python中,我感到有些困惑。
根据我的了解,多态性是“一物多用”的概念。可以使用运算符重载和方法重载来演示多态性,让我们以方法重载作为演示多态性概念的手段。在Java中,我们可以在不使用继承的情况下编写它。请参见下面的代码。
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代码:
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)) # 将会报错
print(obj.add(1, 2, 3))
相同的方法重载在Java中有效,但在Python中无效。为什么会有这种差异?如果我想让它在Python中有效,我必须将我的Python代码更改如下:
class TestPolymorphism:
def add(self, a, b, c=None):
if c is 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))
我不太确信上述代码是多态性的示例。像这里这样的文章并没有给我令人信服的观点。
有人能给我解释一下Python中多态性的理论吗?
英文:
I'm trying to understand the polymorphism in python. Gone through lot of article but there is one doubt still there in my mind. When I compare with java it is little bit confusing for me in 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.
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:
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?. If I want it to work then I have to change my python code as below:
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?
答案1
得分: 0
我一直理解的方式是:
多态性是一个基本概念,它基本上表示看起来相同,但根据上下文以不同方式执行操作。
-
如果你有一个同名方法,它有不同数量的参数,这就是方法重载。
-
如果你有一个同名方法,具有相同数量的参数,但根据类别执行不同操作,这就是方法覆盖。
注意:我提到参数的数量,因为对于动态类型的语言来说,类型只在运行时才重要。对于Java来说,例如,重要的不仅是参数的数量,还有类型,当然了。
这两种方式都是处理多态性的具体方法。
具体来说,根据你使用的编程语言,这个概念的实现方式可能会有所不同,有时甚至会被解释得不同。
英文:
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.
-
if you have a method of the same name which has a different amount of arguments, this is method overloading.
-
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.
Note: I say amount of arguments, as for dynamically typed languages, the type does only matter at run time. For Java, for instance, it is not only the number of arguments, but also the type, of course.
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.
答案2
得分: 0
根据我在Java中的理解,即使方法名称相同,方法也可以不同,而在Python中当你有:
class TestPolymorphism:
def add(self, a, b):
return (a + b)
def add(self, a, b, c):
return (a + b + c)
你写的最后一个 add
会覆盖之前的。
总之,当你有不同参数的 add
方法时,最佳选择是 add(self, a, b, c=None, ..., n=None)
,如果...
英文:
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:
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...
答案3
得分: 0
根据我对多态性的理解,它指的是一种事物以多种形式呈现,这可以通过用户应用界面来进行说明。
可以通过Python代码实现这样一个情景,即在运行时使用绘制方法来将用户应用界面从下拉列表更改为文本框等。
def draw(controls):
for control in controls:
希望你明白我的意思。
英文:
Well from my understanding of polymorphism, it means one thing in many forms, and that can be illustrated using user app interface.
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. etc.
def draw(controls):
for control in controls:
I hope you get the point.
专注分享java语言的经验与见解,让所有开发者获益!
评论