Changes
Jump to navigation
Jump to search
← Older edit
Newer edit →
class
170 bytes removed
,
00:58, 15 March 2012
no edit summary
Line 4:
Line 4:
<source lang="lua">
<source lang="lua">
−
-- Class definition system
class = function(prototype)
class = function(prototype)
−
local derived = {}
+
local derived={}
−
local derivedMT = {
+
if prototype then
−
__index
= prototype
,
+
function derived.
__index
(t
,
key)
−
__call = function
(
proto
,
...
)
+
return rawget
(
derived
,
key
)
or prototype[key]
−
local instance = {}
+
end
−
local instanceMT = {
+
else
−
__index
= derived
,
+
function derived.
__index
(t
,
key)
−
__call = function
()
+
return rawget
(
derived,key
)
−
return nil, "attempt to invoke an instance of a class"
+
end
−
end,
+
end
−
}
+
function derived.__call(proto
,
...)
−
setmetatable(instance,
instanceMT
)
+
local instance={
}
−
if
instance.init then
+
setmetatable(instance,
proto
)
−
instance:
init(...)
+
local init=
instance.
init
−
end
+
if
init then
−
return instance
+
init(
instance,
...)
−
end
,
+
end
−
}
+
return instance
−
setmetatable(derived,
derivedMT
)
+
end
−
return derived
+
setmetatable(derived,
derived
)
−
end
+
return derived
+
end
</source>
</source>
Levak
30
edits
Navigation menu
Personal tools
Log in
Namespaces
Page
Discussion
Variants
Views
Read
View source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
Special pages
Printable version