Lua语法(六)——面相对象编程

参考链接:
系列链接: Lua语法(一)
系列链接: Lua语法(二)——闭包/日期和时间
系列链接: Lua语法(三)——元表与元方法
系列链接: Lua语法(四)——协程
系列链接: Lua语法(五)——垃圾回收
系列链接: Lua语法(六)——面相对象编程

使用Lua表 进行类的模拟,从而面向对象编程

面向对象编程

类创建

Lua中没有类这个类型,所以只有用表来模拟类

创建类方法的方式

方式1:表内用键值对的方式

方式2:表外使用 : 冒号

方式3:表外使用 点 .

案例见下方代码

冒号和点的区别

冒号是点的缩写形式,可以省略传入自身这个参数

综合案例:

--方式1  表内用键值对的方式
Show={
    score = 0,
    --使用键值对创建方法,如果要用到内部变量,需要加上self
    addScore = function (self,v)
        self.score = self.score + v
        print(self.score)
    end
}

--方式2  使用 : 冒号可以省略传入自己这个参数
function Show:decScore(v)
    self.score = self.score -v
    print(self.score)
end

--方式3   使用 点 . 需要添加上self
function Show.mulScore(self,v)
    self.score = self.score + v*2
    print(self.score)
end

function Show:new(o)
    o = o or {}
    self.__index = self
    setmetatable(o,self)
    return o
end

local a = Show:new()
a:addScore(10)
a:decScore(5)
a.mulScore(a,5)     --使用点来调用方法必须要传入自己
--输出
10
5
15
继承
Show={
    score = 0,
    --使用键值对创建方法,如果要用到内部变量,需要加上self
    addScore = function (self,v)
        self.score = self.score + v
        print(self.score)
    end
}

--使用 : 冒号可以省略传入自己这个参数
function Show:decScore(v)
    self.score = self.score -v
    print(self.score)
end

--使用 点 . 需要添加上self
function Show.mulScore(self,v)
    self.score = self.score + v*2
    print(self.score)
end

function Show:new(o)
    o = o or {}
    self.__index = self
    setmetatable(o,self)
    return o
end
-------------继承自Show---------------
BigShow = Show:new()    --继承自Show

local big = BigShow:new{min =0}

big:addScore(10)
big:decScore(5)
-- big:mulScore(5)
big.mulScore(big,5)
print(big.min)

--输出
10
5
15
0

属性私有性—对偶表示

使用对偶表示,实现属性相对私有性。

local score ={}
Show = {}

function Show:addScore(v)
    score[self] = score[self] + v
end

function Show:decScore(v)
    score[self] = score[self] - v
end

function Show:score(v)
    return score[self]
end

function Show:new(o)
    o = o or {}
    setmetatable(o,self)
    self.__index = self
    score[o]=0  --初始化分数
    return o
end

local big = Show:new()
big:addScore(10)    --只能通过方法进行修改内部属性值
print(big:score())  --只能通过方法访问内部属性
print(big.score(big))

--输出
10
10

参考链接:
系列链接: Lua语法(一)
系列链接: Lua语法(二)——闭包/日期和时间
系列链接: Lua语法(三)——元表与元方法
系列链接: Lua语法(四)——协程
系列链接: Lua语法(五)——垃圾回收
系列链接: Lua语法(六)——面相对象编程

相关推荐

  1. Lua语法()——面相对象编程

    2024-04-13 05:16:02       96 阅读
  2. Python学习笔记(面向对象编程

    2024-04-13 05:16:02       55 阅读
  3. 21、Lua 面向对象

    2024-04-13 05:16:02       38 阅读
  4. Lua面向对象/C之间的交互

    2024-04-13 05:16:02       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 05:16:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 05:16:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 05:16:02       82 阅读
  4. Python语言-面向对象

    2024-04-13 05:16:02       91 阅读

热门阅读

  1. FlashDB学习笔记一

    2024-04-13 05:16:02       36 阅读
  2. CSS学习笔记

    2024-04-13 05:16:02       43 阅读
  3. HTML&CSS(二)---HTML常见标签

    2024-04-13 05:16:02       31 阅读
  4. 2024.4.12清空Google剩余的几个网址

    2024-04-13 05:16:02       42 阅读
  5. Objective-C网络请求开发的高效实现方法与技巧

    2024-04-13 05:16:02       42 阅读
  6. C++ 设计模式

    2024-04-13 05:16:02       39 阅读
  7. mysqlySQL中启用慢查询日志并设置阈值

    2024-04-13 05:16:02       33 阅读