root/lang/lua/misc/t-class.lua @ 19737

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
4require "Class"
5require "Test"
6
7Point = 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
30Point3D = 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
46p1 = Point.new(10, 20)
47eq(tostring(p1), "#<Class Point>")
48eq(p1.x, 10)
49eq(p1.y, 20)
50x, y = p1:point()
51eq(x, 10)
52eq(y, 20)
53
54-- metamethod (overload)
55p2 = Point.new(20, 5)
56pp = p1 + p2
57eq(pp.x, 30)
58eq(pp.y, 25)
59
60
61-- inherited class tests
62p3 = Point3D.new(10, 20, 30)
63eq(tostring(p3), "#<Class Point3D>")
64eq(p3.x, 10)
65eq(p3.y, 20)
66eq(p3.z, 30)
67x, y, z = p3:point()
68eq(x, 10)
69eq(y, 20)
70eq(z, 30)
71
72pp = p1 + p3
73eq(pp.x, 20)
74eq(pp.y, 40)
75
76pp = p3 + p1
77eq(pp.x, 20)
78eq(pp.y, 40)
79
80-- eq
81--[[
82p4 = Point.new(10, 10)
83p5 = Point3D.new(10, 10)
84eq(p4 == p5, true)
85]]
Note: See TracBrowser for help on using the browser.