From 1f595e867ae79d8e1ca77dbebd8635de81a7f87b Mon Sep 17 00:00:00 2001 From: lordlogo2002 Date: Thu, 1 Jan 2026 17:27:50 +0100 Subject: [PATCH] Refactor OOP implementation: enhance Animal class methods and improve CLI functionality for animal actions --- main.lua | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++--- oop/oop.lua | 14 +++++++-- 2 files changed, 93 insertions(+), 7 deletions(-) diff --git a/main.lua b/main.lua index 1380fa9..192eb6f 100644 --- a/main.lua +++ b/main.lua @@ -1,8 +1,16 @@ oop = require("oop/oop") function Animal(cls) + function cls.__init(this, name) + this.name = name + end + function cls.speak(this) - print("animal") + print(this.name .. " makes a noise.") + end + + function cls.info(this) + print("Animal: " .. this.name) end end @@ -11,9 +19,79 @@ function Dog(cls) function cls.speak(this) cls.super.speak(this) - print("woof") + print(this.name .. " barks!") end end -local d = new(Dog) -d.speak() -- animal, then woof +function Cat(cls) + cls.inherit(Animal) + + function cls.speak(this) + print(this.name .. " meows.") + end +end + +function Bird(cls) + cls.inherit(Animal) + + function cls.speak(this) + print(this.name .. " chirps.") + end +end + +function CLI(cls) + cls.setattr(cls, "classes", { + Dog = Dog, + Cat = Cat, + Bird = Bird + }) + + function cls.__init(this) + this.classes = cls.classes + end + + function cls.prompt(this, label) + io.write(label) + return io.read() + end + + function cls.handleActions(this, obj) + print("Type an action: speak, info (or 'back' to choose another animal).") + while true do + local action = this.prompt("action> ") + if not action or action == "back" then + return + end + local method = obj[action] + if type(method) == "function" then + method() + else + print("Unknown action.") + end + end + end + + function cls.handleAnimal(this, choice) + local cls_ref = this.classes[choice] + if not cls_ref then + print("Unknown animal.") + return + end + local obj = oop.new(cls_ref, choice) + this.handleActions(obj) + end + + function cls.run(this) + print("Type a class name: Dog, Cat, Bird (or 'quit' to exit).") + while true do + local choice = this.prompt("animal> ") + if not choice or choice == "quit" then + break + end + this.handleAnimal(choice) + end + end +end + +local cli = oop.new(CLI) +cli.run() diff --git a/oop/oop.lua b/oop/oop.lua index 2d2c823..0fd42d7 100644 --- a/oop/oop.lua +++ b/oop/oop.lua @@ -71,10 +71,18 @@ function oop.class(def, base) return oop.setattr(target, key, value) end function cls.inherit(parent) - cls.__base = parent - cls.super = parent + local actual_parent = parent + if type(parent) == "function" then + actual_parent = function_def_cache[parent] + if not actual_parent then + actual_parent = oop.class(parent) + function_def_cache[parent] = actual_parent + end + end + cls.__base = actual_parent + cls.super = actual_parent local mt = getmetatable(cls) or {} - mt.__index = parent + mt.__index = actual_parent mt.__call = mt.__call or function(c, ...) return oop.new(c, ...) end