*簡潔なクラス表記を実現する [#ja8ae593]

Lua5.1用
Lua5.1用 [[http://lua-class-lib.googlecode.com/svn/trunk/cls.lua>http://lua-class-lib.googlecode.com/svn/trunk/cls.lua]]

&aname(lua_class_lib_51);


&aname(class-def);
**クラスの定義方法(cls_demo.lua) [#l5258014]
#sh(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
}

}}

&aname(class-use);
**定義したクラスの使用方法(cls_test.lua) [#uf15161c]
#sh(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

}}

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS