--基类
shape={area=0}
function shape:new( o,side )
o=o or{}
setmetatable(o,self) --将自己设为o的元表,如果o不为空,则自己为o的父类
self.__index=self
side=side or 0
self.area=side*side
return o
end
//查找过程:
//从o表中查找,找不到元素,再到self表中找
function shape:printArea()
print("基类的面积为:",self.area)
end
myshape=shape:new(nil,10)
myshape:printArea()
--派生正方形
square=shape:new()
--派生类的方法new
function square:new(o,side )
o=o or shape:new(o,side)
setmetatable(o,self) --将自己设为父类的表的元表,相当于自己为父类表的扩展
self.__index=self
return o
end
--派生类的方法 printArea
function square:printArea( )
print("正方形的面积:",self.area)
end
//查找过程:
//从o表中查找,找不到元素,再到self表中找
mysquare=square:new(nil,20)
mysquare:printArea()