Refactor class structure: redefine Animal and Dog classes for improved inheritance and method definitions

This commit is contained in:
Chipperfluff 2026-01-01 17:20:23 +01:00
parent 45a226cb7b
commit eb3c3916ef

View File

@ -1,16 +1,19 @@
oop = require("oop/oop") oop = require("oop/oop")
local function MyClass(cls) function Animal(cls)
cls.setattr(cls, "name") function cls.speak(this)
print("animal")
function cls.__init(this, name)
this.name = name
end
function cls.test(this, name)
print("hi " .. this.name .. " from " .. name)
end end
end end
local obj = oop.new(MyClass, "Carl") function Dog(cls)
obj.test("tom") -- Output: hi Carl from tom cls.inherit(Animal)
function cls.speak(this)
cls.super.speak(this)
print("woof")
end
end
local d = new(Dog)
d.speak() -- animal, then woof