简体   繁体   English

python 中两个同名但参数不同的方法

[英]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.

相关问题 python mechanize处理两个具有相同名称的参数 - python mechanize handle two parameters with same name 两个不同的Excel文件以匹配具有相同名称的行 - Two different excel file to match their rows having same name 两个类方法在功能上相同,但名称不同 - Two class methods were same functionally but got an different name 定义两个名称相同但返回类型不同的方法是否合法? - Is it legal to define two methods with the same name but different returning types? 具有相同名称的对象在python中引用了不同的id - Objects having same name refer to different id in python Python中,装饰两个同名方法来区分 - In Python, decorate two methods with the same name to distinguish them 与inheritance、Python同名的方法 - Methods of the same name and inheritance, Python python:在不同的包下具有相同名称的两个模块和类 - python: Two modules and classes with the same name under different packages 如何在相同的列名下将列拆分为两个,并且在 Python 中具有相同的值? - How to split the column into two under same column name and having same values in Python? 尽管代码不同,但两个不同的文件仍提供相同的输出-PyQT5和Python - Two different files giving same output despite having different codes - PyQT5 and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM