File: test-class3.lua

package info (click to toggle)
lua-penlight 1.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,708 kB
  • sloc: makefile: 2
file content (29 lines) | stat: -rw-r--r-- 572 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- another way to define classes. Works particularly well
-- with Moonscript
local class = require('pl.class')
local A = class{
  _init = function(self, name)
    self.name = name
  end,
  greet = function(self)
    return "hello " .. self.name
  end,
  __tostring = function(self)
    return self.name
  end
}

local B = class{
  _base = A,
  
  greet = function(self)
    return "hola " .. self.name
  end
}

local a = A('john')
assert(a:greet()=="hello john")
assert(tostring(a) == "john")
local b = B('juan')
assert(b:greet()=="hola juan")
assert(tostring(b)=="juan")