Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop openERP中的继承(odoo)_Oop_Openerp_Odoo_Openerp 7_Openerp 8 - Fatal编程技术网

Oop openERP中的继承(odoo)

Oop openERP中的继承(odoo),oop,openerp,odoo,openerp-7,openerp-8,Oop,Openerp,Odoo,Openerp 7,Openerp 8,我是openERP的新手,有一次面试。请解释openERP中不同类型继承的概念,我认为总共有3种类型。请从面试的角度用非常简单的方式解释。 附言:我熟悉简单继承的概念。继承: def existing(self, cr, uid, ids, x, y, z, context=None): parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context) # my st

我是openERP的新手,有一次面试。请解释openERP中不同类型继承的概念,我认为总共有3种类型。请从面试的角度用非常简单的方式解释。

附言:我熟悉简单继承的概念。

继承:

def existing(self, cr, uid, ids, x, y, z, context=None):
    parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context)
    # my stuff
    return parent_res_plus_my_stuff
继承机制用于创建可重用的思想。可重用意味着在任何面向对象编程中重用父类的代码

优势:

def existing(self, cr, uid, ids, x, y, z, context=None):
    parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context)
    # my stuff
    return parent_res_plus_my_stuff
  • 减少代码冗余
  • 提供代码重用性
  • 减少源代码大小并提高代码可读性
  • 代码易于管理,并可分为父类和子类
  • 通过重写基类支持代码扩展性 子类中的功能
  • 缺点:

    def existing(self, cr, uid, ids, x, y, z, context=None):
        parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context)
        # my stuff
        return parent_res_plus_my_stuff
    
  • 在继承中,基类和子类是紧密耦合的。 因此,如果您更改父类的代码,它将对 所有的孩子都上了这门课

  • 在类层次结构中,许多数据成员未使用,内存 分配给他们的资源没有得到利用。因此会影响您的系统性能 如果未正确实现继承,则执行该程序

  • 在OpenERP中有两种继承方式。

    1.使用Pythonic方式的经典:

    def existing(self, cr, uid, ids, x, y, z, context=None):
        parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context)
        # my stuff
        return parent_res_plus_my_stuff
    
    它允许通过继承从orm.Model派生的类(比如添加图形支持的geomedel),向模型添加特定的“通用”行为

     class Myclass(GeoModel, AUtilsClass):
    
    使用_inherit:-

    主要目标是添加新行为/扩展现有模型。例如,您希望向发票添加新字段并添加新方法

    class AccountInvoice(orm.Model):
        _inherit = "account.invoice"
        _column = {'my_field': fields.char('My new field')}
        def a_new_func(self, cr, uid, ids, x, y, context=None):
            # my stuff
            return something
    
    覆盖现有方法:

    def existing(self, cr, uid, ids, x, y, z, context=None):
        parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context)
        # my stuff
        return parent_res_plus_my_stuff
    
    2.多态方式:-

    使用\u继承:-

    当使用_inherits时,您将以数据库方式执行一种多态模型

    例如,
    product.product
    继承
    product.template
    res.users
    继承
    res.partner
    。这意味着我们创建一个模型,该模型获取模型的专有技术,但在新的数据库表中添加传统数据/列。因此,当您创建一个用户时,所有合作伙伴数据都存储在
    res\u partner
    表中(并且创建了一个合作伙伴),所有与用户相关的信息都存储在
    res\u users
    表中

    为此,我们使用一个
    dict:_inherits={'res.partner':'partner_id'}
    键对应于基本模型,外键的值对应于基本模型

    与通过XML一样,您可以继承Odoo视图(如表单视图、树视图、搜索视图等),也可以从视图中更改行为

    要点:

    def existing(self, cr, uid, ids, x, y, z, context=None):
        parent_res = super(AccountInvoice, self).existing(cr, uid, ids, x, y, z, context=context)
        # my stuff
        return parent_res_plus_my_stuff
    
    上述两种方法可以应用于Odoo服务器端,您可以更改现有视图的行为,或者您可以在Odoo视图中更改的任何其他内容对客户端的影响


    我希望这会对您有所帮助。:)

    您错过了odoo文档中提到的第三个。它不是真正的继承,而是通过组合_inherit和_name来建模属性的副本。也许你也应该提一下?:-)我们可以同时使用_inherit和_inherit吗@DASADIYACHAITANYANo两者都有不同的用法,根据我们的用法,我们可以选择“继承”或“继承”