- --基类
- Shape = {area=0}
- function Shape:new(o)
- o = o or{}
- setmetatable(o,self)
- self.__index=self
- return o
- end
- function Shape:getArea()
- self.area=0
- end
- function Shape:printArea()
- print("面积为"..self.area)
- end
- --派生类
- Square=Shape:new()
- function Square:new(o,side)
- o=o or Shape:new(o,side)
- setmetatable(o,self)
- self.__index=self
- self.side=side or 0
- return o
- end
- --重载
- function Shape:getArea()
- self.area=self.side*self.side
- end
- function Shape:getArea(side)
- self.area=side*side
- end
- ---------------调用
- mySquare= Square:new()
- mySquare:getArea(200)
- mySquare:printArea()
复制代码
前面脚本写错了..懒人精灵.还是完美支持lua的 |