|
Revision 234, 1.2 kB
(checked in by cho45, 6 years ago)
|
|
lua,
lua/misc,
lua/misc/INI.lua,
lua/misc/t-sandbox.lua,
lua/misc/Test.lua,
lua/misc/t-ini.lua,
lua/misc/Class.lua,
lua/misc/t-class.lua,
lua/misc/List.lua,
lua/misc/t-list.lua,
lua/misc/SandBox.lua:
Lua の簡単なライブラリ
|
| Line | |
|---|
| 1 | #!/opt/local/bin/lua |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | require "Class" |
|---|
| 5 | require "Test" |
|---|
| 6 | |
|---|
| 7 | Point = Class { |
|---|
| 8 | __tostring = function (self) |
|---|
| 9 | return "#<Class Point>" |
|---|
| 10 | end, |
|---|
| 11 | |
|---|
| 12 | initialize = function (self, x, y) |
|---|
| 13 | self.x = x |
|---|
| 14 | self.y = y |
|---|
| 15 | end, |
|---|
| 16 | |
|---|
| 17 | point = function (self) |
|---|
| 18 | return self.x, self.y |
|---|
| 19 | end, |
|---|
| 20 | |
|---|
| 21 | __add = function (a, b) |
|---|
| 22 | return Point.new(a.x + b.x, a.y + b.y) |
|---|
| 23 | end, |
|---|
| 24 | |
|---|
| 25 | __eq = function (a, b) |
|---|
| 26 | return (a.x == b.x) |
|---|
| 27 | end, |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | Point3D = Class { super = Point, |
|---|
| 31 | __tostring = function (self) |
|---|
| 32 | return "#<Class Point3D>" |
|---|
| 33 | end, |
|---|
| 34 | |
|---|
| 35 | initialize = function (self, x, y, z) |
|---|
| 36 | self.super.initialize(self, x, y) |
|---|
| 37 | self.z = z |
|---|
| 38 | end, |
|---|
| 39 | |
|---|
| 40 | point = function (self) |
|---|
| 41 | return self.x, self.y, self.z |
|---|
| 42 | end, |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | -- basic class tests |
|---|
| 46 | p1 = Point.new(10, 20) |
|---|
| 47 | eq(tostring(p1), "#<Class Point>") |
|---|
| 48 | eq(p1.x, 10) |
|---|
| 49 | eq(p1.y, 20) |
|---|
| 50 | x, y = p1:point() |
|---|
| 51 | eq(x, 10) |
|---|
| 52 | eq(y, 20) |
|---|
| 53 | |
|---|
| 54 | -- metamethod (overload) |
|---|
| 55 | p2 = Point.new(20, 5) |
|---|
| 56 | pp = p1 + p2 |
|---|
| 57 | eq(pp.x, 30) |
|---|
| 58 | eq(pp.y, 25) |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | -- inherited class tests |
|---|
| 62 | p3 = Point3D.new(10, 20, 30) |
|---|
| 63 | eq(tostring(p3), "#<Class Point3D>") |
|---|
| 64 | eq(p3.x, 10) |
|---|
| 65 | eq(p3.y, 20) |
|---|
| 66 | eq(p3.z, 30) |
|---|
| 67 | x, y, z = p3:point() |
|---|
| 68 | eq(x, 10) |
|---|
| 69 | eq(y, 20) |
|---|
| 70 | eq(z, 30) |
|---|
| 71 | |
|---|
| 72 | pp = p1 + p3 |
|---|
| 73 | eq(pp.x, 20) |
|---|
| 74 | eq(pp.y, 40) |
|---|
| 75 | |
|---|
| 76 | pp = p3 + p1 |
|---|
| 77 | eq(pp.x, 20) |
|---|
| 78 | eq(pp.y, 40) |
|---|
| 79 | |
|---|
| 80 | -- eq |
|---|
| 81 | --[[ |
|---|
| 82 | p4 = Point.new(10, 10) |
|---|
| 83 | p5 = Point3D.new(10, 10) |
|---|
| 84 | eq(p4 == p5, true) |
|---|
| 85 | ]] |
|---|