lua의class 간이 실현
1504 단어 language
function class(classname, super)
local superType=type(super)
local cls
if superType ~= "function" and superType ~= "table" then
superType=nil
super=nil
end
if super then
cls={}
setmetatable(cls, {__index=super})
cls.super=super
else
cls={ctor=function() end}
end
cls.__cname=classname
cls.__index=cls
function cls.new(...)
local instance=setmetatable({}, cls)
instance.class=cls
instance:ctor(...)
return instance
end
return cls
end
-- Shape
local Shape = class("Shape")
-- ctor() , Shape.new() Shape
function Shape:ctor(shapeName)
self.shapeName = shapeName
print(string.format("Shape:ctor(%s)", self.shapeName))
end
-- Shape draw()
function Shape:draw()
print(string.format("draw %s", self.shapeName))
end
-- Circle Shape
local Circle = class("Circle", Shape)
function Circle:ctor()
-- ctor() ,
-- .super
Circle.super:ctor("circle")
self.radius = 100
end
function Circle:setRadius(radius)
self.radius = radius
end
--
function Circle:draw()
print(string.format("draw %s, radius = %0.2f", self.shapeName, self.radius))
end
local circle1=Circle.new()
circle1:setRadius(125)
circle1:draw()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Azure Bicep 소개Bicep은 범용 프로그래밍 언어가 아니라 Azure Resource Manager (ARM) 템플릿에 대한 투명한 추상화입니다. ARM 템플릿은 Azure 인프라 및 구성을 정의하는 선언적 방법을 제공하는 JSON...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.