使用带参数的基类构造函数方法声明和初始化子类有什么好处:
class BaseClass(object):
def __init__(self, a, b):
self.a = a
self.b = b
class SubClass(BaseClass):
def __init__(self, a, b, c, d):
BaseClass.__init__(self, a, b)
# or super().
self.c = c
self.d = d
newSubClassInstance = SubClass("one", "two", "three", "four")
完全不使用参数,只是初始化如下:
class BaseClass(object):
def __init__(self):
self.a = " "
self.b = " "
class SubClass(BaseClass):
def __init__(self):
self.c = " "
self.d = " "
newSubClassInstance = SubClass()
newSubClassInstance.a = "one"
newSubClassInstance.b = "two"
newSubClassInstance.c = "three"
newSubClassInstance.d = "four"
还是仅仅取决于人们打算如何在程序中编写和使用此代码?不过,我主要问的是期望;例如,如果被要求在 Python 中创建这个特定任务,一个人会比另一个人更好吗?
杨__羊羊
相关分类