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

【Lua】题目小练8

-- 题目 1:定义一个类 Person

-- 属性:name、age,其中 age 默认是 0,不能小于 0。

-- 方法:introduce(),输出 "My name is <name>, I am <age> years old."

-- 要求使用封装思想,age 设置必须通过一个方法 setAge(age),并在方法中处理合法性。

local Person = {}
Person.__index = Person--构造函数
function Person:new(name, age)local obj = {name = name or "UnKnow",age = 0,}setmetatable(obj,self)obj:setAge(age)return obj
endfunction Person:introduce()print("My name is "..self.name..", I am "..self.age.." years old.")
endfunction Person:setAge(age)if (age and type(age) == "number" and age > 0) thenself.age = ageelseprint("[Warning] Invalid age value, defaulting to 0.")end
end

-- 题目 2:类中添加私有方法(模拟私有函数)

-- 在 Person 类中,添加一个私有函数 calculateBirthYear(currentYear),根据年龄推算出生年份。

-- 在 introduce() 中调用它,输出内容增加一句:"I was born in <year>."

-- 提示:Lua 本身没有私有函数机制,但可以通过 local 函数模拟。

local function calculateBirthYear(self, currentYear)return currentYear - self.age
endfunction Person:introduce()print("My name is "..self.name..", I am "..self.age.." years old.")print("I was born in "..calculateBirthYear(self, 2025)..".")
endlocal o = Person:new("Keixo",12)
o:introduce()

-- 题目 3:定义子类 Student 继承 Person

-- 除了继承的属性和方法外,还新增属性 school,方法 introduce() 要重写,输出:

-- My name is <name>, I am <age> years old.

-- I study at <school>.

-- 同时添加方法 study(subject),输出:"<name> is studying <subject>."

local Student = {}
Student.__index = Student
setmetatable(Student, {__index = Person})function Student:new(name, age, school)local obj = Person.new(self, name, age)-- setmetatable(obj, self)obj.school = school or "UnKnow School"return obj
endfunction Student:introduce()print("My name is "..self.name..", I am "..self.age.." years old.")print("I study at "..self.school..".")
endfunction Student:study(subject)print(self.name.." is studying "..subject..".")
endlocal u = Student:new("Z",-2, "nihao")
u:introduce()
u:study("Math")

-- 题目 4:定义子类 Teacher 继承 Person

-- 属性:subject,

-- 方法:teach() 输出:"<name> is teaching <subject>."

-- 并重写 introduce() 输出:

-- My name is <name>, I am <age> years old.

-- I teach <subject>.

local Teacher = {}
Teacher.__index = Teacher
setmetatable(Teacher, {__index = Person})function Teacher:new(name, age, subject)local obj = Person.new(self,name, age)obj.subject = subject-- setmetatable(obj, self)return obj
endfunction Teacher:teach()print(self.name.."is teaching "..self.subject..".")print("I teach "..self.subject..".")
endlocal y = Teacher:new("y", 20, "Math")
y:introduce()
y:teach()

-- 题目 5:实现一个多态接口 introduceAll

-- 定义一个函数 introduceAll(personList),参数是一个表,内部调用每个人的 introduce() 方法。

-- 传入 Person、Student、Teacher 等不同对象,验证其输出是否符合多态行为。

local function introduceAll(personList)for i, person in ipairs(personList) doperson:introduce()end
endlocal people = {Person:new("p",1),Student:new("s",2),Teacher:new("t",3)
}introduceAll(people)

-- 题目 6:添加“静态方法”支持

-- 给 Person 添加一个类方法(静态方法)isAdult(age),返回是否大于等于 18 岁。

-- 提示:通过 Person.isAdult = function(age) ... end 实现。

Person.isAdult = function(age)return (type(age) == "number" and age >= 18) and true or false
endprint(o.isAdult(9))

-- 题目 7:模拟构造函数重载

-- 修改 Person:new(),支持两种构造方式:

-- Person:new(name, age)

-- Person:new({name = "xx", age = xx})

-- 要求两种方式都能成功创建对象。

function Person:new(nameOrTable, age)local obj = {}if type(nameOrTable) == "table" thenfor i,v in pairs(nameOrTable) doobj[i] = vendelseif type(nameOrTable) == "string" thenobj.name = nameOrTableobj.age = ageendsetmetatable(obj,self)return obj
endlocal p = Person:new("p", 12)
local k = Person:new({name = "k", age = 12})p:introduce()
k:introduce()
http://www.dtcms.com/a/315690.html

相关文章:

  • TrackVLA——开放世界下的四足具身视觉跟踪EVT(智能跟随):集目标识别与轨迹规划为一体的VLA,不怕高动态与遮挡
  • JavaWeb02——基础标签及样式(黑马视频笔记)
  • 扩展欧拉定理以及练习题
  • 嵌入式 - 数据结构:循环链表和内核链表
  • 【Unity笔记】Unity TextMeshPro 字体显示为方块的终极解决方案(含中文、特殊字符支持)
  • 如何查看PCI卡的VID,DID,SVID,SSID编号
  • Google AI 发布 MLE-STAR:一款能够自动执行各种 AI 任务的先进机器学习工程代理
  • cf.训练
  • Prometheus 监控平台部署 (云原生环境)
  • Docker Compose管理新范式:可视化控制台结合cpolar提升容器编排效率?
  • 从零开始学网页开发:HTML、CSS和JavaScript的基础知识
  • C++ 多线程(三)
  • 嵌入式学习的第四十三天-ds18b20 数字温度传感器
  • 如何在nuxtjs项目中使用vuex?
  • duxapp中主题系统是如何实现动态切换的
  • Redis 基础(一)
  • 数字图像处理(冈萨雷斯)第三版:第四章——频率域滤波(学前了解知识)——主要内容和重点
  • 【运维基础】Linux 系统启动原理
  • 增量:增量处理
  • 游戏行业DDoS攻防实战指南
  • ApplicationContext的实现类有哪些?
  • 「PromptPilot 大模型智能提示词平台」—— PromptPilot × 豆包大模型 1.6:客户投诉邮件高效回复智能提示词解决方案
  • 芯祥科技:工业/车规级BMS芯片厂商 规格选型对比
  • Python import 详解
  • linux_https,udp,tcp协议(更新中)
  • C++ ---》string类的模拟实现
  • CRT调试堆检测:从原理到实战的资源泄漏排查指南
  • HBM Basic(VCU128)
  • nflsoi 7.29 题解
  • Python-深度学习--2信息熵,条件熵(ID3决策树),KL散度