[英]Two methods having same name but different parameters in python
I read on Google that for method overriding in python the methods in the base and derive class should have same name and same parameters.我在 Google 上读到,对于 python 中的方法覆盖,基础中的方法和派生 class 应该具有相同的名称和相同的参数。 Is it true?
这是真的吗? Can parameters be different?
参数可以不同吗? See my code below:
请参阅下面的代码:
class A:
def add(self,a,b):
return a+b
class B(A):
def add(self,a,b,c):
return a+b+c
Can we still call it overriding?我们还能称它为压倒一切吗? because parameters of both methods are different and I read on Google for method overriding, two methods name and parameters should be same.
因为两种方法的参数不同,并且我在 Google 上阅读了方法覆盖,所以两个方法的名称和参数应该相同。 Please clear my confusion.
请清除我的困惑。
Thanks谢谢
Yes, it can still be called overriding.是的,它仍然可以称为覆盖。 Parameters can be optional, too.
参数也可以是可选的。
edit: See https://stackoverflow.com/a/54155637/7415199编辑:见https://stackoverflow.com/a/54155637/7415199
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.