Код
local print = print
local function Object()
local PUBLIC = {} -- public, interface
local VIRTUAL = {} -- protected, virtual
return PUBLIC, VIRTUAL
end
local function Figure()
-- inheritance
local PUBLIC, VIRTUAL = Object()
local this = VIRTUAL
-- private
local function CreateDC()
print 'Create DC'
end
local function ReleaseDC()
print 'Release DC\n'
end
local function BeginPaint()
print 'Begin Paint'
end
local function EndPaint()
print 'End Paint'
end
function PUBLIC.Draw() -- public method
CreateDC()
BeginPaint()
VIRTUAL.Draw() -- virtual call
EndPaint()
ReleaseDC()
end
VIRTUAL.Draw = nil -- abstract method
return PUBLIC, VIRTUAL
end
local function Point(x, y)
local PUBLIC, VIRTUAL = Figure()
local this = VIRTUAL
function PUBLIC.Set(x, y)
this.x = x + 0
this.y = y + 0
end
PUBLIC.Set(x, y) -- initialization
function PUBLIC.Get()
return this.x, this.y
end
function VIRTUAL.Draw()
print( '\tDraw Point', this.x, this.y )
end
return PUBLIC, VIRTUAL
end
local function Text(x, y, txt)
local PUBLIC, VIRTUAL = Point(x, y)
local this = VIRTUAL
local SUPER = { Set = PUBLIC.Set, Get = PUBLIC.Get }
function PUBLIC.Set(x, y, txt)
SUPER.Set(x, y)
this.txt = txt .. ''
end
PUBLIC.Set(x, y, txt)
function PUBLIC.Get()
return this.x, this.y, this.txt
end
function VIRTUAL.Draw()
print( '\tDraw Text', this.x, this.y, this.txt )
end
return PUBLIC, VIRTUAL
end
local function Rect(x, y, w, h)
local PUBLIC, VIRTUAL = Point(x, y)
local this = VIRTUAL
local SUPER = { Set = PUBLIC.Set, Get = PUBLIC.Get }
function PUBLIC.Set(x, y, w, h)
SUPER.Set(x, y)
this.w = w + 0
this.h = h + 0
end
PUBLIC.Set(x, y, w, h)
function PUBLIC.Get()
return this.x, this.y, this.w, this.h
end
VIRTUAL.Draw = nil
return PUBLIC, VIRTUAL
end
local function Rectangle(x, y, w, h)
local PUBLIC, VIRTUAL = Rect(x, y, w, h)
local this = VIRTUAL
function VIRTUAL.Draw()
print( '\tDraw Rectangle', this.x, this.y, this.w, this.h )
end
-- define operator
function PUBLIC.__tostring()
return ("{ Rectangle %d, %d, %d, %d }"):format(this.x, this.y, this.w, this.h)
end
setmetatable(PUBLIC, PUBLIC)
return PUBLIC, VIRTUAL
end
local function Ellipse(x, y, w, h)
local PUBLIC, VIRTUAL = Rect(x, y, w, h)
local this = VIRTUAL
function VIRTUAL.Draw()
print( '\tDraw Ellipse', this.x, this.y, this.w, this.h )
end
function VIRTUAL.__tostring()
return ("{ Ellipse %d, %d, %d, %d }"):format(this.x, this.y, this.w, this.h)
end
setmetatable(PUBLIC, VIRTUAL)
return PUBLIC, VIRTUAL
end
local point1 = Point(5, 99)
local point2 = Point(333, 7)
point1.Draw()
point2.Draw()
point1.Draw()
local rect1 = Rectangle(2, 5, 20, 30)
rect1.Draw()
local rect2 = Rectangle(100, 100, 100, 100)
rect2.Draw()
rect1.Draw()
rect1.Set(1, 1, 1, 1)
rect1.Draw()
print(rect1, '\n')
print(rect1.__tostring(), '\n')
local ell1 = Ellipse(33, 44, 55, 66)
ell1.Draw()
print( ell1, '\n' )
local text1 = Text(400, 200, 'Aaaaaaaaaaaaaaaaaaaaaa')
local text2 = Text(4, 5, 'Bbbbbbbbbbbbbbbbbbbbbb')
text1.Draw()
text2.Draw()
text2.Set(333, 333, '**********************')
text2.Draw()