• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

LUAOOP编程实现方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

确实如此, 同时可以采用其它lua代码的方式实现OOP的特性。

 

OOP四大特性

抽象

封装

继承

多态

http://www.cnblogs.com/xiaosongluffy/p/5072501.html

四大基本特性:

抽象:提取现实世界中某事物的关键特性,为该事物构建模型的过程。对同一事物在不同的需求下,需要提取的特性可能不一样。得到的抽象模型中一般包含:属性(数据)和操作(行为)。这个抽象模型我们称之为。对类进行实例化得到对象。

封装:封装可以使类具有独立性和隔离性;保证类的高内聚。只暴露给类外部或者子类必须的属性和操作。类封装的实现依赖类的修饰符(public、protected和private等)

继承:对现有类的一种复用机制。一个类如果继承现有的类,则这个类将拥有被继承类的所有非私有特性(属性和操作)。这里指的继承包含:类的继承和接口的实现。

多态:多态是在继承的基础上实现的。多态的三个要素:继承、重写和父类引用指向子类对象。父类引用指向不同的子类对象时,调用相同的方法,呈现出不同的行为;就是类多态特性。多态可以分成编译时多态和运行时多态。

 

Code

--

-- Class helper routines

--

-- Instantiates a class

local function _instantiate(class, ...)

    local inst = setmetatable({}, {__index = class})

    if inst.__init__ then

        inst:__init__(...)

    end

    return inst

end

--- Create a Class object (Python-style object model).

-- The class object can be instantiated by calling itself.

-- Any class functions or shared parameters can be attached to this object.

-- Attaching a table to the class object makes this table shared between

-- all instances of this class. For object parameters use the __init__ function.

-- Classes can inherit member functions and values from a base class.

-- Class can be instantiated by calling them. All parameters will be passed

-- to the __init__ function of this class - if such a function exists.

-- The __init__ function must be used to set any object parameters that are not shared

-- with other objects of this class. Any return values will be ignored.

-- @param base The base class to inherit from (optional)

-- @return A class object

-- @see instanceof

-- @see clone

function class(base)

    -- __parent 属性缓存父类,便于子类索引父类方法

    return setmetatable({__parent = base}, {

        __call = _instantiate,

        __index = base

    })

end

--[[

local function searchParentClass(k, plist)

    for i=1, #plist do

        local v = plist[i][k]

        if v then return v end

    end

end

--- Create a Class object (Python-style object model).

-- The class object can be instantiated by calling itself.

-- Any class functions or shared parameters can be attached to this object.

-- Attaching a table to the class object makes this table shared between

-- all instances of this class. For object parameters use the __init__ function.

-- Classes can inherit member functions and values from a base class.

-- Class can be instantiated by calling them. All parameters will be passed

-- to the __init__ function of this class - if such a function exists.

-- The __init__ function must be used to set any object parameters that are not shared

-- with other objects of this class. Any return values will be ignored.

-- @param base The base class to inherit from (optional)

-- @return A class object

-- @see instanceof

-- @see clone

function class(...)

    local mtb = {}

    local parents = {...}

    mtb = setmetatable(mtb, {

    __call = _instantiate,

    __index = function(t,k)

        return searchParentClass(k, parents)

    end})

    mtb.__index = mtb

    return mtb

end

]]

--- Test whether the given object is an instance of the given class.

-- @param object Object instance

-- @param class Class object to test against

-- @return Boolean indicating whether the object is an instance

-- @see class

-- @see clone

function instanceof(object, class)

    local meta = getmetatable(object)

    while meta and meta.__index do

        if meta.__index == class then

            return true

        end

        meta = getmetatable(meta.__index)

    end

    return false

end

 

说明:

1、 通过class来确定继承关系

2、 通过instanceof来判断继承关系


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
关于Lua一点分析发布时间:2022-07-22
下一篇:
[置顶]轻量级语言Lua入门发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap