lua的类
lua的类有点像javascript,但是更简明灵活,table即对象,对象就是类。Metatables比起ruby里的MetaClass更加好用,缺点是实例化和继承的代码有点多,
不像ruby里的“<”和“<<”,继承链就是查找方法时的方法链。
1 Account={
2 test1=function(a) print("Account test1") end
3 }
4
5 Account.test2=function(a) print("Account test2") end
6
7 function Account.test3(a) print("Account test3") end
8
9
10 function Account:new (o) --类的实例化
11
12 o = o or {}
13
14 setmetatable(o, self)
15
16 self.__index = self
17
18 return o
19
20 end
21
22 function Account.print0(o,a)
23 print(a)
24 end
25
26 function Account:print1(a)
27 print(a)
28 end
29
30
31 --方法定义测试
32 Account.test1()
33 Account.test2()
34 Account.test3()
35
36 --类测试
37 acc=Account:new()
38 acc.test1()
39 acc.print0(acc,"dot print0")
40 acc:print0("not dot print0")
41 acc.print1(acc,"dot print1")
42 acc:print1("not dot print1")
43
44 acc.specialMethod=function(specialMethodTest)
45 print(specialMethodTest)
46 end
47
48 acc.specialMethod("smt test")
49
50 --继承测试
51 SpecialAccount = Account:new()
52 s = SpecialAccount:new{limit=1000.00}
53
54 --多重继承测试
55 Named = {}
56
57 function Named:getname ()
58
59 return self.name
60
61 end
62
63 function Named:setname (n)
64
65 self.name = n
66
67 end
68
69 local function search (k, plist)
70
71 for i=1, table.getn(plist) do
72
73 local v = plist[i][k]
74
75 if v then return v end
76
77 end
78
79 end
80
81 function createClass (...)
82
83 local c = {} -- new class
84
85 setmetatable(c, {__index = function (t, k)
86
87 return search(k, arg)
88
89 end})
90
91 c.__index = c
92
93 function c:new (o)
94
95 o = o or {}
96
97 setmetatable(o, c)
98
99 return o
100
101 end
102
103 return c
104
105 end
106 NamedAccount = createClass(Account, Named)
107
108 account = NamedAccount:new{name = "Paul"}
109
110 print(account:getname())
2 test1=function(a) print("Account test1") end
3 }
4
5 Account.test2=function(a) print("Account test2") end
6
7 function Account.test3(a) print("Account test3") end
8
9
10 function Account:new (o) --类的实例化
11
12 o = o or {}
13
14 setmetatable(o, self)
15
16 self.__index = self
17
18 return o
19
20 end
21
22 function Account.print0(o,a)
23 print(a)
24 end
25
26 function Account:print1(a)
27 print(a)
28 end
29
30
31 --方法定义测试
32 Account.test1()
33 Account.test2()
34 Account.test3()
35
36 --类测试
37 acc=Account:new()
38 acc.test1()
39 acc.print0(acc,"dot print0")
40 acc:print0("not dot print0")
41 acc.print1(acc,"dot print1")
42 acc:print1("not dot print1")
43
44 acc.specialMethod=function(specialMethodTest)
45 print(specialMethodTest)
46 end
47
48 acc.specialMethod("smt test")
49
50 --继承测试
51 SpecialAccount = Account:new()
52 s = SpecialAccount:new{limit=1000.00}
53
54 --多重继承测试
55 Named = {}
56
57 function Named:getname ()
58
59 return self.name
60
61 end
62
63 function Named:setname (n)
64
65 self.name = n
66
67 end
68
69 local function search (k, plist)
70
71 for i=1, table.getn(plist) do
72
73 local v = plist[i][k]
74
75 if v then return v end
76
77 end
78
79 end
80
81 function createClass (...)
82
83 local c = {} -- new class
84
85 setmetatable(c, {__index = function (t, k)
86
87 return search(k, arg)
88
89 end})
90
91 c.__index = c
92
93 function c:new (o)
94
95 o = o or {}
96
97 setmetatable(o, c)
98
99 return o
100
101 end
102
103 return c
104
105 end
106 NamedAccount = createClass(Account, Named)
107
108 account = NamedAccount:new{name = "Paul"}
109
110 print(account:getname())
![](http://www.cnblogs.com/Emoticons/baimantou/202015934.gif)