oop = require("oop/oop") function Animal(cls) function cls.__init(this, name) this.name = name end function cls.speak(this) print(this.name .. " makes a noise.") end function cls.info(this) print("Animal: " .. this.name) end end function Dog(cls) cls.inherit(Animal) function cls.speak(this) cls.super.speak(this) print(this.name .. " barks!") end end 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()