建议开lua板块,借地问个简单的问题

xiao0915 2009-07-12 01:57:47
有个表def = {foo = "int,int",name = "string",nie = "property int",lot = "int,string",}在文件“def.lua”里

1.如何判断表中没有的属性访问非法,如def.foo1 = “123”非法,def.name = “china”合法

2.如何动态判断函数的参数传递合法,如def.foo(1,2)合法 def.foo(1,"125")非法,def.foo(1,2,3)非法
请动态的判断,不要每个函数都用type来判断为number或string

3.当def.name = "china"时怎么调用隐式函数function setname()(已定义的)

请给相关建议,思想,源码都可
...全文
197 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
XDF09042195 2010-07-20
  • 打赏
  • 举报
回复
LUA新人飘过,没看懂等我学会看懂了LZ在结贴把
vanchristin 2010-07-20
  • 打赏
  • 举报
回复
哇,lua是什么东东,好高级
iambic 2010-07-20
  • 打赏
  • 举报
回复
1. Metatable
2. 没看明白。def.foo明明是个字符串,你把它当成函数调用?
3. Metatable
xxf012005 2009-07-19
  • 打赏
  • 举报
回复
1.判断属性的值,为nil就非法,否则再判断参数的类型

2.写个函数判断int,double等返回为number

3.闭包函数写进index和——newindex进行表的监控

上述的访问都放在元表里即能实现动态判断
Nio96 2009-07-12
  • 打赏
  • 举报
回复
不会。。。
ies_sweet 2009-07-12
  • 打赏
  • 举报
回复
不懂lua
帮顶
fsy412 2009-07-12
  • 打赏
  • 举报
回复
第一个问题简单
Lua 5.0.3 Copyright (C) 1994-2006 Tecgraf, PUC-Rio
> t = { 11, 22, 33, you='one', me='two' }
> table.foreach(t,print)
1 11
2 22
3 33
me two
you one
>
> = t[2]
22
> = t.me
two
> = t.fred
nil


Indexing the table with a valid index returns the value stored at that index.Indexing the table with an undefined index returns nil

第三个问题要用到 _index,涉及到Metamethod和Metatable;
比如:
> print(s.length) -- no such key in the String table class
nil
> mt.__index = function (t,key)
>> if key == 'length' then return string.len(t.value) end
>> end
> print(s.length) -- new __index event calls the above function
6

xiao0915 2009-07-12
  • 打赏
  • 举报
回复
等着结贴啊
lua高手呢
大前置 2009-07-12
  • 打赏
  • 举报
回复
帮顶~~~
xiao0915 2009-07-12
  • 打赏
  • 举报
回复
没人会?
Walf_ghoul 2009-07-12
  • 打赏
  • 举报
回复
重载?
jiangkeredgirl 2009-07-12
  • 打赏
  • 举报
回复
重载 = 可以解决吧

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧