最佳答案
我在这里讨论的是嵌套类。实际上,我有两个正在建模的类。DownloadManager 类和 DownloadThread 类。这里显而易见的 OOP 概念是组合。然而,组合并不一定意味着嵌套,对吗?
我的代码是这样的:
class DownloadThread:
def foo(self):
pass
class DownloadManager():
def __init__(self):
dwld_threads = []
def create_new_thread():
dwld_threads.append(DownloadThread())
但现在我想知道有没有更好的筑巢方式,比如:
class DownloadManager():
class DownloadThread:
def foo(self):
pass
def __init__(self):
dwld_threads = []
def create_new_thread():
dwld_threads.append(DownloadManager.DownloadThread())