我是OOP新手,我想知道为什么我需要提供参数主窗口到最后一行。如果我自己在函数外部调用,我不需要参数,但是当我在类中调用它时,我需要提供类名,以便它工作。例如,主窗口.class\u func2()抛出错误
class MainWindow():
def __init__(self):
print("in init")
def claas_func(self):
print ("func1")
def class_func2(self):
outside_func()
def outside_func():
print('outside called')
instance = MainWindow()
MainWindow.class_func2(MainWindow)
试试这个。已创建MainWindow()的实例。现在你可以用它访问它的成员了。你知道吗
同时运行这个命令,注意它初始化了MainWindow()类2x。这是多余的,不恰当的。但是你可以看到它在做什么。你知道吗
你应该看看
@staticmethod
这个@staticmethod(它本身就是一个非常酷的叫做“decorator”的东西)将使方法本身完全可以调用,而不需要传递“self”。你知道吗
希望有帮助
相关问题 更多 >
编程相关推荐