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

【Lua】题目小练2

1.local t = {a = 1}

local mt = {

__index = function(table, key)

return key .. "_default"

end

}

setmetatable(t, mt)

print(t.a)

print(t.b)

1

b_default

2.观察并说明以下代码会不会报错,如果不会,最后输出什么:

MyClass = {}

MyClass.__index = MyClass

function MyClass:add(x)

return self.value + x

end

local obj = setmetatable({value = 10}, MyClass)

print(obj:add(5))

15

3.Parent = {}

Parent.__index = Parent

function Parent:new()

local self = setmetatable({}, Parent)

self.hp = 100

return self

end

function Parent:getHP()

        return self.hp

end

Child = {}

setmetatable(Child, {__index = Parent})

local c = Child:new()

print(c:getHP())

这个例子中,Child:new() 是如何调用的?为什么能工作?输出结果是多少?

在调用Child:new()方法的时候,由于Child本身是一个空的table,在此会调用元表的new()方法,即Person的new()方法,因此c被赋予了一个附带有Parent元表的空table,在执行c:getHP()的时候,由于自身是一个空table,因此会去访问元表的getHP方法,即Person:getHP()方法,最后输出100

4.

local t = {[1] = "a", [3] = "c", x = 7, [2] = "b"}

for i, v in ipairs(t) do

        print("ipairs", i, v)

end

for k, v in pairs(t) do

        print("pairs", k, v)

end

分别说明 ipairspairs 的输出行为(可以直接写出输出顺序)

ipairs()的输出为:ipairs1a,原因是ipairs的输出是先寻找了索引从1开始的,并且在遍历的中途不能断开

pairs()的输出为:

pairs1a

pairs2b

pairs3c

pairsx7

5.

下面的代码想实现一个类,但有 bug,试着找出并修改。

Enemy = {}

function Enemy:new(hp)

self.hp = hp

return self

end

function Enemy:hit(damage)

self.hp = self.hp - damage

end

local e = Enemy:new(100)

e:hit(10)

print(e.hp)

function Enemy:hit(damage)

         self.hp = self.hp - damage

         return self.hp

end

6.local t = setmetatable({}, {

__index = function(table, key)

return key .. "_default"

end

})

t[1] = "one"

print(t[1])

print(t[2])

one

2_default

7.

哪个写法可以正确实现 Lua 中的类继承?

A.

Child = {}

Child.__index = Parent

setmetatable(Child, Parent)

B.

Child = {}

setmetatable(Child, { __index = Parent })

C.

Child = setmetatable({}, Parent)

Child.__index = Child

D.

Child = Parent:new()

Child.__index = Child

A不可实现,中setmetatable(Child, Parent)并没有传入

B.可以实现

C.不可实现setmetatable({}, Parent)没有传入

D.可以实现,基于已有对象克隆方式通常配合Parent:new()返回对象设置好

8.

请写一个类 Player,它有属性 name 和 score,并且提供以下方法:


addScore(n):给分数加 n

printInfo():打印 name 和当前分数

local Player = {}

Player.__index = Person

function Player:new(name, score)

local obj = setmetatable({}, self)

        obj.nam = name or "UnKnow"

        obj.score = score or "UnKnow"

        return obj

end

function Player:addScore(n)

        self.score = self.score + n

end

http://www.dtcms.com/a/284949.html

相关文章:

  • LIN协议核心详解
  • c++之 KMP 讲解
  • Cocos游戏中UI跟随模型移动,例如人物头上的血条、昵称条等
  • C++中,不能声明为虚函数的函数类型
  • C++进阶-AVL树(平衡二叉查找树)(难度较高)
  • 2025 XYD Summer Camp 7.17 模考
  • Vue.js 响应式原理深度解析:从 Vue 2 的“缺陷”到 Vue 3 的“涅槃重生”
  • OpenVela之网络驱动适配指南
  • JxBrowser 7.43.5 版本发布啦!
  • ​​Sublime Text 2.0.2.2221 安装教程 - 详细步骤指南(附下载与配置)​
  • 深入解析:Chunked Prefill 与 FlashAttention/FlashInfer 如何协同工作
  • WSL2 离线安装流程
  • 如何让订货系统支持多角色?
  • 药品通用名、商品名、规格剂型查询API接口-中国药品批文数据库
  • 深度学习之优化方法
  • 页面登录阻止浏览器提醒是否保存密码
  • 算法讲解-移动零
  • 面试Redis篇-深入理解Redis缓存击穿
  • HTML 常用语义标签与常见搭配详解
  • 【Dv3Admin】菜单管理集成阿里巴巴自定义矢量图标库
  • uniapp云托管前端网页
  • 数据库、HTML
  • 中国各省市县坡度数据(Tif/Excel)
  • appium
  • bm-info-window百度地图去掉信息窗口影子
  • npm 和 npx 区别对比
  • 查看一个目录下的文件数量
  • 访问网页的全过程笔记
  • 移动安全工具-spd_dump
  • 聚类的可视化选择:PCA / t-SNE丨TomatoSCI分析日记