[루아] Lua에서 .과 :의 차이(?) + OOP

2015. 12. 17. 03:11프로그래밍/Script

728x90
728x90

http://lua-users.org/wiki/ColonForMethodCall


일반적으로

인스턴스 메소드는 : 콜론을 사용하여 호출한다.

클래스나 팩토리 매소드, 필드를 접근할 때는 . 을 주로 사용한다.



OOP느낌으로?

https://github.com/ElementalKiss/Lua/blob/master/OopExample.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- Meta Class
Rectangle = {area = 0, length = 0}
 
-- Base Class
 
function Rectangle:New(cpyRect,length)
-- copy constructure?
   newRect = cpyRect or {}
   setmetatable(newRect, self)
   self.__index = self
 
 
   self.length = length or 0
   self.area = length*length;
   return newRect
end
 
-- Method
 
function Rectangle:printArea ()
   print("The area is = ",self.area)
end
 
-- Function
function Rectangle.printLenth (self)
   print("The length is = ", self.length)
end
 
-- Creating an object
myShape = Rectangle:New(nil,10)
myShape.printLenth(myShape)
myShape:printArea()
 
-- Creating an object using SomeClass
newShape = Rectangle:New(myshape, 20)
print("The length is = ", newShape.length)
newShape:printArea()
cs





728x90
반응형

'프로그래밍 > Script' 카테고리의 다른 글

[Lua, Json] lua에서 json 처리하기. json for lua  (3) 2016.01.15
개론  (2) 2015.10.01
Lua와 C 바인딩  (0) 2015.09.20
Lua 메타테이블  (0) 2015.09.20
Lua 5.x와 4.0 호환성  (0) 2015.09.20
Lua 조건문, 반복문  (0) 2015.09.19
Lua 연산자  (0) 2015.09.19