lua类库 middleclass学习笔记

简介: middleclass使在lua中面象对象变的简单 抄了一遍他的示例代码运行着试了试,基本懂了 local class = require 'middleclass' --类的继承 Person = class('Person') --定义一个Person类 function Person:initialize(name) --构造函数 self.

middleclass使在lua中面象对象变的简单

抄了一遍他的示例代码运行着试了试,基本懂了

local class = require 'middleclass'

--类的继承
Person = class('Person')  --定义一个Person类
function Person:initialize(name)  --构造函数
    self.name = name
end

function Person:speak() --方法
    print("Hi,i am " ..self.name .. ".")
end

AgedPerson = class('AgedPerson',Person) --子类
AgedPerson.static.ADULT_AGE = 18  --类成员

function AgedPerson:initialize(name,age)  --子类构造
    Person.initialize(self,name)
    self.age = age
end

function AgedPerson:speak()  --子类方法
    Person.speak(self)   --子类调用父类方法
    if(self.age < AgedPerson.ADULT_AGE) then
        print("i am underaged")
    else
        print("i am an adult")
    end
end

local p1 = AgedPerson:new('张东升',13) --定义一个子类对象
local p2 = AgedPerson:new('无量',21)
p1:speak()



--Mixins 这种特性可以在不同的类之间共享一些函数,
--可以是不同的基类
HasWings =
{
    fly = function(self)
        print('flap flap i am a ' .. self.class.name)
    end
}

Animal = class('Animal')  --动物类
Insect = class('Insect',Animal)  --昆虫类
Worm = class('Worm',Insect) --虫子
Bee = class('Bee',Insect) --蜜蜂
Bee:include(HasWings) --增加类成员
Mammal = class('Mammal',Animal) --哺乳动物
Fox = class('Fox',Mammal) --狐狸
Bat = class('Bat',Mammal) --蝙蝠
Bat:include(HasWings) --

local bee = Bee()
local bat = Bat()
bee:fly()
bat:fly()




--下在这种方法
DrinksCoffe = {}
function DrinksCoffe:drink(drinkTime)
    if drinkTime ~= self.class.coffeeTime then
        print(self.name .. ": It is not the time to drink coffee")
    else
        print(self.name .. ": Mmm I love coffee ad drinkTime")
    end
end

function DrinksCoffe:include(klass)
    print(klass.name .. " drinks coffee at " .. klass.coffeeTime)
end

EnglishMan = class('EnglishMan')
EnglishMan.static.coffeeTime = 5
EnglishMan:include(DrinksCoffe)
function EnglishMan:initialize(name) self.name = name end

Spaniard = class('Spaniard')
Spaniard.static.coffeeTime = 6
Spaniard:include(DrinksCoffe)
function Spaniard:initialize(name) self.name = name end

tom = EnglishMan:new('tom')
juan = Spaniard:new('juan')
tom:drink(5)
juan:drink(5)
juan:drink(6)



--为类增加元方法
Point = class('Point')
function Point:initialize(x,y)
    self.x = x
    self.y = y
end

function Point:__tostring()
    return 'Point:[' .. tostring(self.x) .. ',' .. tostring(self.y) .. ']'
end
p1 = Point(100,200)
print(p1)

 

相关文章
|
11月前
|
网络协议 安全 Linux
Linux C/C++之IO多路复用(select)
这篇文章主要介绍了TCP的三次握手和四次挥手过程,TCP与UDP的区别,以及如何使用select函数实现IO多路复用,包括服务器监听多个客户端连接和简单聊天室场景的应用示例。
267 0
|
程序员
Qualcomm QXDM工具简介和log抓取
高通工具简介 QXDM 简介 QXDM 安装 QXDM 激活 QXDM 使用AT打开Diagnostic口 QXDM 配置 1 Message View Configuration Message Packets Log Packets Log PacketsO...
6161 0
|
11月前
|
监控 关系型数据库 MySQL
深入了解MySQL主从复制:构建高效稳定的数据同步架构
深入了解MySQL主从复制:构建高效稳定的数据同步架构
305 1
|
11月前
|
存储 NoSQL 关系型数据库
Redis 有序集合(sorted set)
10月更文挑战第17天
244 4
|
6月前
|
消息中间件 安全 API
Apache RocketMQ ACL 2.0 全新升级
Apache RocketMQ ACL 2.0 全新升级
229 8
|
6月前
|
SQL 缓存 关系型数据库
SQL为什么不建议执行多表关联查询
本文探讨了SQL中不建议执行多表关联查询的原因,特别是MySQL与PG在多表关联上的区别。MySQL仅支持嵌套循环连接,而不支持排序-合并连接和散列连接,因此在多表(超过3张)关联查询时效率较低。文章还分析了多表关联查询与多次单表查询的效率对比,指出将关联操作放在Service层处理的优势,包括减少数据库计算资源消耗、提高缓存效率、降低锁竞争以及更易于分布式扩展等。最后,通过实例展示了如何分解关联查询以优化性能。
218 0
|
计算机视觉 Python
【Python】已解决:ModuleNotFoundError: No module named ‘PIL’
【Python】已解决:ModuleNotFoundError: No module named ‘PIL’
8386 0
|
存储 分布式计算 Apache
Spark编程范例:Word Count示例解析
Spark编程范例:Word Count示例解析
|
JavaScript
npm切换源,nrm安装、配置及使用
默认的npm源是国外的,速度比较慢。可以选择国内镜像,加快下载安装速度,比如我们可以切换到taobao源或者公司内部的源。
1293 1
npm切换源,nrm安装、配置及使用