Difference between revisions of "clasp"
m (i18n) |
|||
(6 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Minimal Lua class library in | + | Minimal Lua class library in 4 lines of code: https://github.com/evolbug/lua-clasp <br /> |
<source lang="lua"> | <source lang="lua"> | ||
class = require "clasp" | class = require "clasp" | ||
Line 15: | Line 15: | ||
a = Vector(10, 10) | a = Vector(10, 10) | ||
− | print('Vector:', a.x, a.y, a.isVector) | + | print('Vector:', a.x, a.y, a.isVector) -- "Vector: 10 10 true" |
Line 27: | Line 27: | ||
b = Vector3(1, 2, 3) | b = Vector3(1, 2, 3) | ||
− | print('Vector3:', b.x, b.y, b.z, b.isVector) | + | print('Vector3:', b.x, b.y, b.z, b.isVector) -- "Vector3: 1 2 3 true" |
Line 36: | Line 36: | ||
self.y = y | self.y = y | ||
end; | end; | ||
− | + | __ = { -- metamethod table | |
− | + | tostring = function(self) | |
return 'Point('..tostring(self.x)..', '..tostring(self.y)..')' | return 'Point('..tostring(self.x)..', '..tostring(self.y)..')' | ||
end; | end; | ||
Line 44: | Line 44: | ||
c = Point(15, 25) | c = Point(15, 25) | ||
− | print(c) | + | print(c) -- "Point(15, 25)" |
</source> | </source> | ||
Line 52: | Line 52: | ||
{{#set:Keyword=Class}} | {{#set:Keyword=Class}} | ||
[[Category:Libraries]] | [[Category:Libraries]] | ||
+ | == Other Languages == | ||
+ | {{i18n|clasp}} |
Latest revision as of 01:00, 16 December 2019
Minimal Lua class library in 4 lines of code: https://github.com/evolbug/lua-clasp
class = require "clasp"
-- Basic class
Vector = class {
isVector = true; -- static values
init = function(self, x, y) -- initializer function
self.x = x
self.y = y
end;
}
a = Vector(10, 10)
print('Vector:', a.x, a.y, a.isVector) -- "Vector: 10 10 true"
-- Inheritance
Vector3 = Vector:extend {
init = function(self, x, y, z) -- function overriding
Vector.init(self, x, y) -- superclass method call
self.z = z
end;
}
b = Vector3(1, 2, 3)
print('Vector3:', b.x, b.y, b.z, b.isVector) -- "Vector3: 1 2 3 true"
-- Metamethods
Point = class {
init = function(self, x, y)
self.x = x
self.y = y
end;
__ = { -- metamethod table
tostring = function(self)
return 'Point('..tostring(self.x)..', '..tostring(self.y)..')'
end;
};
}
c = Point(15, 25)
print(c) -- "Point(15, 25)"
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info