lua语言的学习(一.2)

2014年3月31日,星期一,晚21点21分。

这是今天第二更!不欠着了奥。

=========我是正经的分割线=======tclass例程=======

第一步就卡主了,if not package是啥意思。。。。等明天研究吧

=========我是昨天的分割线====4月1日=======

tclass.lua

下面对tclass的分析还很简陋,我发现这些可能用到的时候比较少,所以现在的目标改为:弄懂常用的语法,下一篇开始!


if not Test then--这是h文件中的namespace名
	local loadlib--定义一个局部变量
	if not package then--如果包名不存在
		loadlib = _G['loadlib'] 用全局函数
	else
		loadlib = package.loadlib否则用包中的函数
	end
	f, e, eo = loadlib("./libtclass.so", "luaopen_tclass")导入一个函数
	if f then
		f()
	else
		print(eo, e)
		os.exit()
	end
end

a = {}
rawset(a, ".c_instance", "something")新加一个变量,为其赋值

function hello()

	print("hello world")
end

rawset(Test.B, "hello", hello)

-- type convertion tests
--print(Test.A)
--print(tolua.type(Test.A.last))
--assert(tolua.type(Test.A.last) == 'Test::Tst_A') -- first time the object is mapped
--assert(tolua.type(Test.B.last) == 'Test::Tst_B') -- type convertion to specialized type
--assert(tolua.type(Test.A.last) == 'Test::Tst_B') -- no convertion: obj already mapped as B


local a = Test.A:new()
assert(tolua.type(Test.A.last) == 'Test::Tst_A') -- no type convertion: same type
local b = Test.B:new()
assert(tolua.type(Test.A.last) == 'Test::Tst_B') -- no convertion: obj already mapped as B
local c = Test.luaC:new(0)
assert(tolua.type(Test.A.last) == 'Test::Tst_C') -- no convertion: obj already mapped as C
assert(tolua.type(Test.luaC.last) == 'Test::Tst_C')

local aa = Test.A.AA:new()
local bb = Test.A.BB:new()
local xx = Test.create_aa()

-- method calling tests
assert(a:a() == 'A')
assert(b:a() == 'A')
assert(b:b() == 'B')
assert(c:a() == 'A')
assert(c:b() == 'B')
assert(c:c() == 'C')
assert(aa:aa() == 'AA')
assert(bb:aa() == bb:Base():aa())
assert(xx:aa() == 'AA')
assert(Test.is_aa(bb) == true)

-- test ownershipping handling
-- should delete objects: 6 7 8 9 10 (it may vary!)
local set = {}
for i=1,10 do
 local c = Test.luaC:new(i)
	if i>5 then
		tolua.takeownership(c)
	end
	--set[i] = c
end



e = Test.B:new_local()

print("e is type "..tolua.type(e))
print("ae is type "..tolua.type(ae))

--e:delete()

b:hello()

----------
local out = Test.Outside:new_local()
out:outside()
Test.Outside:outside_static(nil)


print "***** cast"
local acast = Test.B:create_void()
print("type is "..tolua.type(acast))
local bcast = tolua.cast(acast, "Test::Tst_B")
print("bcast is "..tostring(bcast))
print("type is "..tolua.type(bcast))
print(bcast:b())

-- test properies
local n = 7
a.number = n
assert(a.number == n*2)

-- constructors
print(getmetatable(Test.A))
print(getmetatable(Test.B))
print(getmetatable(Test.E))

local a = Test.A()
local b = Test.B()
local e = Test.E(5)
--print(e+5)
print(tostring(getmetatable(Test.B).__call))
print(tostring(Test.B.__call))
print(tostring(Test.B.__call(Test.B)))
print(tolua.type(b))

e:set_ptr(e)
local ve = tolua.cast(e:get_ptr(), "Test::Tst_E")
ve:set_ptr(ve)

print"1"
Test.A.pete = {}
print"2"
table.insert(Test.A.pete, a)
print"3"


for i=1,100000 do
	la = {}
	tolua.inherit(la, a)
end

print("Class test OK")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,这是一份我为您准备的Lua语言学习路径: 1. 安装Lua环境 在开始学习Lua之前,首先需要安装Lua环境。您可以从官方网站下载最新版本的Lua安装包:http://www.lua.org/download.html 。安装完成后,您就可以在命令行或者终端中输入“lua”命令,进入Lua解释器环境。 2. 学习Lua基础语法 Lua的语法非常简洁,易于学习。您可以从Lua官方网站的教程入手:http://www.lua.org/manual/5.1/ 。建议您先学习Lua的基础语法,如变量、表达式、控制流等。 3. 学习Lua面向对象编程 Lua支持面向对象编程,您可以学习Lua中的类、对象、继承和多态等面向对象编程的概念。建议您阅读Lua官方网站的教程:http://www.lua.org/pil/16.html 。 4. 学习Lua标准库 Lua标准库提供了丰富的API,涵盖了文件操作、字符串处理、网络编程等方面。建议您仔细阅读官方文档:http://www.lua.org/manual/5.1/ 。 5. 学习Lua扩展库 Lua社区提供了许多扩展库,涉及图形界面、数据库、Web开发等方面。您可以根据自己的需求选择相应的扩展库进行学习和使用。建议您浏览Lua社区的官方网站:http://lua-users.org/wiki/ModulesTutorial 。 6. 实践项目 最后,您可以通过实践项目来巩固所学的知识。您可以选择一些简单的Lua项目,如游戏、脚本等。这些项目可以帮助您深入理解Lua的运用,并提高您的编程能力。 希望这份学习路径对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值