Python错误“&书信电报;方法>;缺少1个必需的位置参数:';自我'&引用;
Python新手。尝试创建一个简单的示例,演示两个级别的抽象。 获取错误TypeError:“HPNotebook”对象不可调用” 我已经浏览了大量的例子,但仍然感到困惑 为了理解,我已经在代码中显示了3个级别。Python错误“&书信电报;方法>;缺少1个必需的位置参数:';自我'&引用;,python,abstraction,Python,Abstraction,Python新手。尝试创建一个简单的示例,演示两个级别的抽象。 获取错误TypeError:“HPNotebook”对象不可调用” 我已经浏览了大量的例子,但仍然感到困惑 为了理解,我已经在代码中显示了3个级别。 你能给我指个地方帮我解释一下这个问题以及如何消除它吗 就如何纠正这一点提出建议。 谢谢 只需在HPNotebook上用super()替换self()。\uuu init\uuu并在HPNotebook.scroll上用super().scroll()替换HP.scroll() clas
你能给我指个地方帮我解释一下这个问题以及如何消除它吗 就如何纠正这一点提出建议。 谢谢 只需在
HPNotebook上用super()
替换self()
。\uuu init\uuu
并在HPNotebook.scroll上用super().scroll()
替换HP.scroll()
class HP笔记本电脑(HP):
定义初始化(自):
超级()
def单击(自我):
打印(“HP单击”)
def滚动(自):
super().scroll()
另外,请检查是否更好地理解python继承。super()
而不是HPNotebook中的self()
。\uuuu init\uuuuo()
此外,我认为您不能调用HP.scroll()
。您需要调用super().scroll()
谢谢你们都指出了这个问题。谢谢你们的链接。我们将阅读更多有关Super和继承的内容。
from abc import abstractmethod,ABC #this is to allow abstraction. the ABC forces inherited classes to implement the abstracted methods.
class TouchScreenLaptop(ABC):
def __init__(self):
pass
@abstractmethod #indicates the following method is an abstract method.
def scroll(self): # a function within the parent
pass #specifically indicates this is not being defined
@abstractmethod #indicates the following method is an abstract method.
def click(self):
pass #specifically indicates this is not being defined
class HP(TouchScreenLaptop):
def __init__(self):
pass
@abstractmethod #indicates the following method is an abstract method.
def click(self):
pass
def scroll(self):
print("HP Scroll")
class HPNotebook(HP):
def __init__(self):
self()
def click(self):
print("HP Click")
def scroll(self):
HP.scroll()
hp1=HPNotebook()
hp1.click() #the 2 level deep inherited function called by this instance
hp1.scroll() #the 1 level deep inherited function called by this instance