gpt4 book ai didi

python - 正确的类继承

转载 作者:行者123 更新时间:2023-11-30 23:47:58 24 4
gpt4 key购买 nike

我编写了一些代码,允许普遍使用术语“工作”来执行独特的任务。可以通过设置初始变量“job_type”来选择具体的作业。从该初始变量中选择一个特定的子类来执行适当的工作。也许代码会更有意义:)

if __name__=='__main__':
# these variables would normally be called in from a config file
job_type = 'job1'
uni_var = 10

job_select = superClass(job_type, uni_var)
job_select.job()


class superClass(object):
def __init__(self, job_type, uni_var):
self.job_type = job_type
self.uni_var = uni_var

if self.job_type == 'job1':
self.jobChoice = option1()
else:
self.jobChoice = option2()

# This is the definition called by the main function it then
# redirects the request to the appropriate job sub class
def job(self):
self.jobChoice.job()

class option1(superClass):
def __init__(self):
pass

def job(self):
print 'job option 1'
print uni_var

class option2(superClass):
def __init__(self):
pass

def job(self):
print 'job option 2'
print uni_var

此代码背后的想法是允许单个/常量“main”函数纯粹基于变量“job_type”来执行各种独特的任务。看起来效果不错。

我的问题(作为一个非常缺乏经验的编码员)是,我是否以正确的方式处理这个问题,或者是否有更好的方法来做事?

此外,我是否在父类(super class)中正确设置了变量“uni_var”,以便在所有/任何父类(super class)子类之间正确共享?

谢谢。

最佳答案

我怀疑您真正想要的是使用 Factory Method Pattern在这里。

您可以将代码更改为如下所示:

if __name__=='__main__':
# these variables would normally be called in from a config file
job_type = 'job1'
uni_var = 10

job_select = superClass.optionFactory(job_type, uni_var)
job_select.job()


class superClass(object):
def __init__(self, job_type, uni_var):
self.job_type = job_type
self.uni_var = uni_var

# This is the definition called by the main function it then
# redirects the request to the appropriate job sub class
def job(self):
raise NotImplementedError()

@staticmethod
def optionFactory(job_type, uni_var):
"Return an instance of superClass based on job_type and uni_var."
if job_type == "job1":
return option1(job_type, uni_var)
else:
return option2(job_type, uni_var)

class option1(superClass):
def __init__(self, job_type, uni_var):
super(option1, self).__init__(job_type, uni_var)

def job(self):
print 'job option 1'
print uni_var

class option2(superClass):
def __init__(self, job_type, uni_var):
super(option2, self).__init__(job_type, uni_var)

def job(self):
print 'job option 2'
print uni_var

但是,请注意,此实现将要求每次创建新子类时都更改 superClass。另一种选择是使 optionFactory 方法成为独立函数(而不是 superClass 的方法)。像这样:

if __name__=='__main__':
# these variables would normally be called in from a config file
job_type = 'job1'
uni_var = 10

job_select = optionFactory(job_type, uni_var)
job_select.job()


class superClass(object):
def __init__(self, job_type, uni_var):
self.job_type = job_type
self.uni_var = uni_var

# This is the definition called by the main function it then
# redirects the request to the appropriate job sub class
def job(self):
raise NotImplementedError()

class option1(superClass):
def __init__(self, job_type, uni_var):
super(option1, self).__init__(job_type, uni_var)

def job(self):
print 'job option 1'
print uni_var

class option2(superClass):
def __init__(self, job_type, uni_var):
super(option2, self).__init__(job_type, uni_var)

def job(self):
print 'job option 2'
print uni_var

def optionFactory(job_type, uni_var):
"Return an instance of superClass based on job_type and uni_var."
if job_type == "job1":
return option1(job_type, uni_var)
else:
return option2(job_type, uni_var)

关于python - 正确的类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com