簡單的方法
我們可以用一種簡單的方式來達到 OOP 的模擬,這個方法中,我們只會用到對表格的操作。以角色類別舉例:Character 只包含一個函式 new,透過在 new 中宣告的區域變數 character 建立一個新的實體,並在 new 之中將 character 的成員定義好,最後將它回傳。繼承的方式是在新類別的 new 方法直接使用被繼承類別的 new 方法,直接取得被繼承類別的新物件,並直接在該物件添加新法法與新屬性。這樣的方式在使用上相當直觀,而且很好理解,但缺點就是你必須將類別成員都在 new 中定義好,無法在外部定義。
----------------
-- Character.lua
----------------
-- Define the base object
local Character = {}
-- Create the constructor function
-- This lets you pass in table values to override the defaults and make your object unique
Character.new = function( options )
local character = {}
character.name = (options and options.name) or "Noname"
character.job = (options and options.job) or "Homeless"
character.power = (options and options.power) or 4
character.weapon = (options and options.weapon) or "fist"
-- Add a method to the object
function character:attack()
print(string.format("%s %s use %s with power %d to attack", self.job, self.name, self.weapon, self.power))
end
return character
end
return Character
----------------
-- Warrior.lua
----------------
local Character = require("Character")
-- Define the base object
local Warrior = {}
-- Create the constructor function
-- This lets you pass in table values to override the defaults and make your objectique
Warrior.new = function( options )
options = options or {}
options.job = "Warrior"
options.weapon = "sword"
options.power = options.power or 100
local warrior = Character.new(options)
function warrior:shieldBash()
print(string.format("%s %s use shield bash", self.job, self.name))
end
return warrior
end
return Warrior
----------------
--Mage.lua
----------------
local Character = require("Character")
-- Define the base object
local Mage = {}
-- Create the constructor function
-- This lets you pass in table values to override the defaults and make your objectique
Mage.new = function( options )
options = options or {}
options.job = "Mage"
options.weapon = "stick"
local mage = Character.new(options)
-- Add a method to the object
function mage:attack()
print(string.format("%s %s use %s with power %d to attack", self.job, self.name, self.weapon, self.power))
end
function mage:fire()
print(string.format("%s %s use fire art", self.job, self.name))
end
function mage:ice()
print(string.format("%s %s use ice art", self.job, self.name))
end
return mage
end
return Mage
----------------
--Paladin.lua
----------------
local Warrior = require("Warrior")
-- Define the base object
local Paladin = {}
-- Create the constructor function
-- This lets you pass in table values to override the defaults and make your objectique
Paladin.new = function( options )
options = options or {}
options.job = "Paladin"
options.power = options.power or 200
local paladin = Warrior.new(options)
-- Add a method to the object
function paladin:heal()
print(string.format("%s %s use heal art", self.job, self.name))
end
return paladin
end
return Paladin
----------------
--main.lua
----------------
local Character = require("Character")
local Warrior = require("Warrior")
local Paladin = require("Paladin")
local Mage = require("Mage")
local Kevin = Character.new({name = "kevin"})
local Mary = Mage.new({name = "Mary", power = 5})
local Mark = Mage.new({name = "Mark"})
local Steve = Warrior.new({name = "Hank"})
local Edison = Paladin.new({name = "Edison"})
Kevin:attack()
Mary:attack()
Mary:ice()
Mark:attack()
Mark:fire()
Steve:attack()
Steve:shieldBash()
Edison:attack()
Edison:shieldBash()
Edison:heal()