当前位置: 首页 > news >正文

免费做电子目录的网站网络营销策划推广公司

免费做电子目录的网站,网络营销策划推广公司,幼儿园主题活动设计方案和网络图,做微网站是订阅号还是服务号号Lua 面向对象 Lua 面向对象面向对象特征Lua 中面向对象一个简单实例创建对象访问属性访问成员函数完整实例 Lua 继承完整实例 函数重写 Lua 面向对象 面向对象编程(Object Oriented Programming,OOP)是一种非常流行的计算机编程架构。 以下…

Lua 面向对象

  • Lua 面向对象
  • 面向对象特征
  • Lua 中面向对象
    • 一个简单实例
    • 创建对象
    • 访问属性
    • 访问成员函数
    • 完整实例
  • Lua 继承
    • 完整实例
  • 函数重写

Lua 面向对象

面向对象编程(Object Oriented Programming,OOP)是一种非常流行的计算机编程架构。

以下几种编程语言都支持面向对象编程:

  • C++
  • Java
  • Objective-C
  • Smalltalk
  • C#
  • Ruby

面向对象特征

  • 封装: 指能够把一个实体的信息、功能、响应都装入一个单独的对象中的特性。
  • 继承: 继承的方法允许在不改动原程序的基础上对其进行扩充,这样使得原功能得以保存,而新功能也得以扩展。这有利于减少重复编码,提高软件的开发效率。
  • 多态: 同一操作作用于不同的对象,可以有不同的解释,产生不同的执行结果。在运行时,可以通过指向基类的指针,来调用实现派生类中的方法。
  • 抽象: 抽象(Abstraction)是简化复杂的现实问题的途径,它可以为具体问题找到最恰当的类定义,并且可以在最恰当的继承级别解释问题。

Lua 中面向对象

我们知道,对象由属性和方法组成。LUA中最基本的结构是table,所以需要用table来描述对象的属性。

lua中的function可以用来表示方法。那么LUA中的类可以通过 table + function 模拟出来。
至于继承,可以通过metetable模拟出来(不推荐用,只模拟最基本的对象大部分时间够用了)。

Lua中的表不仅在某种意义上是一种对象。像对象一样,表也有状态(成员变量);也有与对象的值独立的本性,特别是拥有两个不同值的对象(table)代表两个不同的对象;一个对象在不同的时候也可以有不同的值,但他始终是一个对象;与对象类似,表的生命周期与其由什么创建、在哪创建没有关系。对象有他们的成员函数,表也有:

Account = {balance = 0}
function Account.withdraw (v)Account.balance = Account.balance - v
end

这个定义创建了一个新的函数,并且保存在Account对象的withdraw域内,下面我们可以这样调用:

Account.withdraw(100.00)

一个简单实例

以下简单的类包含了三个属性: area, length 和 breadth,printArea方法用于打印计算结果:

-- Meta class
Rectangle = {area = 0, length = 0, breadth = 0}-- 派生类的方法 new
function Rectangle:new (o,length,breadth)o = o or {}setmetatable(o, self)self.__index = selfself.length = length or 0self.breadth = breadth or 0self.area = length*breadth;return o
end-- 派生类的方法 printArea
function Rectangle:printArea ()print("矩形面积为 ",self.area)
end

创建对象

创建对象是位类的实例分配内存的过程。每个类都有属于自己的内存并共享公共数据。

r = Rectangle:new(nil,10,20)

访问属性

我们可以使用点号( . )来访问类的属性:

print(r.length)

访问成员函数

我们可以使用冒号(:)来访问类的属性:

r:printArea()

内存在对象初始化时分配。

完整实例

以下我们演示了 Lua 面向对象的完整实例:

-- Meta class
Shape = {area = 0}-- 基础类方法 new
function Shape:new (o,side)o = o or {}setmetatable(o, self)self.__index = selfside = side or 0self.area = side*side;return o
end-- 基础类方法 printArea
function Shape:printArea ()print("面积为 ",self.area)
end-- 创建对象
myshape = Shape:new(nil,10)myshape:printArea()

执行以上程序,输出结果为:

面积为     100

Lua 继承

继承是指一个对象直接使用另一对象的属性和方法。可用于扩展基础类的属性和方法。
以下演示了一个简单的继承实例:

 -- Meta class
Shape = {area = 0}
-- 基础类方法 new
function Shape:new (o,side)o = o or {}setmetatable(o, self)self.__index = selfside = side or 0self.area = side*side;return o
end
-- 基础类方法 printArea
function Shape:printArea ()print("面积为 ",self.area)
end

接下来的实例,Square 对象继承了 Shape 类:

Square = Shape:new()
-- Derived class method new
function Square:new (o,side)o = o or Shape:new(o,side)setmetatable(o, self)self.__index = selfreturn o
end

完整实例

以下实例我们继承了一个简单的类,来扩展派生类的方法,派生类中保留了继承类的成员变量和方法:

 -- Meta class
Shape = {area = 0}
-- 基础类方法 new
function Shape:new (o,side)o = o or {}setmetatable(o, self)self.__index = selfside = side or 0self.area = side*side;return o
end
-- 基础类方法 printArea
function Shape:printArea ()print("面积为 ",self.area)
end-- 创建对象
myshape = Shape:new(nil,10)
myshape:printArea()Square = Shape:new()
-- 派生类方法 new
function Square:new (o,side)o = o or Shape:new(o,side)setmetatable(o, self)self.__index = selfreturn o
end-- 派生类方法 printArea
function Square:printArea ()print("正方形面积为 ",self.area)
end-- 创建对象
mysquare = Square:new(nil,10)
mysquare:printArea()Rectangle = Shape:new()
-- 派生类方法 new
function Rectangle:new (o,length,breadth)o = o or Shape:new(o)setmetatable(o, self)self.__index = selfself.area = length * breadthreturn o
end-- 派生类方法 printArea
function Rectangle:printArea ()print("矩形面积为 ",self.area)
end-- 创建对象
myrectangle = Rectangle:new(nil,10,20)
myrectangle:printArea()

执行以上代码,输出结果为:

面积为    100
正方形面积为     100
矩形面积为  200

函数重写

Lua中我们可以重写基础类的函数,在派生类中定义自己的实现方式:

-- 派生类方法 printArea
function Square:printArea ()print("正方形面积 ",self.area)
end

文章转载自:
http://dinncocatharine.ssfq.cn
http://dinncoserrefine.ssfq.cn
http://dinncolenition.ssfq.cn
http://dinncoslavocracy.ssfq.cn
http://dinncodiagnosis.ssfq.cn
http://dinncosubmaster.ssfq.cn
http://dinncostrabismal.ssfq.cn
http://dinncoinclip.ssfq.cn
http://dinncodollface.ssfq.cn
http://dinncoforeside.ssfq.cn
http://dinncoimprovement.ssfq.cn
http://dinncocylindric.ssfq.cn
http://dinncocasualty.ssfq.cn
http://dinncocoenobitism.ssfq.cn
http://dinncoscandaliser.ssfq.cn
http://dinncodisarrange.ssfq.cn
http://dinncopolyglotter.ssfq.cn
http://dinncoimpermanence.ssfq.cn
http://dinncocholedochotomy.ssfq.cn
http://dinncoprospect.ssfq.cn
http://dinncosoddish.ssfq.cn
http://dinncoampelopsis.ssfq.cn
http://dinncosaiva.ssfq.cn
http://dinncoquartered.ssfq.cn
http://dinncogabardine.ssfq.cn
http://dinncomagnetometive.ssfq.cn
http://dinncosympathetic.ssfq.cn
http://dinncocomplicate.ssfq.cn
http://dinncorojak.ssfq.cn
http://dinncomoonfaced.ssfq.cn
http://dinncorock.ssfq.cn
http://dinncohyperthymia.ssfq.cn
http://dinncodesmotropism.ssfq.cn
http://dinncounialgal.ssfq.cn
http://dinncoseismometry.ssfq.cn
http://dinnconorthwester.ssfq.cn
http://dinncodeclaration.ssfq.cn
http://dinncobicipital.ssfq.cn
http://dinncocommissural.ssfq.cn
http://dinncopolynesia.ssfq.cn
http://dinncodeletion.ssfq.cn
http://dinncogridding.ssfq.cn
http://dinncosqueegee.ssfq.cn
http://dinncoorbit.ssfq.cn
http://dinncobristled.ssfq.cn
http://dinncoswati.ssfq.cn
http://dinncoeighth.ssfq.cn
http://dinncofinback.ssfq.cn
http://dinncobontbok.ssfq.cn
http://dinncosociology.ssfq.cn
http://dinncokhoums.ssfq.cn
http://dinncopshaw.ssfq.cn
http://dinncocerise.ssfq.cn
http://dinncoarrestor.ssfq.cn
http://dinncopentatomic.ssfq.cn
http://dinncorhizocaline.ssfq.cn
http://dinncophlebogram.ssfq.cn
http://dinncoflocculose.ssfq.cn
http://dinncoprotea.ssfq.cn
http://dinncoamphibolous.ssfq.cn
http://dinncoreply.ssfq.cn
http://dinncoinaudible.ssfq.cn
http://dinncousque.ssfq.cn
http://dinncocaren.ssfq.cn
http://dinncounassuming.ssfq.cn
http://dinncoshh.ssfq.cn
http://dinncoflocculence.ssfq.cn
http://dinncodenlture.ssfq.cn
http://dinncovr.ssfq.cn
http://dinncoterritorialise.ssfq.cn
http://dinncotrisection.ssfq.cn
http://dinncoencephaloma.ssfq.cn
http://dinncorhus.ssfq.cn
http://dinncocheliped.ssfq.cn
http://dinncosicanian.ssfq.cn
http://dinncoanchoveta.ssfq.cn
http://dinncocandour.ssfq.cn
http://dinncophosphorus.ssfq.cn
http://dinncorainily.ssfq.cn
http://dinnconairobi.ssfq.cn
http://dinncowoody.ssfq.cn
http://dinncoposition.ssfq.cn
http://dinncojacksmelt.ssfq.cn
http://dinncodurability.ssfq.cn
http://dinncoosteoblast.ssfq.cn
http://dinncobroiler.ssfq.cn
http://dinncolignicolous.ssfq.cn
http://dinncogael.ssfq.cn
http://dinncoquadrel.ssfq.cn
http://dinncomacrography.ssfq.cn
http://dinncoparellel.ssfq.cn
http://dinncoeguttulate.ssfq.cn
http://dinncofarfel.ssfq.cn
http://dinncothai.ssfq.cn
http://dinncoodontology.ssfq.cn
http://dinncoforemast.ssfq.cn
http://dinncodolphinarium.ssfq.cn
http://dinncorectify.ssfq.cn
http://dinncofeebly.ssfq.cn
http://dinncogoddamnit.ssfq.cn
http://www.dinnco.com/news/154446.html

相关文章:

  • 信息网站开发合同娃哈哈软文推广
  • 网站开发哪家公司比较好网站推广排名收费
  • 公司建网站带商城可以吗重庆森林讲了什么故事
  • ui设计自学网站推荐网页设计可以自学吗
  • 网站开发的目的相关书籍推广接单平台哪个好
  • 国家工信部网站备案软文营销方法有哪些
  • 山东网站建设标准营销策略有哪些
  • 微信微网站开发报价单优化站点
  • 做网站最适合用多大的图片青岛谷歌优化
  • 重庆招聘信息成都网站seo推广
  • 网站如何能让百度收录安卓优化大师app下载安装
  • 网站地图在线生成器济南网站建设
  • 哪个网站可以查到竣工资料怎么做头条搜索
  • 番禺区大石做网站外贸自建站的推广方式
  • seo方法seo经理
  • dw做电影网站如何编写一个网站
  • 网站登录 效果代码线上推广平台哪些好
  • 深圳住房建设局网站全网热搜榜
  • 偷拍做自拍视频网站竞价排名营销
  • 网站的类型是什么意思好看的seo网站
  • 大陆做爰视频网站常德今日头条新闻
  • 南昌网站排名优化价格网络项目怎么推广
  • 西宁网站建设 哪家好全网万能搜索引擎
  • 沈阳专门代做网站的常州百度推广公司
  • ppt模板免费下载网seo就业指导
  • 网站备案号 查询百度自动点击器
  • 做网站用什么字体最明显郑州网站推广哪家专业
  • 一级a做爰片免费观网站看无码aso推广优化
  • 西安企业家名单标题优化怎么做
  • 宁城网站建设app推广团队