刚听完类 继承等东西我懵逼了,不会了
如何在EEP_INFO中继承PARAM_INFO CODE_PATH的方法,我就是想要这两个类中的变量{:10_306:} ,小白完全不会了,查也不会查了{:10_266:}#获取当前dir
curr_dir = os.getcwd()
all_dict = {}#如果将小字典加入该总字典中可以执行all_dict.update(new_dict)
class PARAM_INFO:
@staticmethod
def __init__(self,param_dict):
super(PARAM_INFO,self).__init__ #调用父类的__init__方法
self.model = param_dict.get('MODEL')
self.typee = param_dict.get('TYPE')
self.model_type = param_dict.get('MODEL_TYPE')
self.factory_package = param_dict.get('FACTORY_PACKAGE')
self.panel_info = param_dict.get('PANEL_INFO')
self.platform = param_dict.get('PLATFORM')
self.panel_size = param_dict.get('PANEL_SIZE') # 需要确保在 param_dict 中已经存在该 key
self.panel_number = param_dict.get('PANEL_NUMBER') # 需要确保在 param_dict 中已经存在该 key
param_dict = {}
with open(curr_dir + '/param_main_file.txt','rt') as f:
for i ,line in enumerate(f):
line = line.strip()
if i == 0:
MODEL_TYPE = line
split_str = MODEL_TYPE.split("_")
MODEL = split_str
TYPE = split_str
elif i == 1:
FACTORY_PACKAGE = line
elif i == 2:
PANEL_INFO = line
elif i == 3:
PLATFORM = line
else:
break
#将变量值存入字典中
for var_name in ['MODEL','TYPE','MODEL_TYPE','FACTORY_PACKAGE','PANEL_INFO','PLATFORM']:
param_dict = locals()
all_dict.update(param_dict)
#调用PARAM_INFO中的all_dict字典及其变量
#param_info = PARAM_INFO(all_dict)
#print(param_info.__dict__)#打印字典
#print(param_info.model)#打印变量
#获取需要的path
class CODE_PATH:
@staticmethod
def __init__(self,codepath_dict):
self.mtk_code = codepath_dict.get('MTK_CODE')
self.rtk_code = codepath_dict.get('RTK_CODE')
self.mstar_code = codepath_dict.get('MSTAR_CODE')
self.amlogic_code = codepath_dict.get('AMLOGIC_CODE')
self.ntk_code = codepath_dict.get('NTK_CODE')
self.hisi_code = codepath_dict.get('HISI_CODE')
#定义代码路径
MTK_CODE = "/work_sas1/workspace/Release/build_system_iscsi/factory/MTK/factory"
RTK_CODE = "/work_sas1/workspace/Release/build_system_iscsi/factory/RTK/factory"
MSTAR_CODE = "/work_sas1/workspace/Release/build_system_iscsi/factory/Mstar/factory"
AMLOGIC_CODE = "/work_sas1/workspace/Release/build_system_iscsi/factory/Amlogic/factory"
NTK_CODE = "/work_sas1/workspace/Release/build_system_iscsi/factory/NTK/factory"
HISI_CODE = "/work_sas1/workspace/Release/build_system_iscsi/factory/Hisi/factory"
# 将变量名值存入字典
codepath_dict = {}
for var_name in ['MTK_CODE','RTK_CODE','MSTAR_CODE','AMLOGIC_CODE','NTK_CODE','HISI_CODE']:
codepath_dict = locals()
all_dict.update(codepath_dict)
#调用CODE_PATH中的all_dict字典及其变量
#code_path = CODE_PATH(all_dict)
#print(code_path.__dict__)或print(CODE_PATH.all_dict)
#print(code_path.mtk_code)
class EEP_INFO(): 根据你的描述,你希望在EEP_INFO类中继承PARAM_INFO和CODE_PATH类的方法。
首先,你需要调整你的代码,让PARAM_INFO和CODE_PATH类继承自object,
然后让EEP_INFO类继承自PARAM_INFO和CODE_PATH。
为了方便,在EEP_INFO类中我们将创建一个新的初始化方法__init__来完成这个任务。
这是修改后的代码:
class PARAM_INFO(object):
def __init__(self, param_dict):
self.model = param_dict.get('MODEL')
# ...其他属性的赋值
class CODE_PATH(object):
def __init__(self, codepath_dict):
self.mtk_code = codepath_dict.get('MTK_CODE')
# ...其他属性的赋值
class EEP_INFO(PARAM_INFO, CODE_PATH):
def __init__(self, param_dict, codepath_dict):
PARAM_INFO.__init__(self, param_dict)
CODE_PATH.__init__(self, codepath_dict)
# 创建param_dict和codepath_dict
param_dict = {...}# 从文件中读取的参数字典
codepath_dict = {...}# 代码路径字典
# 创建EEP_INFO实例
eep_info = EEP_INFO(param_dict, codepath_dict)
# 访问EEP_INFO实例的属性
print(eep_info.model)
print(eep_info.mtk_code)
这样,EEP_INFO类就可以继承PARAM_INFO和CODE_PATH类的属性和方法了。你可以使用eep_info实例访问这些属性。 isdkz 发表于 2023-4-3 12:05
根据你的描述,你希望在EEP_INFO类中继承PARAM_INFO和CODE_PATH类的方法。
首先,你需要调整你的代码, ...
早上我也是这样试的,只是没写如下两行
param_dict = {...}# 从文件中读取的参数字典
codepath_dict = {...}# 代码路径字典
但是如上执行有错
AttributeError: 'EEP_INFO' object has no attribute 'param_dict
基础知识不扎实,我早上已经试晕了{:10_266:}
页:
[1]