继承

传统的继承

简介

对象可能继承于很多定制的或是特定的模块。这比继承一个对象增加或修改某方面更好。

It is done with:

  1. _inherit='object.name'

对象的扩展

There are two possible ways to do this kind of inheritance. Both ways result in a new class of data, which holds parent fields and behaviour as well as additional fielda and behaviour, but they differ in heavy programatical consequences.

例子1中产生一个新的子类叫“custom_material”,当操作network.material的视图或是列表时可以看到或是使用它。这和例子2不同。

这是由于这个新类操作的表格(other.material)是不能被之前的对象“network.material”的视图或列表所识别的。

例子1:

  1. class custom_material(osv.osv):
  2. _name = 'network.material'
  3. _inherit = 'network.material'
  4. _columns = {
  5. 'manuf_warranty': fields.boolean('Manufacturer warranty?'),
  6. }
  7. _defaults = {
  8. 'manuf_warranty': lambda *a: False,
  9. }
  10. custom_material()

小技巧

Notice

_name == _inherit

这个例子,“custom_material”增加了一个新的字段“manuf_warranty”到对象“network.material”中。这个类的新的实例对运行在父类“network.material”上的视图或列表是可见的。

在面向对象的设计中,这种继承通常称为“类继承(class inheritance)”。子类继承父类的字段和函数。

例子2:

  1. class other_material(osv.osv):
  2. _name = 'other.material'
  3. _inherit = 'network.material'
  4. _columns = {
  5. 'manuf_warranty': fields.boolean('Manufacturer warranty?'),
  6. }
  7. _defaults = {
  8. 'manuf_warranty': lambda *a: False,
  9. }
  10. other_material()

小技巧

Notice

_name != _inherit

在这个例子中,“other_material”会继承“network.material”的所有字段,它另外会加入一个新的字段“manuf_warranty”。所有这些字段都在表格“other.material”中。这个类的新的实例对运行在父类“network.material”上的视图或列表是不可见的。

这种类型的继承被称为“原型继承(inheritance by prototyping)”。因为新创建的子类拷贝了父类的所有字段。子类继承父类的字段和方法。

Inheritance by Delegation

Syntax ::

  1. class tiny_object(osv.osv)
  2. _name = 'tiny.object'
  3. _table = 'tiny_object'
  4. _inherits = { 'tiny.object_a' : 'name_col_a', 'tiny.object_b' : 'name_col_b',
  5. ..., 'tiny.object_n' : 'name_col_n' }
  6. (...)

对象“tiny.object”继承n个对象“tiny.object_a, …,tiny.object_n”的所有的字段和方法。

To inherit from multiple tables, the technique consists in adding one column to the table tiny_object per inherited object. This column will store a foreign key (an id from another table). The values ‘name_col_a’ ‘name_col_b’ … ‘name_col_n’ are of type string and determine the title of the columns in which the foreign keys from ‘tiny.object_a’, …, ‘tiny.object_n’ are stored.

This inheritance mechanism is usually called “ instance inheritance “ or “ value inheritance “. A resource (instance) has the VALUES of its parents.