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")
|
oop = require("oop/oop")
|
||||||
|
|
||||||
local function MyClass(cls)
|
function Animal(cls)
|
||||||
cls.setattr(cls, "name")
|
function cls.speak(this)
|
||||||
|
print("animal")
|
||||||
function cls.__init(this, name)
|
|
||||||
this.name = name
|
|
||||||
end
|
|
||||||
|
|
||||||
function cls.test(this, name)
|
|
||||||
print("hi " .. this.name .. " from " .. name)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local obj = oop.new(MyClass, "Carl")
|
function Dog(cls)
|
||||||
obj.test("tom") -- Output: hi Carl from tom
|
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