相关疑难解决方法(0)

在Python中定义另一个类中的类有什么好处?

我在这里谈论的是嵌套类.基本上,我有两个我正在建模的类.DownloadManager类和DownloadThread类.这里显而易见的OOP概念是构图.然而,构图并不一定意味着嵌套,对吧?

我的代码看起来像这样:

class DownloadThread:
    def foo(self):
        pass

class DownloadManager():
    def __init__(self):
        dwld_threads = []
    def create_new_thread():
        dwld_threads.append(DownloadThread())
Run Code Online (Sandbox Code Playgroud)

但现在我想知道是否存在嵌套会更好的情况.就像是:

class DownloadManager():
    class DownloadThread:
        def foo(self):
            pass
    def __init__(self):
        dwld_threads = []
    def create_new_thread():
        dwld_threads.append(DownloadManager.DownloadThread())
Run Code Online (Sandbox Code Playgroud)

python oop

94
推荐指数
5
解决办法
6万
查看次数

标签 统计

oop ×1

python ×1