Posted on 2009-09-15 17:43
kongkongzi 阅读(3530)
评论(0) 编辑 收藏 引用 所属分类:
lua
lua的命名空间、类、全局函数、命名空间下的全局函数、类的静态函数
冒号语法可以用来定义方法
Lua中的函数是带有词法定界(lexical scoping)的第一类值(first-class values)。
第一类值指:在Lua中函数和其他值(数值、字符串)一样,函数可以被存放在变量中,也可以存放在表中,可以作为函数的参数,还可以作为函数的返回值。
当我们提到函数名(比如print),实际上是说一个指向函数的变量,像持有其他类型值的变量一样。
函数定义实际上是一个赋值语句,将类型为function的变量赋给一个变量。
将第一类值函数应用在表中是Lua实现面向对象和包机制的关键。
Lua中函数可以作为全局变量也可以作为局部变量。
函数作为table的域(大部分Lua标准库使用这种机制来实现的比如io.read、math.sin)。
从C++调用lua;从lua调用lua。
多种宠物,按种类分类建立宠物对象,在每个对象中记录该种宠物的简介和信息。
pet = {}; -- 定义了一个table => lua中table的操作? table:Arrays、Hash tables
For each bound class, tolua creates a Lua table and stores it at a variable which name is the name of the C++ class. This tables may contain other tables that represent other tables, the way C++ classes may contain other classes and structs. Static exported fields are accessed by indexing this table with the field names (similar to struct fields). Static methods are also called using this table, with a colon. Non static exported fields are accessed by indexing the variable that holds the object. Class methods follow the format of the function declaration showed above. They can be accessed from Lua code using the conventional way Lua uses to call methods, applied of course to a variable that holds the appropriate object or to the class table, for static methods.
There are a few special methods that are also supported by tolua. Constructors are called as static methods, named new, new_local (on tolua++), or calling the class name directly (also on tolua++, see below for the difference betwheen these methods). Destructors are called as a conventional method called delete.
table 每个域中的值也可以是任何类型(除 nil外)。特别的,因为函数本身也是值,所以 table 的域中也可以放函数。这样 table 中就可以有一些 methods 了 (参见see §2.5.9)。