Refactor OOP implementation: enhance Animal class methods and improve CLI functionality for animal actions
This commit is contained in:
parent
eb3c3916ef
commit
1f595e867a
86
main.lua
86
main.lua
@ -1,8 +1,16 @@
|
|||||||
oop = require("oop/oop")
|
oop = require("oop/oop")
|
||||||
|
|
||||||
function Animal(cls)
|
function Animal(cls)
|
||||||
|
function cls.__init(this, name)
|
||||||
|
this.name = name
|
||||||
|
end
|
||||||
|
|
||||||
function cls.speak(this)
|
function cls.speak(this)
|
||||||
print("animal")
|
print(this.name .. " makes a noise.")
|
||||||
|
end
|
||||||
|
|
||||||
|
function cls.info(this)
|
||||||
|
print("Animal: " .. this.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -11,9 +19,79 @@ function Dog(cls)
|
|||||||
|
|
||||||
function cls.speak(this)
|
function cls.speak(this)
|
||||||
cls.super.speak(this)
|
cls.super.speak(this)
|
||||||
print("woof")
|
print(this.name .. " barks!")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local d = new(Dog)
|
function Cat(cls)
|
||||||
d.speak() -- animal, then woof
|
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()
|
||||||
|
|||||||
14
oop/oop.lua
14
oop/oop.lua
@ -71,10 +71,18 @@ function oop.class(def, base)
|
|||||||
return oop.setattr(target, key, value)
|
return oop.setattr(target, key, value)
|
||||||
end
|
end
|
||||||
function cls.inherit(parent)
|
function cls.inherit(parent)
|
||||||
cls.__base = parent
|
local actual_parent = parent
|
||||||
cls.super = 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 {}
|
local mt = getmetatable(cls) or {}
|
||||||
mt.__index = parent
|
mt.__index = actual_parent
|
||||||
mt.__call = mt.__call or function(c, ...)
|
mt.__call = mt.__call or function(c, ...)
|
||||||
return oop.new(c, ...)
|
return oop.new(c, ...)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user