Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python类的子类或实例_Python_Class_Inheritance_Instance_Subclass - Fatal编程技术网

python类的子类或实例

python类的子类或实例,python,class,inheritance,instance,subclass,Python,Class,Inheritance,Instance,Subclass,这是我使用python类和子类的第一个示例。这里的一个问题是ParentUI属性被覆盖 class OptUI(object): def __init__(self, ParentUI): self.ParentUI = ParentUI class ListUI(object): def __init__(self, ParentUI): self.ParentUI = ParentUI class Window(OptUI, ListUI): Ma

这是我使用python类和子类的第一个示例。这里的一个问题是ParentUI属性被覆盖

class OptUI(object):
def __init__(self, ParentUI):
    self.ParentUI = ParentUI

class ListUI(object):
    def __init__(self, ParentUI):
        self.ParentUI = ParentUI

class Window(OptUI, ListUI):
    MainFrame = "MainFrame"
    TestFrame = "TestFrame"
    def __init__(self):
        OptUI.__init__(self, self.MainFrame)
        ListUI.__init__(self, self.TestFrame)
如果在Window类中使用实例而不是继承,则有一个解决方案

class Window(object):
    MainFrame = "MainFrame"
    TestFrame = "TestFrame"
    def __init__(self):
        self.OUI = OptUI(self.MainFrame)
        self.LUI = ListUI(self.TestFrame)

在第二种情况下,属性ParentUI有一个准名称空间。那么这在实践中是一个好方法吗?这是如何解决的?

我认为你需要考虑你的类是如何相互关联的,然后用它来确定代码应该如何构造。 继承意味着两个类之间的IS-A关系。也就是说,派生类的实例是基类的实例。在原始代码中,
Window
类试图从
OptUI
ListUI
继承,如果这两个对象可以以有意义的方式重叠,并且
Window
对象可以同时是这两个对象,那么这可能是有意义的。但是,由于基类都希望有一个具有不同值的
ParentUI
变量,因此同时使用这两个变量可能没有意义(尽管这也可能只是
OptUI
ListUI
类的设计缺陷)。你应该得出自己的结论,因为我不知道你的课程到底是什么意思

另一方面,包含UI类的实例(称为“组合”)并不能说明
窗口
类应该具有哪些接口。它建议一个HAS-a关系,因为每个
窗口
实例都有一个
OptUI
实例和一个
ListUI
实例。如果这比一段“是一种关系”更有意义的话,那么写作肯定是一条路要走


每种方法都做一些可能也是有意义的。也就是说,从一个UI类继承,同时包含另一个UI类的实例。
窗口
实例可能是一个
OptUI
实例,同时有一个
ListUI
实例。

我想你应该在