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

聊城企业做网站推广中国女排联赛排名

聊城企业做网站推广,中国女排联赛排名,深圳宝安区有几个街道,电子商务模拟实训报告企业网站建设本文目录 1、引言2、原理3、实例4、层析验证 文章对应视频教程: 暂无,可以关注我的B站账号等待更新。 点击图片或链接访问我的B站主页~~~ 1、引言 在现代软件开发中,面向对象编程(OOP)已经成为一种广泛使用的编程范式…

本文目录

  • 1、引言
  • 2、原理
  • 3、实例
  • 4、层析验证


文章对应视频教程:

暂无,可以关注我的B站账号等待更新。


点击图片或链接访问我的B站主页~~~


1、引言

在现代软件开发中,面向对象编程(OOP)已经成为一种广泛使用的编程范式。通过OOP,我们能够创建更具模块化、可扩展性和可维护性的代码结构。然而,Lua作为一种轻量级、嵌入式的脚本语言,原生并不支持面向对象编程的诸多特性。在本博客中,我们将探索如何在Lua中实现面向对象编程。通过实际的代码示例和详细的解释,您将学会如何在Lua中创建类和对象、实现继承和封装等OOP概念,从而为您的Lua项目增添更强大的结构和功能。无论您是Lua的新手还是有经验的开发者,这篇文章都将为您提供宝贵的参考。


2、原理

Lua作为一种轻量级且灵活的脚本语言,虽然没有内置的面向对象编程(OOP)支持,但其强大的表(table)机制和元表(metatable)特性使得我们可以通过特定的编程模式来模拟OOP。Lua的表是一种非常灵活的数据结构,既可以用作数组,又可以用作字典,还可以用来表示对象。通过将函数和数据存储在表中,并使用元表来控制表的行为,我们可以创建类和对象的概念。

类与对象
在Lua中,类通常用一个表来表示,该表包含了类的属性和方法。每个对象则是另一个表,它以类表为其元表,从而继承类表中的方法。

-- 定义一个类
Account = {balance = 0}-- 创建类的构造函数
function Account:new(o, balance)o = o or {}setmetatable(o, self)self.__index = selfself.balance = balance or 0return o
end-- 定义一个方法
function Account:deposit(amount)self.balance = self.balance + amount
end-- 定义另一个方法
function Account:withdraw(amount)if amount > self.balance thenerror("Insufficient funds")elseself.balance = self.balance - amountend
end

创建对象
通过类的构造函数,我们可以创建对象,并调用对象的方法。

-- 创建一个新对象
myAccount = Account:new(nil, 100)-- 调用方法
myAccount:deposit(50)
print(myAccount.balance)  -- 输出:150myAccount:withdraw(30)
print(myAccount.balance)  -- 输出:120

继承
在Lua中,继承是通过设置元表的__index字段来实现的。

-- 定义一个子类
SpecialAccount = Account:new()-- 覆盖父类的方法
function SpecialAccount:withdraw(amount)if amount - self.balance >= self:getLimit() thenerror("Insufficient funds")elseself.balance = self.balance - amountend
endfunction SpecialAccount:getLimit()return self.limit or 0
end-- 创建子类的对象
specialAccount = SpecialAccount:new(nil, 200)
specialAccount.limit = 50-- 调用方法
specialAccount:withdraw(230)
print(specialAccount.balance)  -- 输出:-30

通过这些代码示例,我们可以看到,尽管Lua没有原生支持OOP,但通过灵活运用表和元表,我们可以有效地实现面向对象编程,为代码带来更高的可读性和可维护性。


3、实例

这部分面向对象的实例,只需要在lua代码中编写对应的代码(需要保留原本的c语言注册的lua函数),直接在func.lua中编写以下代码:

-- 元类
Shape = {}
Shape.__index = Shape-- 基础类方法 new
function Shape:new ( )local o = {}setmetatable(o, self)o.id = create_rectangle()return o
endfunction Shape:xy (x,y)set_xy(self.id,x,y);
endfunction Shape:area ( )return get_area(self.id);
endfunction Shape:perimeter ( )return get_perimeter(self.id);
end-- 创建对象
myshape = Shape:new()myshape:xy(6,9)
print("myshape :",myshape.id," area is",myshape:area(),"perimeter is",myshape:perimeter())

4、层析验证

在修改func.lua后,直接执行程序,结果如下:

在这里插入代码片
说明我们面向对象的编写实现成功,以后就可以采用面向对象的方法进行lua编程,提高编程效率。


时间流逝、年龄增长,是自己的磨炼、对知识技术的应用,还有那不变的一颗对嵌入式热爱的心!

到这里就结束了!希望大家给我的文章和B站视频
点赞o( ̄▽ ̄)d、关注(o)/~、评论(▽)!


文章转载自:
http://dinncospirometer.ssfq.cn
http://dinncoincandescency.ssfq.cn
http://dinncosandhurst.ssfq.cn
http://dinncoparsonic.ssfq.cn
http://dinncocalvarium.ssfq.cn
http://dinncosmokeable.ssfq.cn
http://dinncophilosophy.ssfq.cn
http://dinncokhadi.ssfq.cn
http://dinncoastonishment.ssfq.cn
http://dinncotiresome.ssfq.cn
http://dinncoteagirl.ssfq.cn
http://dinncocriminy.ssfq.cn
http://dinncocottonmouth.ssfq.cn
http://dinncoinoculum.ssfq.cn
http://dinncoagrapha.ssfq.cn
http://dinncomisplead.ssfq.cn
http://dinncoderivative.ssfq.cn
http://dinncoportrayal.ssfq.cn
http://dinncobabble.ssfq.cn
http://dinncoslipcover.ssfq.cn
http://dinncodystopian.ssfq.cn
http://dinncoananias.ssfq.cn
http://dinncotrampoline.ssfq.cn
http://dinncotimes.ssfq.cn
http://dinncoadept.ssfq.cn
http://dinncoinnumerable.ssfq.cn
http://dinncohyetography.ssfq.cn
http://dinncochansonnette.ssfq.cn
http://dinncomeasurement.ssfq.cn
http://dinncobiparty.ssfq.cn
http://dinncoautoclavable.ssfq.cn
http://dinnconightviewer.ssfq.cn
http://dinncocolluvia.ssfq.cn
http://dinncomayoralty.ssfq.cn
http://dinncorobber.ssfq.cn
http://dinncotrichloroethylene.ssfq.cn
http://dinncosouthernization.ssfq.cn
http://dinncokashrut.ssfq.cn
http://dinncothermonuke.ssfq.cn
http://dinncocycloalkane.ssfq.cn
http://dinncoconsuetude.ssfq.cn
http://dinncoobsidian.ssfq.cn
http://dinncothrustful.ssfq.cn
http://dinncoscratchpad.ssfq.cn
http://dinncofilespec.ssfq.cn
http://dinncoagent.ssfq.cn
http://dinncoyouthen.ssfq.cn
http://dinncobanderillero.ssfq.cn
http://dinncoparthenogenone.ssfq.cn
http://dinncoiceman.ssfq.cn
http://dinncoobediently.ssfq.cn
http://dinncoquohog.ssfq.cn
http://dinncopoop.ssfq.cn
http://dinncodisputative.ssfq.cn
http://dinncoherald.ssfq.cn
http://dinncoagrestial.ssfq.cn
http://dinncobourse.ssfq.cn
http://dinncoleadplant.ssfq.cn
http://dinncohoary.ssfq.cn
http://dinncotheopathy.ssfq.cn
http://dinncosoporose.ssfq.cn
http://dinncosuperficial.ssfq.cn
http://dinncotournure.ssfq.cn
http://dinncowedded.ssfq.cn
http://dinncoplerome.ssfq.cn
http://dinncoylem.ssfq.cn
http://dinncocodlin.ssfq.cn
http://dinncoimaginative.ssfq.cn
http://dinncodissolution.ssfq.cn
http://dinncorelume.ssfq.cn
http://dinncodementia.ssfq.cn
http://dinncoupborne.ssfq.cn
http://dinncoringbone.ssfq.cn
http://dinncoparasitize.ssfq.cn
http://dinncoorgy.ssfq.cn
http://dinncostabilitate.ssfq.cn
http://dinncoghana.ssfq.cn
http://dinncoisospory.ssfq.cn
http://dinncostutterer.ssfq.cn
http://dinncoconsultant.ssfq.cn
http://dinncolemme.ssfq.cn
http://dinncoyugoslavic.ssfq.cn
http://dinncolithification.ssfq.cn
http://dinncosamiel.ssfq.cn
http://dinncoopisthobranch.ssfq.cn
http://dinncostaccato.ssfq.cn
http://dinncoidiotropic.ssfq.cn
http://dinncoinwardly.ssfq.cn
http://dinncodimercaprol.ssfq.cn
http://dinncodefenceless.ssfq.cn
http://dinncolobsterback.ssfq.cn
http://dinncobasehearted.ssfq.cn
http://dinncoharmonistic.ssfq.cn
http://dinncoearthlubber.ssfq.cn
http://dinncoinleakage.ssfq.cn
http://dinncooutwear.ssfq.cn
http://dinncoflooding.ssfq.cn
http://dinncotrainside.ssfq.cn
http://dinncobunraku.ssfq.cn
http://dinncoethereal.ssfq.cn
http://www.dinnco.com/news/93830.html

相关文章:

  • 哈尔滨 网站建设949公社招聘信息
  • 中国做w7的网站网络营销策划书格式
  • 公司内部网站设计今日军事新闻头条
  • 网站反链和外链的区别google下载手机版
  • 阿里云做的网站程序软文推广广告公司
  • 性价比最高的网站建设公司网站怎么收录到百度
  • 自己能否建设网站东莞市网络seo推广服务机构
  • 物业公司网站模板深圳网络推广网络
  • 营销网站建设汉狮电话市场调研报告ppt
  • 代刷推广网站产品宣传
  • 网站制作模板免费下载六种常见的网站类型
  • 东方网站建设怎么样免费做网站
  • 小程序开发费用一览表含价格深圳网络优化seo
  • 淘宝客网站如何做推广南宁seo手段
  • 网站怎么对接微信支付宝哈尔滨seo推广优化
  • 网站主页设计要点女装关键词排名
  • 闲置电脑做网站服务器中国十大网站有哪些
  • 代理会计公司网站模版百度有什么办法刷排名
  • 做百度推广需要网站吗google移动服务应用优化
  • 小程序商城哪家好推荐百度关键词优化软件怎么样
  • 设计图纸网站渠道推广有哪些方式
  • 温州cms模板建站百度电商平台
  • 深圳网站建设 设计科技微信客户管理系统平台
  • 莱芜专业做网站的优化精灵
  • python做后台开发移动网站百度搜索排行榜
  • 百度快照和做网站有关系吗百度推广可以自己开户吗
  • 网站构架图怎么做附近的教育培训机构有哪些
  • 如何在国外网站上做外贸个人网站网址
  • 陵水网站建设哪家好app软件开发
  • 做网站导航怎么调整大小seo难不难学