Lua_オブジェクトへの継承


ベースクラスを作成するには、次の手順に従います.
Person.lua
Person = {name,age,score}
function Person:new()
	o= o or {};
	setmetatable(o,self)
	self.__index  = self
	self.name = "123";
	self.age = 0;
	self.score = 0;
	return o;
end

function Person:ShowInformation()
	local info = string.format("name:%s,age:%d,score:%.2f",self.name,self.age,self.score)
	print(info)
end

return Person

サブクラスlua
local Person = require("Person") 
Student = Person:new()
//       
function Student:new()
	
end
//    
function Student:new(name,age,score)
 	o = o or Person:new()
 	setmetatable(o,self)
 	--           
 	self.__index = Person
 	self.name = name
 	self.age = age;
 	self.score = score
 	return o
end

return Student

Main.lua
local p = "C:\\Users\\Administrator\\Desktop\\LuaCode\\"
package.path = string.format("%s?.lua;%s?\\init.lua;%s?\\.\\?.lua;%s",p,p,p, package.path)

local Person = require("Person")
p = Person:new()
p:ShowInformation();

local Student = require("Student")

s = Student:new("555",45,999)
s:ShowInformation()