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

【Lua】题目小练7

Lua实现类、以及类的继承:

Point = {}
Point.__index = Pointfunction Point:new(x1, y1)local obj = {x = x1 or 0, y = y1 or 0}setmetatable(obj,self)return obj
endfunction Point:describe()print(string.format("Point at (%d, %d)", self.x, self.y))
end-- 定义子类Distance,继承Point
Distance = setmetatable({},{__index = Point})
Distance.__index = Distancefunction Distance:new(x,y)local obj = Point.new(self, x, y)setmetatable(obj, self)return obj
endfunction Distance:distanceTo(other)local dx = self.x - other.xlocal dy = self.y - other.yreturn math.sqrt(dx * dx + dy * dy)
endlocal p1 = Distance:new(0, 0)
local p2 = Distance:new(1, 1)p1:describe()
p2:describe()print(p1:distanceTo(p2))

-- 题目一:封装一个矩形类

-- 要求:

-- 类名:Rectangle

-- 属性:width, height(默认值都是 0)

-- 方法:

-- area():返回面积

-- perimeter():返回周长

local Rectangle = {}
Rectangle.__index = Rectanglefunction Rectangle:new(w, h)local obj = {w = (w and w > 0) and w or 0,h = (h and h > 0) and h or 0}setmetatable(obj, Rectangle)return obj
endfunction Rectangle:area()return self.w * self.h
endfunction Rectangle:perimeter()return 2 * (self.w + self.h)
endlocal obj = Rectangle:new(2,3)
print(obj:area())
print(obj:perimeter())

-- 题目二:实现一个继承关系

-- 要求:

-- 基类:Shape

-- 方法:describe(),输出 "This is a shape."

-- 子类:Circle

-- 属性:radius

-- 重写 describe(),输出 "This is a circle with radius R"

-- 添加方法:area()(使用 π≈3.14)

local Shape = {}
Shape.__index = Shapefunction Shape:describe()print("This is a shape.")
endlocal Circle = setmetatable({}, {__index = Shape})
Circle.__index = Circlefunction Circle:new(radius)local obj = {radius = (radius and radius > 0) and radius or 1 --默认值为1}setmetatable(obj, self)return obj
endfunction Circle:describe()print("This is a circle with radius " .. self.radius)
endfunction Circle:area()return 3.14 * self.radius ^ 2
endlocal obj = Circle:new(2)
obj:describe()
print(obj:area())

-- 题目三:多级继承

-- 要求:

-- Animal 基类,有属性 name,方法 speak() 打印 "Some generic animal sound"

-- Dog 继承 Animal,重写 speak() 输出 "Woof!"

-- Sheepdog 再继承 Dog,增加方法 guard() 输出 "Guarding the sheep!"

local Animal = {}
Animal.__index = Animalfunction Animal:new(name)local obj = {name = name or "UnKnow"}setmetatable(obj, self)return obj
endfunction Animal:speak()print("Some generic animal sound")
endlocal Dog = setmetatable({},{__index = Animal})
Dog.__index = Dogfunction Dog:new(name)return Animal.new(self,name)
endfunction Dog:speak()print("Woof")
endlocal Sheepdog = setmetatable({},{__index = Dog})
Sheepdog.__index = Sheepdogfunction Sheepdog:new(name)return Dog.new(self,name)
endfunction Sheepdog:guard()print("Guarding the sheep!")
endlocal obj = Sheepdog:new("ASheepdog")
obj:speak()
obj:guard()
http://www.dtcms.com/a/310555.html

相关文章:

  • Nestjs框架: 请求生命周期与应用生命周期
  • Vue模板语法详解:从基础到进阶的响应式绑定指南1
  • 工业数采引擎-DTU
  • CSS属性值计算规则:从声明到渲染的精确过程
  • 《C++》STL--list容器详解
  • 【读文献】Capacitor-drop AC-DC
  • 移除 Excel 文件(.xlsx)的工作表保护
  • Ubuntu 系统下使用 lsusb 命令识别 USB 设备及端口类型详解
  • 从“多、老、旧”到“4i焕新”:品牌官方商城(小程序/官网/APP···)的范式跃迁与增长再想象
  • 数据结构与算法——字典(前缀)树的实现
  • Rockchip RK3568J +FPGA边缘智能系统及储能网关
  • 以太网是什么网,什么网是以太网
  • spring cloud alibaba ——sidecar服务异构
  • Vite+React组件库提速方案
  • 区块链概述
  • 嵌入式 C 语言入门:函数封装与参数传递学习笔记 —— 从定义到内存机制
  • Syzkaller实战教程6:[重要]初始种子加载机制剖析第二集
  • 如何理解卷积,和自注意力机制的局限与优势(个人理解)
  • C++中typename基本用法
  • Nastool+cpolar:群晖NAS用户的全场景影音自由方案
  • 理解HTTP协议
  • 网络配置+初始服务器配置
  • Effective C++ 条款15:在资源管理类中提供对原始资源的访问
  • 在 Docker 中启动 Nginx 并挂载配置文件到宿主机目录
  • MyBatis知识点
  • 烽火HG680-KX-海思MV320芯片-2+8G-安卓9.0-强刷卡刷固件包
  • 电子电气架构 --- 加速48V技术应用的平衡之道
  • 机器学习sklearn:处理缺失值
  • 应用分层
  • 菜鸟教程Shell笔记 数组 运算符 echo命令