簡潔なクラス表記を実現する†Lua5.1用 http://lua-class-lib.googlecode.com/svn/trunk/cls.lua クラスの定義方法(cls_demo.lua)†require 'cls' module(..., package.seeall) class 'Person' { __init__ = function(self, name) self.name = name end; say = function(self) print('Hello, my name is ' .. self.name .. '.') self:saySthElse() end; saySthElse = function(self) end } class 'Worker' { __init__ = function(self, id) self.id = id end; showId = function(self) print('My worker id is ' .. self.id .. '.') end } class 'Employee: Person, Worker' { __init__ = function(self, name, salary, id) Person.__init__(self, name) Worker.__init__(self, id) self.salary = salary end; saySthElse = function(self) print('My salary is ' .. self.salary .. '.') end } 定義したクラスの使用方法(cls_test.lua)†require 'cls_demos' print '--------------------------------------------' p = cls_demos.Person('Bob') p:say() p2 = cls_demos.Person('David') p2:say() print '--------------------------------------------' e = cls_demos.Employee('Bob', 1000, 1) e:say() e:showId() e2 = cls_demos.Employee('Alice', 10000, 2) e2:say() e2:showId() print '--------------------------------------------' if isInstanceOf(e, cls_demos.Person) then print 'e is an instance of Person' else print 'e is not an instance of Person' end if isInstanceOf(e, cls_demos.Worker) then print 'e is an instance of Worker' else print 'e is not an instance of Worker' end w = cls_demos.Worker(100) if isInstanceOf(w, cls_demos.Person) then print 'w is an instance of Person' else print 'w is not an instance of Person' end if isInstanceOf(w, cls_demos.Worker) then print 'w is an instance of Worker' else print 'w is not an instance of Worker' end |