クラスを使う 1

box = class()                        -- 何も継承せずに box クラスを作る。
function box:init(x, y, w, h, color) -- box クラスを初期化する。
    self.x = x
    self.y = y
    self.w = w
    self.h = h
    self.color = color
end
function box:paint(gc)               -- box クラスの描画メソッドを作る。
    gc:setColorRGB(unpack(self.color))
    gc:fillRect(self.x, self.y, self.w, self.h)
end

child = class(box)                   -- box クラスを継承して child クラスを作る。


-- 確かめてみる。
b = box(50, 30, 50, 150, {0, 255, 0})   -- box クラスをインスタンス b として実体化する。 
c = child(10, 50, 100, 30, {0, 0, 255}) -- child クラスをインスタンス c として実体化する。

function on.paint(gc)
    b:paint(gc) -- インスタンス b に描画メソッドを適用する。
    c:paint(gc) -- インスタンス c に、継承した描画メソッドを適用する。
end

実行結果:
f:id:ti-nspire:20160223091742j:plain:w400

参考:
http://www.johnhanna.us/pdf/Lua_4_All_Teachers.pdf