Code: Select all
TimedBox = class('TimedBox')
TimedBox:subclass('MessageBox')
Code: Select all
TimedBox = class('TimedBox', MessageBox)
-- or --
TimedBox = MessageBox:subclass('TimedBox')
- TimedBox = class('TimedBox') Creates a new class (a subclass of Object)
- TimedBox:subclass('MessageBox') is creating a subclass of TimedBox, called MessageBox. It is also returning it, but no variable receives it.
Code: Select all
function MessageBox:initialize()
end
All constructors should be either implicit (not appear at all, so they get a default implementation) or, if they are explicit, make a call to super.initialize(self)
Code: Select all
function MessageBox:initialize()
super.initialize(self) -- always, always do this.
end
Code: Select all
MessageBox = class('MessageBox')
MessageBox.DEFAULT_TEXT = "Default"
function MessageBox:initialize(text)
super.initialize(self) -- always, always do this.
self.text = text or self.class.DEFAULT_TEXT -- I prefer this, but you can (for now) do self.DEFAULT_TEXT instead of self.class.DEFAULT_TEXT
end
TimedBox = class('TimedBox', MessageBox) -- subclass
function TimedBox:initialize(text)
super.initialize(self, text)
end
StayBox = class('StayBox', MessageBox) -- subclass
StayBox.DEFAULT_TEXT = "StayBox" -- I changed the default text for StayBox here
function StayBox:initialize(text)
super.initialize(self, text)
end
ChoiceBox = class('ChoiceBox', MessageBox) -- subclass
function ChoiceBox:initialize(text)
super.initialize(self, text)
end
Code: Select all
function love.load()
msg = MessageBox:new()
print(msg.text)
timed = TimedBox:new("Parameter")
print(timed.text)
stay = StayBox:new()
print(stay.text)
end
Code: Select all
Default
Parameter
StayBox