Implementation method of LUA OOP programming

Source: Internet
Author: User
Tags lua

Lua native does not support OOP features

This is true, and the features of OOP can be implemented in other LUA code ways.

Four features of OOP

Abstract

Packaging

Inherited

Polymorphic

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

four basic features:

Abstraction : The process of extracting a key feature of something in the real world and building a model for that thing. For the same thing under different requirements, the characteristics that need to be extracted may differ. The resulting abstract model typically contains: attributes (data) and operations (behaviors). This abstract model we call class . Instantiates a class to get an object.

Encapsulation : Encapsulation enables classes to be independent and isolated, guaranteeing the high cohesion of classes. Exposes only properties and operations that are necessary to the outer or child class of the class. Class-Encapsulated implementation-dependent class modifiers (public, protected, private, and so on)

inheritance : A reuse mechanism for existing classes. If a class inherits an existing class, the class will have all the non-private attributes (properties and operations) of the inherited class. The inheritance here refers to the inheritance of classes and the implementation of interfaces.

polymorphism: Polymorphism is achieved on the basis of inheritance. Three elements of polymorphism: inheritance, overrides, and parent class references to child class objects. When a parent class reference points to a different subclass object, the same method is called, rendering a different behavior, or a class polymorphic attribute. Polymorphism can be divided into compile-time polymorphism and run-time polymorphism.

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 is attached to the this object.

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

--all instances of the This class. For object parameters use the __INIT__ function.

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

--Class can is instantiated by calling them. All parameters'll be passed

--The __init__ function of this class-if such a function exists.

--The __INIT__ function must is used to set any object parameters that is not shared

--with the objects of this class. Any return values would be ignored.

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

--@return A class object

--@see instanceof

--@see Clone

function Class (Base)

--__parent Property Cache parent class for child class Index parent class method

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 is attached to the this object.

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

--all instances of the This class. For object parameters use the __INIT__ function.

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

--Class can is instantiated by calling them. All parameters'll be passed

--The __init__ function of this class-if such a function exists.

--The __INIT__ function must is used to set any object parameters that is not shared

--with the objects of this class. Any return values would 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

Description

1, through class to determine the inheritance relationship

2, through the instanceof to judge the inheritance relationship

Implementation method of LUA OOP programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.