阿里云建站保证销售额,开网店软件,珠海做网站哪里公司好,成都电脑培训班零基础面向对象编程(OOP)是现代编程时代中使用最广泛的编程技术之一。
OOP的特征
类(Class) - 类是用于创建对象的可扩展模板。 对象(Objects) - 它是类的实例#xff0c;并为其分配了单独的内存空间。 继承(Inheritance) - 这是一个概…
面向对象编程(OOP)是现代编程时代中使用最广泛的编程技术之一。
OOP的特征
类(Class) - 类是用于创建对象的可扩展模板。 对象(Objects) - 它是类的实例并为其分配了单独的内存空间。 继承(Inheritance) - 这是一个概念一个类的变量和函数被另一类继承。 封装(Encapsulation) - 这是将数据和函数合并到一个类中的过程。
您可以借助Table和Lua的一流函数在Lua中实现面向对象。通过将函数和相关数据放入表中可以形成一个对象。继承可以在Meta的帮助下实现它为父对象中不存在的函数(方法)和字段提供了一种查找机制。
Lua中的Table具有独立其值的对象特征。具有相同值的两个对象是不同的对象而一个对象在不同时间可以具有不同的值但是它始终是同一对象。
让无涯教程考虑一个简单的数学示例。经常遇到需要处理不同形状(如圆形矩形和正方形)的情况。
形状可以具有公共属性Area。因此可以从具有公共属性区域的基础对象形状扩展其他形状。每个形状都可以具有自己的属性和函数如矩形可以具有属性长度宽度面积作为其属性以及printArea和calculateArea作为其函数。
创建类
下面显示了具有三个属性区域长度和宽度的矩形的简单类实现。它还具有printArea函数以打印计算出的区域。
-- Meta class
Rectangle {area 0, length 0, breadth 0}-- Derived class method newfunction 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-- Derived class method printAreafunction Rectangle:printArea ()print(The area of Rectangle is ,self.area)
end
创建对象
创建对象是为类分配内存的过程。每个对象都有其自己的内存并共享公共类数据。
rRectangle:new(nil,10,20)
访问属性
无涯教程可以使用点运算符访问类中的属性如下所示:
print(r.length)
访问函数
您可以使用带有该对象的冒号运算符访问成员函数如下所示-
r:printArea()
分配内存并设置初始值。可以将初始化过程与其他面向对象语言的构造函数进行比较。
完整的示例
来看一个在Lua中使用面向对象的完整示例。
-- Meta class
Shape {area 0}-- Base class method newfunction Shape:new (o,side)o o or {}setmetatable(o, self)self.__index selfside side or 0self.area side*side;return o
end-- Base class method printAreafunction Shape:printArea ()print(The area is ,self.area)
end-- Creating an object
myshape Shape:new(nil,10)myshape:printArea()
当您运行上述程序时您将获得以下输出。
The area is 100
Lua 继承
继承是将简单的基础对象(如形状)扩展为矩形正方形等的过程。它在现实世界中经常用于共享和扩展基本属性和函数。
来看一个简单的类扩展。有一个如下所示的类。
-- Meta class
Shape {area 0}-- Base class method newfunction Shape:new (o,side)o o or {}setmetatable(o, self)self.__index selfside side or 0self.area side*side;return o
end-- Base class method printAreafunction Shape:printArea ()print(The area is ,self.area)
end
可以将形状扩展到方形如下所示。
Square Shape:new()-- Derived class method newfunction Square:new (o,side)o o or Shape:new(o,side)setmetatable(o, self)self.__index selfreturn o
end
Lua 覆盖
可以覆盖基类函数而不是使用基类中的函数派生类可以有自己的实现如下所示:
-- Derived class method printAreafunction Square:printArea ()print(The area of square is ,self.area)
end
Lua 继承示例
无涯教程可以借助另一个新方法借助元表来扩展Lua中的简单类实现如上所示。基类的所有成员变量和函数都保留在子类中。
-- Meta class
Shape {area 0}-- Base class method newfunction Shape:new (o,side)o o or {}setmetatable(o, self)self.__index selfside side or 0self.area side*side;return o
end-- Base class method printAreafunction Shape:printArea ()print(The area is ,self.area)
end-- Creating an object
myshape Shape:new(nil,10)
myshape:printArea()Square Shape:new()-- Derived class method newfunction Square:new (o,side)o o or Shape:new(o,side)setmetatable(o, self)self.__index selfreturn o
end-- Derived class method printAreafunction Square:printArea ()print(The area of square is ,self.area)
end-- Creating an object
mysquare Square:new(nil,10)
mysquare:printArea()Rectangle Shape:new()-- Derived class method newfunction Rectangle:new (o,length,breadth)o o or Shape:new(o)setmetatable(o, self)self.__index selfself.area length * breadthreturn o
end-- Derived class method printAreafunction Rectangle:printArea ()print(The area of Rectangle is ,self.area)
end-- Creating an objectmyrectangle Rectangle:new(nil,10,20)
myrectangle:printArea()
当运行上面的程序时将获得以下输出-
The area is 100
The area of square is 100
The area of Rectangle is 200
在上面的示例中从基类Square创建了两个派生类-Rectangle和Square。可以在派生类中重写基类的函数。在此示例中派生类覆盖函数printArea。 Lua - 面向对象 - 无涯教程网无涯教程网提供面向对象编程(OOP)是现代编程时代中使用最广泛的编程技术之一。 OOP的特征 类(Class) ...https://www.learnfk.com/lua/lua-object-oriented.html 文章转载自: http://www.morning.nsppc.cn.gov.cn.nsppc.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.bmtkp.cn.gov.cn.bmtkp.cn http://www.morning.sgcdr.com.gov.cn.sgcdr.com http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn http://www.morning.mmynk.cn.gov.cn.mmynk.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.yrkdq.cn.gov.cn.yrkdq.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.lczxm.cn.gov.cn.lczxm.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.cbmqq.cn.gov.cn.cbmqq.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.dqxnd.cn.gov.cn.dqxnd.cn http://www.morning.qrmry.cn.gov.cn.qrmry.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.rjcqb.cn.gov.cn.rjcqb.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn