多重继承类

lua-users home
wiki

一个用于 Lua 的多重继承类库

该库基于 **PIL**(**Programming in Lua**,作者 Roberto Ierusalimschy)中关于 Lua 面向对象编程的通用指南,并且也受到 SimpleLuaClasses 中类库的启发,该库被用作起点。

其目标是实现一个功能齐全的 C++ 风格的类系统,包括多重继承、共享(类似于 C++ 的虚)派生以及对歧义和继承属性的正确处理。

摘要

使用全局命名类(参见手册)的简要摘要。

标准派生

class.A(P, Q)
class.B(P, Q)
class.C(A, B)

Cobj = C(constructor arguments ...)

共享派生

class.X(P, shared(Q))
class.Y(P, shared(Q))
class.Z(X, Y)

Zobj = Z(constructor arguments ...)

类用法

class.Account()
function Account:__init(initial)  -- constructor
	self.balance = initial or 0
end
function Account:deposit(amount)
	self.balance = self.balance + amount
end

myAccount = Account(10.00)

class.NamedAccount(Account)
function NamedAccount:__init(name, initial)
	self.Account:__init(initial)
	self.name = name or 'anonymous'
end

myNamedAccount = NamedAccount('John', 10.00)

对象属性存在于它们各自的对象中。

myNamedAccount.name		== 'John'
myNamedAccount.balance		== nil 
myNamedAccount.Account.balance	== 10.00

非歧义的基属性被继承。以下两者等效

myNamedAccount.Account:deposit(2.00) 
myNamedAccount:deposit(2.00)

歧义属性不被继承

class.SavingsAccount(Account)
class.CurrentAccount(Account)
class.CombinedAccount(SavingsAccount, CurrentAccount)

myCombinedAccount = CombinedAccount()
myCombinedAccount:deposit(2.00)  ← Error, deposit is nil

限定符解决歧义

myCombinedAccount.CurrentAccount:deposit(2.00)

如果 SavingsAccountCurrentAccountshared(Account) 派生,那么将存在一个 CombinedAccount:deposit() 方法。但在这种情况下,这样做没有意义,因为我们实际上需要两个独立的余额。

文件

完整的 Word 文档和源代码可以在这里找到

[手册]
[简要摘要 - 命名类]
[简要摘要 - 未命名类]
[源代码 - 命名和未命名类]
[源代码 - 仅未命名类]

以下是一些关于单继承和多重继承的简单示例

[单继承,未命名类]
[多重继承,未命名类] (1)
[单继承,命名类]
[多重继承,命名类] (1)
[多重继承,命名类]

(1) 在这些示例中,您可以使用选项 shared_meters(启用共享继承)和 keep_ambiguous(后者必须在 require 之前定义)。

这两个简单的类说明了索引和元方法

[带有索引的元组类]
[带有元方法的集合类]

所有内容汇总

[存档]

Lua 文件使用 4 个空格的制表符进行编辑。

作者

Hugo Etchegoyen
[email protected]

如果您修改了此内容,请在此处添加您的数据,谢谢。

历史

Version 2.04.04 - November 15, 2010
Included patches and ideas from Peter Schaefer ([email protected]) 
improving the efficiency of build(), __init() and other parts of the code.
The former remove_ambiguous() function was removed, improving the efficiency 
of both build() (instance creation) and mt:__call() (class creation). 
Ambiguities are now processed using the ambiguous_keys tables introduced by Peter.
Removed inheritance of class properties, which was inconsistent with
the way instance properties are handled (see pages 4-5 of the manual).
This was mostly harmless, but confusing. Now only methods are inherited.

Version 2.03 - February 6, 2007
Added support for indexing via __get() and __set() methods.
Added a couple of examples illustrating indexing and metamethods.

Version 2.02 - January 31, 2007
Added unclasslib.lua, a more efficient version of classlib.lua restricted
to unnamed classes.
Added compatibility with versions of Lua compiled without old-style vararg
support.

Version 2.01 - January 30, 2007
Added named classes and dot notation. Replaced the previous page
which was too long by a summary, details to be found in the
documentation, get it by clicking on the link above.
This version is backwards compatible with unnamed classes and the
square bracket notation, so it should run any previous code.

Version 1.03 - January 26, 2007
Changed the handling of constructors so that base objects not
explicitly initialized by them still get initialized by default.
Removed the source code from this page. Get it by clicking on 
the link above.

Version 1.02 - January 25, 2007
Added the possibility of not deleting ambiguous values from
classes and objects, useful for debugging.
Added a couple of simple test examples.

Version 1.01 - January 24, 2007
Minor polishing - modified some for loops to use ipairs()

Version 1.0 - January 24, 2007

另请参见


最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2010 年 11 月 18 日下午 5:20 GMT (差异)