Lua基础:Packages

本文详细介绍了在Lua编程语言中如何设计和实现一个复数类,包括复数的加、减、乘、除等基本运算,以及如何通过私有成员和局部函数保护数据的完整性。此外,还讨论了不同封装方式对代码结构的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Packages:组织全局变量的命名

  1. complex={}   --全局名complex
  2.  
  3. function complex.new(r,i) return {r=r,i=i} end
  4.  
  5. complex.i=complex.new(0,1)
  6.  
  7. function complex.add(c1,c2)
  8.     return complex.new(c1.r+c2.r,c1.i+c2.i)
  9. end
  10.  
  11. function complex.sub(c1,c2)
  12.     return complex.new(c1.r-c2.r,c1.i-c2.i)
  13. end
  14.  
  15. function complex.mul(c1,c2)
  16.     return complex.new(c1.r*c2.r-c1.i*c2.i,c1.r*c2.i+c1.i*c2.r)
  17. end
  18.  
  19. function complex.inv(c)
  20.     local n=c.r^2+c.i^2
  21.     return complex.new(c.r/n,-c.i/n)
  22. end
  23.  
  24. return complex

私有成员Privacy:将私有部分定义为局部变量local修饰

  1. local P={}                    
  2.  
  3. complex=P
  4.  
  5. local function checkComplex(c)
  6.     if not((type(c)=="table") and tonumber(c.r) and tonumber(c.i)) then
  7.         error("bad complex number",3)
  8.     end
  9. end
  10.  
  11. function P.add(c1,c2)
  12.     checkComplex(c1);
  13.     checkComplex(c2);
  14.     return P.new(c1.r+c2.r,c1.i+c2.i)
  15. end
  16.  
  17. return P
  18. -------------------------------------------------------------------
  19. 方法二:将所有函数都声明为局部的,然后放到表中
  20. local function checkComplex(c)
  21.     if not((type(c)=="table") and tonumber(c.r) and tonumber(c.i)) then
  22.         error("bad complex number",3)
  23.     end
  24. end
  25.  
  26. local function new(r,i) return {r=r,i=i} end
  27.  
  28. local function add(c1,c2)
  29.     checkComplex(c1);
  30.     checkComplex(c2);
  31.     return new(c1.r+c2.r,c1.i+c2.i)
  32. end
  33.  
  34. complex={
  35.     new=new,
  36.     add=add,
  37.     sub=sub,
  38.     mul=mul,
  39.     div=div,
  40. }

包与文件:写一个 package 然后将所有的代码放到一个单独的文件中。然后我们只需要执行这个文件即加载 package。

  1. local P={}
  2.     if _REQUIREDNAME==nil then
  3.         complex=P
  4.     else
  5.         _G[_REQUIREDNAME]=P
  6. end

使用全局表:

  1. local P={}
  2.  
  3. complex=P
  4.  
  5. setfenv(1,P)
  6.  
  7. function add(c1,c2)             --自动转变成complex.add
  8.     return new(c1.r+c2.r,c1.i+c2.i)
  9. end
  10. -------------------------------------------------------------
  11. local P={}
  12.     if _REQUIREDNAME==nil then
  13.         complex=P
  14.     else
  15.         _G[_REQUIREDNAME]=P
  16. end
  17. setfenv(1,P)
  18. -----------------------------------------------------------
  19. --使用继承
  20. local P={}
  21. setmetatable(P,{_index=_G})
  22. setfenv(1,P)
  23. ------------------------------------------------------------
  24. --声明一个局部变量来保存老的环境,外部访问要加上前缀_G
  25. local P={}
  26. pack=P
  27. local _G=_G
  28. setfenv(1,P)
  29. -----------------------------
  30. local P={}
  31. pack=P
  32. local sqrt=math.sqrt
  33. local io=io
  34. setfenv(1,P)
  35.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值