【发布时间】:2021-03-24 05:55:22
【问题描述】:
我有这个带有父/子类的小例子。我知道代码将毫无问题地运行,但是用具有不同签名的方法覆盖父方法是否是一种好习惯? PyCharm 似乎并不认为这是一个好主意:
方法“Daughter.do()”的签名与类“Mother”中基方法的签名不匹配
有关信息,调用 Parent 方法 do 应该执行 do(self) 不是 do(self, c)。
代码示例:
class Parent:
def __init__(self):
self._a = "a"
def do(self):
print("Parent: {}".format(self._a))
class Child(Parent):
def __init__(self):
Parent.__init__(self)
self._b = "b"
def do(self, c):
print("Child: {}".format(self._a))
print("Child: {}".format(self._b))
print("Child: {}".format(c))
【问题讨论】:
标签: python python-3.x