加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python双继承

发布时间:2020-12-20 11:34:04 所属栏目:Python 来源:网络整理
导读:我正在尝试为以下问题找到合适的继承配置: 我的基类是一个Agent类,它有3个抽象方法: class Agent():__metaclass__ = abc.ABCMeta def __init__(self): @abc.abstractmethod def bidding_curve(self): @abc.abstractmethod def evaluate_bidding_curve(self
我正在尝试为以下问题找到合适的继承配置:

我的基类是一个Agent类,它有3个抽象方法:

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确定我的代理如何与模拟环境交互.

接下来,我需要代理的实现来确定代理如何与应用程序交互.但是,我希望这种交互独立于模拟环境.例如.我想创建一个可以与Heater应用程序通信的代理,并实现这样做的逻辑.

class Heater(WhatAgentShouldIUseHere):
    pass

我希望这个Heater类能够实现Agent基类的抽象方法以及Dymo或FMUAgent的实现,因此它知道如何与模拟进行交互.但我不想用从不同的模拟代理继承的相同逻辑(因此基本相同的代码)编写2个Heater类.我在这张图片中总结了这个问题:

有没有办法使用继承来防止这种情况?

/ Arnout

解决方法

Make Heater是一个简单的对象,它在构造函数中接受一个代理:

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(...)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读