Python双重继承
我正在尝试为以下问题找到合适的继承配置: 我的基类是一个代理类,它有3个抽象方法:Python双重继承,python,inheritance,Python,Inheritance,我正在尝试为以下问题找到合适的继承配置: 我的基类是一个代理类,它有3个抽象方法: class Agent(): __metaclass__ = abc.ABCMeta def __init__(self): @abc.abstractmethod def bidding_curve(self): @abc.abstractmethod def evaluate_bidding_curve(self): @abc.abstractmetho
class Agent():
__metaclass__ = abc.ABCMeta
def __init__(self):
@abc.abstractmethod
def bidding_curve(self):
@abc.abstractmethod
def evaluate_bidding_curve(self):
@abc.abstractmethod
def action(self):
可以使用可执行文件(dymo)或FMU(C源代码的zip文件)模拟代理。因此,我创建了两种不同类型的代理,它们继承自基本代理,并实现与模拟通信的方式。这是dymo仿真环境的实现,但FMU也是如此
class DymoAgent(Agent):
def __init__(self, power, dymo):
"""
Agent based on the dymosim simulation environment.
@param dymo: The dymo that needs to be run
"""
super(DymoAgent, self).__init__()
self.dymo = dymo
def bidding_curve(self):
pass
def evaluate_bidding_curve(self, priority):
pass
def action(self, priority):
self.dymo.action()
DymoAgent和FMUAgent决定了我的Agent将如何与仿真环境交互
接下来,我需要代理的实现来确定代理将如何与应用程序交互。但是,我希望这种交互独立于模拟环境。例如,我想创建一个可以与加热器应用程序通信的代理,并实现这样做的逻辑
class Heater(WhatAgentShouldIUseHere):
pass
我想让这个加热器类实现来自代理基类的抽象方法和Dymo或FMUAgent的实现,以便它知道如何与模拟交互。但是我不想用从不同的模拟代理继承的相同逻辑(因此基本上是相同的代码)编写两个加热器类。我在这幅图中总结了这个问题:
有没有办法使用继承来防止这种情况
/arnot使
加热器
成为一个简单的对象,在其构造函数中接受代理:
class Heater(object):
def __init__(self, agent):
self.agent = agent
# define other methods for interaction with Heater application
# in which you can access agent methods like self.agent.a_method(...)