我是OOP的新手,我處於以下情況:我有一些報表「引擎」用於多個報表,唯一需要的是是配置文件的路徑。OOP:類繼承添加一個屬性vs構造函數參數
我會在Python代碼,但是這是一個不可知論者question.So,我有以下兩種方法
A)類ReportEngine是一個抽象類,它具有所需的一切,但對於配置的路徑文件。這樣,你只需要實例化ReportX類
class ReportEngine(object):
...
class Report1(ReportEngine):
_config_path = '...'
class Report2(ReportEngine):
_config_path = '...'
report_1 = Report1()
B)類ReportEngine可以通過配置文件路徑
class ReportEngine(object):
def __init__(self, config_path):
self._config_path = config_path
...
report_1 = ReportEngine(config_path="/files/...")
哪一種方法是正確的實例化?如果它很重要,報表對象將被插入另一個類,使用組合。
我同意:roto2: –