Created
November 25, 2014 15:04
-
-
Save Pondidum/f5d440db2fc27c3d1aa8 to your computer and use it in GitHub Desktop.
Inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local class = { | |
| extend = function(self, this) | |
| --i could put this method on class itself, | |
| --but then i couldnt completely close over the self/base var | |
| local base = self | |
| this.super = function(child) | |
| local index = function(_, methodName) | |
| return function(_, ...) | |
| base[methodName](child, ...) | |
| end | |
| end | |
| return setmetatable({}, { __index = index }) | |
| end | |
| return setmetatable(this, { __index = self }) | |
| end, | |
| new = function(self, ...) | |
| local this = setmetatable({}, { __index = self }) | |
| this:ctor(...) | |
| return this | |
| end, | |
| ctor = function(self, ...) | |
| end | |
| } | |
| local control = class:extend({ | |
| ctor = function(self, name, parent) | |
| self.frame = { name = name} --CreateFrame("Frame", name, parent) | |
| end, | |
| }) | |
| local label = control:extend({ | |
| ctor = function(self, ...) | |
| self:super():ctor(...) | |
| self.frame.type = "label" | |
| end | |
| }) | |
| local i1 = label:new("a") | |
| local i2 = label:new("b") | |
| assert(i1.frame ~= i2.frame) | |
| print(i1.frame.name, i1.frame.type) | |
| print(i2.frame.name, i2.frame.type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment