[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: teach your Grandma prototypical inheritance? was (Uncommon OOP in Lua: right or wrong?)
- From: Scott Vokes <vokes.s@...>
- Date: Sat, 25 Sep 2010 09:49:12 -0400
Whoops, forgot to actually show the inheritance lookup being used.
Let's try that again, now that I've had my coffee...
[====[
local function inherit(x, y) setmetatable(x, {__index=y}) end
mammal = {}
mammal.blood = function() return "warm" end
mammal.selfDefense = function() error("override me") end
cat = {}
inherit(cat, mammal)
cat.selfDefense = function() return "claws" end
dog = {}
inherit(dog, mammal)
dog.selfDefense = function() return "bite" end
-- doesn't override anything, just shows that missing fields are shared
aNormalCat = {}
inherit(aNormalCat, cat)
ninjaCat = {}
inherit(ninjaCat, cat)
ninjaCat.selfDefense = function() return "shuriken" end
print("cat -> ", cat.selfDefense())
print("dog -> ", dog.selfDefense())
print("aNormalCat -> ", aNormalCat.selfDefense()) --same as cat
print("ninjaCat -> ", ninjaCat.selfDefense())
]====]
Scott