
Lua에서는 __construct 함수가 없습니다. 대신, Lua는 __index, __newindex, __call, __gc, __eq, __lt, __le, __add, __sub, __mul, __div, __mod, __unm, __pow, __concat, __band, __bor, __bxor, __bnot, __shl, __shr, __shl, __shr 등과 같은 메타 함수를 제공합니다.
클래스를 생성하는 방법은 다음과 같습니다.
#hostingforum.kr
lua
-- Lua 클래스 생성 예제
local MyClass = {}
MyClass.__index = MyClass
function MyClass:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function MyClass:printMessage()
print("Hello, World!")
end
local obj = MyClass:new()
obj:printMessage()
Lua에서는 __init 함수가 없습니다. 대신, __index 메타 함수를 사용하여 초기화 작업을 수행할 수 있습니다. __index 메타 함수는 테이블에 존재하지 않는 키를 조회할 때 호출되는 함수입니다.
__construct 함수를 사용하는 경우 __init 함수를 사용할 수 없습니다. __construct 함수는 C++ 등 다른 언어에서 사용하는 함수입니다. Lua에서는 __index, __newindex, __call, __gc, __eq, __lt, __le, __add, __sub, __mul, __div, __mod, __unm, __pow, __concat, __band, __bor, __bxor, __bnot, __shl, __shr, __shl, __shr 등과 같은 메타 함수를 제공합니다.
2025-04-17 12:11