From eb3c3916efd9f6f873ecb9700b6f3caf2f04f650 Mon Sep 17 00:00:00 2001 From: lordlogo2002 Date: Thu, 1 Jan 2026 17:20:23 +0100 Subject: [PATCH] Refactor class structure: redefine Animal and Dog classes for improved inheritance and method definitions --- main.lua | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/main.lua b/main.lua index f27293f..1380fa9 100644 --- a/main.lua +++ b/main.lua @@ -1,16 +1,19 @@ oop = require("oop/oop") -local function MyClass(cls) - cls.setattr(cls, "name") - - function cls.__init(this, name) - this.name = name - end - - function cls.test(this, name) - print("hi " .. this.name .. " from " .. name) +function Animal(cls) + function cls.speak(this) + print("animal") end end -local obj = oop.new(MyClass, "Carl") -obj.test("tom") -- Output: hi Carl from tom +function Dog(cls) + cls.inherit(Animal) + + function cls.speak(this) + cls.super.speak(this) + print("woof") + end +end + +local d = new(Dog) +d.speak() -- animal, then woof