|
Towards Object Oriented Programming in Lua
I've spent a lot of the weekend working on our Lua course ... and for those of you new to this language I'll be sharing a few snippets with you here on "The Horse's Mouth" over coming weeks. Today, I've got a short section which starts to introduce how you can do OO programming in Lua. The section of notes goes on to cover all the OO classics such as Encapsulation, Polymorphism and Inheritance and the course examples for this module are online via here
In Lua, we can keep functions in a table too, and we can bind a series of "metamethods" to a table which give it additional properties We also have syntactic sugar to make the new structures look and run sweetly. All of which leads to an effective Object Oriented capability in Lua, although without the classic keywords "class", "method", "private" etc.
Here are some of the basics - the use of the "." operator, and the storage of a piece of code as a first class function alongside the data.
stuff = {}
stuff["john"] = 14
stuff.dick = 24
function stuff.henry(value)
return value * 2
end
print (stuff.john)
print (stuff["dick"])
print (stuff.henry(16))
print (stuff["john"])
If you're trying to work out the difference between the "." operator and the use of a subscript in square brackets ... don't. They mean the same thing!
Here's how that program runs:
[trainee@easterton u107]$ lua dibdab
14
24
32
14
24
34
[trainee@easterton u107]$(written 2008-06-30 06:26:15)
Associated topics are indexed under U107 - Object Orientation - the Lua way
Some other Articles
Saying NOT in Perl, PHP, Python, Lua ...Lua Course - here is the indexDefensive coding techniques in PHP?July to December 2008 - Open Source training scheduleTowards Object Oriented Programming in LuaCo-routines in Lua - co-operative processingConversion of c/r line ends to l/f line endsSome sideways thoughts on the newscannot restore segment prot after reloc message - PHP / httpdVirtual Hosting on Apache httpd - a primer example
|
1816 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|