Last active
May 3, 2017 22:12
-
-
Save P403n1x87/77b87ce5edf9960f698a612699b6b133 to your computer and use it in GitHub Desktop.
Object Orientation in Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--+--------------------------------------------------------------------------+ | |
-- Class Constructor. | |
-- | |
-- Supports single inheritance. To define a new class use as | |
-- | |
-- A = class() | |
-- | |
-- To define a class B that inherits from A, use as | |
-- | |
-- B = class(A) | |
--+--------------------------------------------------------------------------+ | |
function class(cls) | |
-- Create a new class | |
C = {} | |
C.__index = C | |
if cls then | |
-- If cls is provided, make the new class a subclass of the given class | |
setmetatable(C, cls) | |
end | |
-- Empty initializer function. This can be overridden by any new class | |
-- and is similar to Python's __init__ method. | |
C.init = function(self, ...) end | |
-- The instance constructor. In general there should be no need to overridde | |
-- this function, unless you know what you're doing. This is analogous to | |
-- Python's __new__ method | |
C.new = function(self, ...) | |
obj = cls and cls:new(unpack({...})) or {} | |
setmetatable(obj, self) | |
obj:init(unpack({...})) | |
return obj | |
end | |
return C | |
end | |
--+--------------------------------------------------------------------------+ | |
-- Superclass | |
-- | |
-- Returns the superclass of the given class. | |
-- It is a simple wrapper around getmetatable | |
--+--------------------------------------------------------------------------+ | |
function super(cls) | |
return getmetatable(cls) | |
end | |
--+--------------------------------------------------------------------------+ | |
-- Instance Of | |
-- | |
-- Returns the class of the given object. | |
--+--------------------------------------------------------------------------+ | |
function instanceof(obj) | |
return getmetatable(obj) | |
end | |
--+--------------------------------------------------------------------------+ | |
-- Is Instance | |
-- | |
-- Checks if an instance is an object of the given class | |
--+--------------------------------------------------------------------------+ | |
function isinstance(obj, cls) | |
return instanceof(obj) == cls | |
end | |
--+--------------------------------------------------------------------------+ | |
-- Standard object (analogous to Python2's object class) | |
--+--------------------------------------------------------------------------+ | |
object = class() | |
--+--------------------------------------------------------------------------+ | |
-- Class A | |
--+--------------------------------------------------------------------------+ | |
A = class(object) | |
function A:init(msg) | |
assert(msg ~= nil, "A message must be provided to the class constructor") | |
self.msg = msg | |
end | |
function A:class() | |
return "A" | |
end | |
function A:bark() | |
print("Arf!") | |
end | |
--+--------------------------------------------------------------------------+ | |
-- Class B | |
-- | |
-- Inherits from A | |
--+--------------------------------------------------------------------------+ | |
B = class(A) | |
function B:class() | |
return "B" | |
end | |
function B:superClass() | |
return super(B):class() | |
end | |
--+--------------------------------------------------------------------------+ | |
-- Test | |
-- | |
-- Expected output: | |
-- | |
-- a is an object: true | |
-- The class of a is A | |
-- a says: Hello World! | |
-- The class of b is B | |
-- The superclass of b is A | |
-- Arf! | |
-- | |
--+--------------------------------------------------------------------------+ | |
a = A:new("Hello World!") | |
print("a is an object: " .. (super(instanceof(a)) == object and "true" or "false")) | |
print("The class of a is " .. a:class()) | |
print("a says: " .. a.msg) | |
b = B:new("Hello Subclass!") | |
print("The class of b is " .. b:class()) | |
print("The superclass of b is " .. b:superClass()) | |
b:bark() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment