--hero class local Object = require 'Oop.Object' --local Class = require 'Oop.Class' HeroModel=Object:subclass('HeroModel'); function HeroModel:initialize() print('HeroModel initialize') end function HeroModel.class:getInstance() ----.class is important!!!!!!! if self.hasInstance==nil then self.hasInstance=self:new() end return self.hasInstance end --HeroModel:new() s1=HeroModel:getInstance() s2=HeroModel:getInstance() if s1==s2 then print ('same model') end
--------12.20-------
至于为什么要在getInstance()方法上面加一个.class
加上.class之后就相当于是一个静态方法,也就像是java里面的static
在没有为这个类创建实例的时候就可以调用