Refactor class structure: redefine Animal and Dog classes for improved inheritance and method definitions
This commit is contained in:
parent
45a226cb7b
commit
eb3c3916ef
25
main.lua
25
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user