----------------------------------------------------------

--面向对象核心库

----------------------------------------------------------

----------------------------------------------------------

--base object

object = {}

function object:getName()

return "unknow"

end

function object:resetHashCode()

self.__hashcode = nil

self:getHashCode()

end

function object:getHashCode()

self.__hashcode = self.__hashcode or (os.time() + math.random())

return  self.__hashcode

end

----------------------------------------------

-- create interface helper

function createInterface(object,name,func)

warning(object[name] == nil,"Interface exists,create fail" .. name)

end

function callInsterface(object,name,...)

local func =  object[name]

return func(object,...)

end

function callmethod(object,name,...)

local func =  object[name]

return func(object,...)

end

function implementInterface(object,name,func)

object[name] =  func

end

function createAbstract(object,name,func)

object[name] =  function(self)

warning(false,"not implement Abstract " .. name)

return nil

end

end

function createVirtualFunction(object,name,func)

implementInterface(object,name,func)

end

function override(object,name,func)

implementInterface(object,name,func)

end

function sealed(object)

object.__sealed = true

end

----------------------------------------------

--oop helpers

local function addctor(class)

assert(class,"class is nil value")

if class.ctor == nil then

class.ctor = function(self) end

end

end

function registerFun_getInstance(cls)

--    warning(cls.getInstance == false,"cls has function:getInstacnce")

cls.__instance = nil;

cls.getInstance =function()

if cls.__instance == nil then

assert(cls.new and type(cls.new)== "function","not found ctor function :new");

cls.__instance = cls:new()

return cls.__instance

end

return cls.__instance

end

end

---向对象中添加临时数据

function setTempVariable(object,key,value)

assert(key,"key is a nil value")

assert(value~=nil,"value is a nil value")

object.__tempData = object.__tempData or  {}

object.__tempData["__"..key] = value

end

---查找对象中保存的临时数据

function getTempVariable(object,key)

assert(object,"object is a nil value")

assert(key,"key is a nil value")

if object.__tempData == nil then  return nil end

return  object.__tempData["__"..key]

end

function callSupper(instance,supperName,name,cls,...)

if instance ~= nil then

if instance.supper  == nil then return end

if  instance.supper[supperName] ~=nil then

for key, var in pairs( instance.supper[supperName]) do

if key ==  name then

local fun = var

local returnValue,e = pcall(fun,cls or instance,...)

assert(e==nil,e)

return returnValue,true

end

end

else

for key, var in pairs(instance.supper) do

if type(var) == "table" and key~="__index" and key ~= "__child" then

local returnValue,result = callSupper(var,supperName,name,instance,...)

if result == true then return returnValue,true end

end

end

end

if instance.__child ~=nil then

local returnValue,result = callSupper(instance.__child,supperName,name,cls,...)

if result == false and instance.__child.__child then

returnValue,result = callSupper(instance.__child.__child,supperName,name,cls,...)

end

return returnValue,true

end

end

return nil,false

end

---调用基类中的函数

function callSupperA(instance,supperName,name,...)

return callSupper(instance,supperName,name,instance,...)

end

local function copyAllSupper(class,base)

if base ~= nil and class ~= nil and base.supper ~= nil then

class.supper = class.supper or {}

for key, var in pairs(base.supper) do

if class.supper[key] == nil then

class.supper[key] =  var

end

end

end

end

--继承帮助函数

--ext:扩展类,

--base:基类

--supper name

---------------------------------------------

--@param #table ext 当前类的定义

--@param #table base 当前类的要继承的类,一般也为table

--@param #string suppername 当前继承的基类的别名

--@return #table 一个新的对旬的实列

function inheritanceA(ext,base,suppername)

--    assert(ext,"not set ext object.")

--    assert(base,"not set base object.")

--    assert(base.__sealed == nil,"基类设置为了禁止继承.")

if base.__sealed ~= nil then

assert(base.__sealed == nil,"基类设置为了禁止继承.")

return

end

--metatable copy

local function setmetatableA(tag,source)

for k,v in pairs(source) do

if tag[k] == nil then

if type(v) == "table" and k ~= "__index" and k~= "__child" then

tag[k] = {}

setmetatableA (tag[k],v)

tag[k].__index = tag[k]

else

tag[k] =  v

end

end

end

return tag

end

ext = setmetatableA(ext,base)

ext.__index = ext

--添加自己的构造函数

if ext.new == nil then

function ext.new  ( self )

local instance = {}

setmetatableA(instance,self)

instance.__index = instance

instance:ctor()

instance:resetHashCode()

return instance,ext.supper

end

addctor(ext)

end

ext.supper = ext.supper or {}

setmetatableA(ext.supper,base)

ext.supper.__child = ext

ext.supper.__index = ext.supper;

---添加基类的默认构造函数

if ext.supper and ext.supper.new == nil then

---此逻辑的意义不大

ext.supper.new =  ext.supper.new or function ( self )

local instance = {}

setmetatableA(instance,self)

instance:resetHashCode()

return ext.supper,ext.supper or {}

end

addctor(ext.supper)

end

if suppername ~= nil and type(ext.supper[suppername])~= "function" then

ext.supper[suppername] =  ext.supper[suppername] or {}

setmetatableA(ext.supper[suppername],base)

ext.supper[suppername].__index = ext.supper[suppername]

end

ext.__index = ext;

---注册单列接口

registerFun_getInstance(ext)

return ext,ext.supper

end

---继承对象实例

--@param #table ext 当前对象的实例

--@param #table base 基类的实例

function inheritance(ext,base)

--    assert(false,"调用本函数只能通过supper来访问,如果多少继承,只有最后一次继承的会生效")

return inheritanceA(ext or {},base,"__supper")

end

---继承自model 文件

--@param #table ext 当前对象的实例

--@param #string mname 当前要继承的模块对象

function inheritanceM(ext,mname)

local obj = require (mname)

assert(obj~=nil,"load mode error,mode name:"..mname)

return inheritanceA(ext,obj,mname)

end

return object

lua 脚本之高级篇 (面向对象的完全支持),有性能问题。的更多相关文章

  1. Javascript高级篇-面向对象的特性

    一.创建对象 1.1初始化器 var any={ name:"some", age:10, action:function(){ alert(this.name+":&q ...

  2. 要想用活Redis,Lua脚本是绕不过去的坎

    前言 Redis 当中提供了许多重要的高级特性,比如发布与订阅,Lua 脚本等.Redis 当中也提供了自增的原子命令,但是假如我们需要同时执行好几个命令的同时又想让这些命令保持原子性,该怎么办呢?这 ...

  3. Lua学习高级篇

    Lua学习高级篇 之前已经说了很多,我目前的观点还是那样,在嵌入式脚本中,Lua是最优秀.最高效的,如果您有不同的观点,欢迎指正并讨论,切勿吐槽.这个系列完全来自于<Programming in ...

  4. 第二十三篇:在SOUI中使用LUA脚本开发界面

    像写网页一样做客户端界面可能是很多客户端开发的理想. 做好一个可以实现和用户交互的动态网页应该包含两个部分:使用html做网页的布局,使用脚本如vbscript,javascript做用户交互的逻辑. ...

  5. 第五篇:python高级之面向对象高级

    python高级之面向对象高级   python高级之面向对象高级 本节内容 成员修饰符 特殊成员 类与对象 异常处理 反射/自省 单例模式 1.成员修饰符 python的类中只有私有成员和公有成员两 ...

  6. 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!

    [COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...

  7. Unity3D热更新之LuaFramework篇[07]--怎么让unity对象绑定Lua脚本

    前言 在上一篇文章 Unity3D热更新之LuaFramework篇[06]--Lua中是怎么实现脚本生命周期的 中,我分析了由LuaBehaviour来实现lua脚本生命周期的方法. 但在实际使用中 ...

  8. Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween

    在上一篇文章 Unity3D热更新之LuaFramework篇[04]--自定义UI监听方法 中,我对LuaBehaviour脚本进行了扩展,添加了两个新的UI监听方法,也提到最好能单写一个脚本处理此 ...

  9. Redis分布式锁—SETNX+Lua脚本实现篇

    前言 平时的工作中,由于生产环境中的项目是需要部署在多台服务器中的,所以经常会面临解决分布式场景下数据一致性的问题,那么就需要引入分布式锁来解决这一问题. 针对分布式锁的实现,目前比较常用的就如下几种 ...

随机推荐

  1. HDU 4417 划分树写法

    Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...

  2. Exponial~(欧拉函数)~(发呆题)

    Description Everybody loves big numbers (if you do not, you might want to stop reading at this point ...

  3. 获取oracle当前系统设置了哪些事件

    ALTER SESSION SET EVENTS '10046 trace name context forever,level 12' 会话已更改. DECLARE EVENT_LEVEL NUMB ...

  4. php设定错误和异常处理可使用的函数

    1.register_shutdown_function 使用场景:当我们的脚本执行完成或意外死掉导致PHP执行即将关闭时,这个函数会被调用. 函数介绍: void register_shutdown ...

  5. python_plot画图参数设置

    # coding:utf-8 import pandas as pd import numpy as np import matplotlib.pyplot as plt # one_hot数据的读取 ...

  6. 基于x64的处理器意思

    基于x64的处理器意思是CPU的架构是X64的,也是64位的CPU. 基本简介: "x86-64",有时会简称为"x64",是64位微处理器架构及其相应指令集的 ...

  7. Ubuntu Touch环境搭建

    最近搞了一下Nexus 5的MultiRom Manger,体验了一把Ubuntu Touch和Android L,总体感觉还不错,不过Android L的NFC驱动还有问题,Ubuntu Touch ...

  8. Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向”

    Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向” 原因还未知..

  9. float和double类型的存储方式

    Float double 类型在计算机的存储方式 计算机中只认识10的二进制数,那么该如何存储小数呢? 那么我们先看Floa类型: Float在计算机(32位)中是4个字节的,具体地:第一位为符号位0 ...

  10. easyUi根据一个日期给另一日期自动赋值的js

    $('#loanbegindate').datebox({ onSelect:function(date){ changeDate(); } }); $('#loanterm,#loantermtyp ...