多重繼承類別 |
|
這個類別庫是基於 Roberto Ierusalimschy 所寫的 PIL(用 Lua 程式設計)中,關於 Lua 中 OOP 的一般準則,而它的靈感同時來自 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)
若 SavingsAccount
和 CurrentAccount
從 shared(Account)
派生,將會存在一個 CombinedAccount:deposit()
方法。但在這種情況下沒有意義,因為我們真的需要兩個分開的餘額。
檔案
可以在這裡找到完整的 Word 文件和原始碼
以下是一些單一和多重繼承的簡單範例
(1) 在這些範例中,你可以用 shared_meters
(啟用共享繼承)和 keep_ambiguous
(後者必須在 require
前定義)選項。
這兩個簡單類別說明了索引和元方法
全部放在一起
Lua 檔案是用 4 個空格的 tab 編輯。
作者
Hugo Etchegoyen hetchegoyen@hasar.com
如果你修改此檔案,請將你的資料加在這裡,謝謝。
歷史
Version 2.04.04 - November 15, 2010 Included patches and ideas from Peter Schaefer (peter.schaefer@gmail.com) 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