1 Star 0 Fork 0

roubincode / skyClass

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

skyClass

skyClasslua的基于元表的class

功能:

  • 构造函数
  • 继承
  • 允许自定义元方法
  • 简单反射
  • 类型绑定

提示

该库加入了基于EmmyLua的类型注释,建议使用EmmyLua插件以获得较好的类型提示

使用方法

创建类

require("class")

---@class a
a = class("a")

-- 构造函数
function a:ctor(name)
    self.name = name
    self.age = 0
end

-- 创建实例
local a1 = a:new("张三")
a1.age = 12
local a2 = a:new("李四")
a2.age = 14

print(a1.name,a1.age)
print(a2.name,a2.age)

输出

张三    12
李四    14

类方法

require("class")

---@class a
a = class("a")

function a:ctor(name)
    self.name = name
    self.age = 0
end

---* 自定义方法
---@pram year number
function a:grow(year)
    self.age = self.age + year
end

local a1 = a:new("张三")
a1.age = 12
a1:grow(4)
print(a1.name,a1.age)

输出

张三    16

类静态成员

require("class")

---@class a
a = class("a")

a.MONEY = 0
function a:ctor(name)

end

---@parm num number
function a.AddMoney(num)
    a.MONEY = a.MONEY + num
end

a.AddMoney(1000)
local a1 = a:new()
a1.AddMoney(1000)

print(a.MONEY,a1.MONEY)

输出

2000    2000

类继承

require("class")

---@class a
a = class("a")

function a:ctor(name)
    self.name = name
    self.age = 0
end

---@pram year number
function a:grow(year)
    self.age = self.age + year
end

local super
---@class b:a
b = class("b",a)

function b:ctor(name)
    a.ctor(self,name)
    self.money = 0
end

---@pram num number
function b:earn(num)
    self.money = self.money + num
end

local b1 = b:new("李四")
b1.age = 14
b1:grow(5)
b1:earn(5000)

print(b1.name,b1.age,b1.money)

输出

李四    19      5000

元方法定义

require("class")
---@class a
a = class("a")

function a:ctor()
    self.num = 0
end

function a:__add(other)
    local new = a:new()
    new.num = self.num + other.num
    return new
end

local a1 = a:new()
a1.num = 10
local a2 = a:new()
a2.num = 5
local a3 = a1 + a2
print(a1.num,a2.num,a3.num)

输出

10      5       15

注意:不要覆盖__index__newindex方法

MIT License Copyright (c) 2021 剑圣 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

a lua class 展开 收起
Lua
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/roubincode/skyClass.git
git@gitee.com:roubincode/skyClass.git
roubincode
skyClass
skyClass
master

搜索帮助