4.30. Lua object oriented

发布时间 : 2025-10-25 13:34:07 UTC      

Page Views: 9 views

Object-oriented programming is a very popular computer programming architecture.

The following programming languages support object-oriented programming:

  • C++

  • Java

  • Objective-C

  • Smalltalk

  • C#

  • Ruby

4.30.1. Object-oriented feature #

  • 1)Encapsulation: a feature that can load the information, functions, and responses of an entity into a single object.

  • 2)Inheritance: the inheritance method allows it to be extended without changing the original program, so that the original function can be preserved and the new function can be extended. This helps to reduce repeated coding and improve the efficiency of software development.

  • 3)Polymorphism: the same operation acts on different objects, which can have different interpretations and produce different execution results. At run time, methods in an implementation derived class can be called through apointer to the base class.

  • 4)Abstraction: Abstraction is a way to simplify complex real-world problems, it can find the most appropriate class definition for specific problems, and can explain the problem at the most appropriate inheritance level.

4.30.2. Object-oriented in Lua #

We know that objects are made up of properties and methods. The most basic structure in LUA is table , so you need to use table to describe the properties of the object.

lua in function can be used to represent a method. Then the classesin LUA can be simulated by table + function.

As for inheritance, you can use the metetable simulate it (not recommended, only simulate the most basic objects, most of the implementation is enough).

Table in Lua is not only an object in a sense. Like objects, tables also have states (member variables); they also have the nature of being independent of the values of objects, especially objects with two different values (table) represent two different objects; an object can have differentvalues at different times, but it is always an object; like objects, the life cycle of a table has nothing to do with what is created and where it iscreated. Objects have their member functions, and tables have:

Account = {balance = 0} function Account.withdraw (v) Account.balance = Account.balance - v end 

This definition creates a new function and saves it in the Account object’s withdraw domain, we can call the following:

Account.withdraw(100.00) 

A simple example #

The following simple class contains three attributes: area , length , and breadth . The printArea method is used to print the calculation results:

Example

-- metaclass Rectangle = {area = 0, length = 0, breadth = 0} -- Methods for Derived Classes new function Rectangle:new (o,length,breadth) o = o or {} setmetatable(o, self) self.__index = self self.length = length or 0 self.breadth = breadth or 0 self.area = length*breadth; return o end -- Methods for Derived Classes printArea function Rectangle:printArea () print("The rectangular area is ",self.area) end 

Create object #

Creating an object is the process of allocating memory to an instance of a class. Each class has its own memory and shares common data.

r = Rectangle:new(nil,10,20) 

Access Properti #

We can use a period ( . ) to access the properties of the class:

print(r.length) 

Access member function #

We can use colons : to access the member functions of the class

r:printArea() 

Allocated when the memory object is initialized.

Complete instance #

Here we demonstrate Lua complete example of object-oriented:

Example

-- metaclass Shape = {area = 0} -- Basic class methods new function Shape:new (o,side) o = o or {} setmetatable(o, self) self.__index = self side = side or 0 self.area = side*side; return o end -- Basic class methods printArea function Shape:printArea () print("The area is ",self.area) end -- create object myshape = Shape:new(nil,10) myshape:printArea() 

Execute the above program, and the output is as follows:

The area is 100 

4.30.3. Lua inheritance #

Inheritance means that one object uses the properties and methods of anotherobject directly. Can be used to extend the properties and methods of the underlying class.

The following demonstrates a simple inheritance example:

-- Meta class Shape = {area = 0} -- Basic class methods new function Shape:new (o,side) o = o or {} setmetatable(o, self) self.__index = self side = side or 0 self.area = side*side; return o end -- Basic class methods printArea function Shape:printArea () print("The area is ",self.area) end 

The next example Square object inherits from the Shape class:

Square = Shape:new() -- Derived class method new function Square:new (o,side) o = o or Shape:new(o,side) setmetatable(o, self) self.__index = self return o end 

Complete instance #

In the following example, we inherit a simple class to extend the methods ofthe derived class, which retains the member variables and methods of the inherited class:

Example

-- Meta class Shape = {area = 0} -- Basic class methods new function Shape:new (o,side) o = o or {} setmetatable(o, self) self.__index = self side = side or 0 self.area = side*side; return o end -- Basic class methods printArea function Shape:printArea () print("The area is ",self.area) end -- create object myshape = Shape:new(nil,10) myshape:printArea() Square = Shape:new() -- Derived class methods new function Square:new (o,side) o = o or Shape:new(o,side) setmetatable(o, self) self.__index = self return o end -- Derived class methods printArea function Square:printArea () print("The square area is",self.area) end -- create object mysquare = Square:new(nil,10) mysquare:printArea() Rectangle = Shape:new() -- Derived class methods new function Rectangle:new (o,length,breadth) o = o or Shape:new(o) setmetatable(o, self) self.__index = self self.area = length * breadth return o end -- Derived class methods printArea function Rectangle:printArea () print("The rectangular area is",self.area) end -- create object myrectangle = Rectangle:new(nil,10,20) myrectangle:printArea() 

Execute the above code, and the output is as follows:

Area of 100 A square area of 100 A rectangular area of 200 

4.30.4. Function rewriting #

In Lua we can override the functions of the base class and define our own implementation in the derived class:

-- Derived class methods printArea function Square:printArea () print("Square area ",self.area) end 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。