Lua面向对象

简介: Lua面向对象
print("=================面向对象=================")
Student = {
    age = 1, 
    sex = true,
    Up = function ()
        print(Student.age)
        print("Growing up")
    end,
    Learn = function()
        print("好好学习")
    end
}
Student.name = "guan"
function Student:Speak2()
    print(self.name .. " Speak")
end
-- Lua中.和冒号的区别
-- 冒号的方法第一个参数默认传入self表示当前实例
Student:Speak2()
print("=================封装=================")
Object = {}
Object.id = 1
function Object:Test()
    print(self.id)
end
function Object:new()
    -- 对象就是变量 返回一个新的变量
    -- 返回出去的内容 本质上就是表对象
    local obj = {}
    self.__index = self
    setmetatable(obj, self)
    -- index当找自己的变量,找不到是 就时去元表中找__index指向的内容
    return obj
end
print("=================继承=================")
function Object:subClass(className)
    -- _G声明全局标量,以键值对的形式
    _G[className] = {}
    -- 继承规则
    local obj = _G[className]
    self.__index = self
    obj.base = self
    setmetatable(obj, self)
end
Object:subClass("Person")
local p1 = Person:new()
p1.id = 100
print(p1:Test())
Object:subClass("Monster")
local m1 = Monster:new()
print(m1.id)
print("=================多态=================")
-- 相同方法 不同表象就是多态
-- 相同方法 不同执行逻辑 就是多态
Object:subClass("GameObject") 
GameObject.posX = 0
GameObject.posY = 0
function GameObject:Move()
    self.posX = self.posX + 1
    self.posY = self.posY + 1
    print(self.posX)
    print(self.posY)
end
GameObject:subClass("Player")
-- 重写Move()方法
function Player:Move()
    -- base.Move() C#写法
    -- 添加Obj.base = self 在父类中
    -- self指的是Gameobject
    -- 避免把基类表传入到方法中
    -- 谁调用,self就是指谁
    self.base.Move(self) -- 执行GameObject:Move()
end
local p1 = Player:new()
p1:Move()
local p2 = Player:new()
p2:Move()

汇总笔记

-- 面向对象
Object = {}
--实例化方法
function Object:new()
    local obj = {}
    self.__index = self
    setmetatable(obj, self)
    return obj
end
-- 继承
function Object:subClass(className)
    _G[className] = {}
    local obj = _G[className]
    -- 设置自己的父类表
    obj.base = self
    --给子类设置元表以及__index
    self.__index = self
    setmetatable(obj, self)
end
Object:subClass("GameObject")
GameObject.posX = 0
GameObject.posY = 0
function GameObject:Move()
    self.posX = self.posX + 1
    self.posY = self.posY + 1
end
-- 实例化对象使用
-- local obj = GameObject:new()
-- print(obj.posX)
-- obj:Move()
-- print(obj.posX)
-- local obj2 = GameObject:new()
-- print(obj2.posX)
-- obj2:Move()
-- print(obj2.posX)
GameObject:subClass("Player")
-- 多态重写了Gameobject的move方法
function Player:Move()
    -- 需要使用.进行手动传入self
    self.base.Move(self)
end
-- 实例化Player对象
local p1 = Player:new()
print(p1.posX)
p1:Move()
print(p1.posX)
local p2 = Player:new()
print(p2.posX)
p2:Move()
print(p2.posX)
相关文章
|
4月前
|
C++ 索引 Python
Lua中self 、自索引及其面向对象应用代码示例
Lua中self 、自索引及其面向对象应用代码示例
Lua程序设计(三)面向对象实现一个简单的类
1.Lua面向对象实现步骤 ①创建一个全局表(称之为元表) ②设置这个元表的__index值(值通常为元表自己,这样就能通过__index查找到对应的属性和方法)__index 赋值其实是一个function的语法糖,Sharp.
1249 0
Lua程序设计(四)面向对象类继承
1.类继承  ①代码 Sharp = { _val = 1} --① 父类 function Sharp:new() local new_sharp = { } self.__index = self --②,self == Sharp setmetatable(n...
827 0
|
索引
Lua程序设计(二)面向对象概念介绍
----------------------------------------------------------- Lua面向对象3 local smartMan = { name = "Tinywan", age = 26, money = 800000, ...
890 0
Lua程序设计(一)面向对象概念介绍
  完整代码 local mt = {} mt.__add = function(t1,t2) print("两个Table 相加的时候会调用我") end local t1 = {} local t2 = {} -- 给两个table 设置新的元表,一个元表...
969 0
|
1月前
|
存储 NoSQL Redis
Redis的Lua脚本有什么作用?
Redis Lua脚本用于减少网络开销、实现原子操作及扩展指令集。它能合并操作降低网络延迟,保证原子性,替代不支持回滚的事务。通过脚本,代码复用率提高,且可自定义指令,如实现分布式锁,增强Redis功能和灵活性。
41 1
|
2月前
|
缓存 NoSQL Java
【Redis】5、Redis 的分布式锁、Lua 脚本保证 Redis 命令的原子性
【Redis】5、Redis 的分布式锁、Lua 脚本保证 Redis 命令的原子性
63 0