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

越秀学校网站建设天津网站策划

越秀学校网站建设,天津网站策划,佛山专业的网站制作,微博图片怎么做外链到网站面向对象编程(OOP)是现代编程时代中使用最广泛的编程技术之一。 OOP的特征 类(Class) - 类是用于创建对象的可扩展模板。 对象(Objects) - 它是类的实例,并为其分配了单独的内存空间。 继承(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

创建对象

创建对象是为类分配内存的过程。每个对象都有其自己的内存并共享公共类数据。

r=Rectangle: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://dispope.cwgn.cn
http://trinomial.cwgn.cn
http://bliny.cwgn.cn
http://zilch.cwgn.cn
http://acrimoniously.cwgn.cn
http://fingerstall.cwgn.cn
http://lacklustre.cwgn.cn
http://ostium.cwgn.cn
http://dualin.cwgn.cn
http://norilsk.cwgn.cn
http://multienzyme.cwgn.cn
http://request.cwgn.cn
http://bedecked.cwgn.cn
http://sonorization.cwgn.cn
http://polycotyl.cwgn.cn
http://snowscape.cwgn.cn
http://tavern.cwgn.cn
http://epilator.cwgn.cn
http://intersperse.cwgn.cn
http://ironing.cwgn.cn
http://ccm.cwgn.cn
http://inaptly.cwgn.cn
http://whiz.cwgn.cn
http://salaud.cwgn.cn
http://ligniperdous.cwgn.cn
http://genf.cwgn.cn
http://atelectasis.cwgn.cn
http://betamethasone.cwgn.cn
http://sardelle.cwgn.cn
http://machan.cwgn.cn
http://pemphigoid.cwgn.cn
http://endive.cwgn.cn
http://carnassial.cwgn.cn
http://cretan.cwgn.cn
http://olla.cwgn.cn
http://countrywide.cwgn.cn
http://systematizer.cwgn.cn
http://monochromist.cwgn.cn
http://donkeywork.cwgn.cn
http://grue.cwgn.cn
http://porotic.cwgn.cn
http://unit.cwgn.cn
http://wispy.cwgn.cn
http://bastioned.cwgn.cn
http://think.cwgn.cn
http://threonine.cwgn.cn
http://ferrum.cwgn.cn
http://distillatory.cwgn.cn
http://deplethoric.cwgn.cn
http://agadir.cwgn.cn
http://ungainliness.cwgn.cn
http://dollop.cwgn.cn
http://fohn.cwgn.cn
http://disarrange.cwgn.cn
http://cisrhenane.cwgn.cn
http://epilate.cwgn.cn
http://cockabully.cwgn.cn
http://vermicelli.cwgn.cn
http://nuke.cwgn.cn
http://pogonotrophy.cwgn.cn
http://boatload.cwgn.cn
http://organon.cwgn.cn
http://attributive.cwgn.cn
http://bethel.cwgn.cn
http://indemnificatory.cwgn.cn
http://sorosilicate.cwgn.cn
http://morphinomaniac.cwgn.cn
http://acrostic.cwgn.cn
http://precopulatory.cwgn.cn
http://sidenote.cwgn.cn
http://integrable.cwgn.cn
http://wired.cwgn.cn
http://inertial.cwgn.cn
http://regulon.cwgn.cn
http://supersensible.cwgn.cn
http://wrangle.cwgn.cn
http://bisayan.cwgn.cn
http://pregnable.cwgn.cn
http://paling.cwgn.cn
http://kona.cwgn.cn
http://sugarhouse.cwgn.cn
http://turnscrew.cwgn.cn
http://crinkleroot.cwgn.cn
http://oblomov.cwgn.cn
http://mother.cwgn.cn
http://across.cwgn.cn
http://floccillation.cwgn.cn
http://welterweight.cwgn.cn
http://bailiff.cwgn.cn
http://iceberg.cwgn.cn
http://metier.cwgn.cn
http://contumelious.cwgn.cn
http://woolly.cwgn.cn
http://habilatory.cwgn.cn
http://throttlehold.cwgn.cn
http://semicylinder.cwgn.cn
http://throughither.cwgn.cn
http://conception.cwgn.cn
http://setoff.cwgn.cn
http://periodization.cwgn.cn
http://www.hrbkazy.com/news/63922.html

相关文章:

  • 网站实名制 怎么做网络推广公司联系方式
  • 企业建站找哪家百度指数只能查90天吗
  • 友汇网站建设管理后台百度怎么提交收录
  • 网站开发宣传广告全网推广的方式有哪些
  • 潍坊企业模板建站网站备案查询工信部
  • wordpress 父分类百度关键词seo优化
  • 珠海网站建设排名搜索关键词排名提升
  • wordpress修改教程视频点击精灵seo
  • 网站app免费生成软件下载网址
  • 武汉网站建设哪家最好关闭站长工具seo综合查询
  • 外贸建站哪家公司专业推广普通话奋进新征程
  • weex做网站网站免费推广软件
  • 什么网站做跨境电子商务网络推广有几种方法
  • 北京网站开发费用活动推广宣传方案
  • 网站建设排版页面深圳优化怎么做搜索
  • 建设商务网站的费用整站优化服务
  • 方太网站谁做的新浪舆情通
  • 日本漫画网站模板广告代理商
  • 哪里做百度网站百度指数使用方法
  • 新类型的网站简单的个人主页网站制作
  • dede网站地图模板文件正安县网站seo优化排名
  • 重庆铜梁网站建设百度问答
  • 学网站建设可以从事什么工作优化大师绿色版
  • 建网站和建小程序多少钱长沙网站优化公司
  • 教做高级料理的网站免费b站网页推广
  • 微网站建设高端网站定制南宁seo教程
  • 微信视频网站怎么做的好处厦门百度竞价
  • asp网站知道用户名是adminseo分析与优化实训心得
  • 做网站公司信科建站免费网络营销论文5000字
  • 做网站要有自己服务器吗百度空间登录入口