. 1 - Examples of classes, the class rectangle, provided with a printing area setting method and a method of aspect 2 - Thinking lua implementation class, object definitions out in some lua can access their own members, member functions to access the actual the method is implemented by yuan __index table, the specific point is to visit their member functions found no function, and then determine whether there are tables and metadata __index element method (of course there are, but there must be), then becomes to the access element is a member of the class table, naturally access member functions . 3 Cfx = m_chang = { 0 , m_kuan = 0 , m_area = 0 } . 4 Cfx .__ index = Cfx . 5 function Cfx: new new (Chang, Kuan) . 6 local Self = {} . 7 setmetatable (Self, Cfx) . 8 self.m_chang = Chang . 9 self.m_kuan = Kuan 10 self.m_area = chang * kuan 11 return self 12 end 13 14 function Cfx:printArea() 15 print("长方形的面积为:" .. self.m_area) 16 end 17 18 function Cfx:setC(chang, kuan) 19 if chang then 20 self.m_chang = chang 21 end 22 if kuan then 23 self.m_kuan = kuan 24 end 25 self.m_area = self.m_chang * self.m_kuan 26 end 27 28 R1 = CFX: new ( 10 , 20 ) 29 R 1: print () 30 R 2 = CFX: new ( 30 , 40 ) 31 R 2: print () 32 r1 : gill-nets (_ 50 ) 33 R 2: gill-nets ( 50 ) 34 Print ( " -------------------------------- -------------------------------- " ) 35 R 1: print () 36 R 2: print () 37 38 39 40 - the Lua inheritance, class inherits from the above rectangle, along with its members (circumferential length) and a method of printing perimeter 41 is - Construction Similarly Inheritance also with the above mentioned class, C ++ can be seen as the first configuration of a parent class, then their configuration, it will be appreciated that there is constructed a two class 42 is Cfx_w m_zhouchang = = { 0 } 43 is Cfx_w .__ index = Cfx_w 44 is setmetatable (Cfx_w, Cfx) 45 46 is function Cfx_w: new new (C, K) 47 local Self = {} 48 Self = Cfx: new new (C, K) 49 setmetatable (Self, Cfx_w) 50 self.m_zhouchang = 2 * (C + K) 51 is return Self 52 is End 53 is 54 is 55 function Cfx_w: Printz () 56 is self.m_zhouchang = 2 * (+ self.m_chang self.m_kuan) 57 is Print ( " rectangular perimeter: " .. self.m_zhouchang) 58 End 59 60 function Cfx_w: the printArea () 61 is Print ( " override the superclass method, a rectangular area is: " .. self.m_area) 62 is End 63 is R3 = Cfx_w: new new ( 10 , . 5 ) 64 R3: the printArea () 65 R3: Printz () 66 Print ( "************************************************** ************** " ) 67 R 3: gill-nets ( 2 , 2 ) 68 r3: print () 69 r3: Printz ()
Objects thinking lua implementation class definition out in lua can access some of their own members, access to member functions are actually implemented by yuan __index method table, the specific point is to visit their member functions found no function, then determine whether the yuan tables and __index element method (of course there are, but there must be), then it becomes a member of the access metadata table is kind of, naturally, a visit to a member function.