Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Lua module U107
Object Orientation - the Lua way
Exercises, examples and other material relating to training module U107. This topic is presented on public courses Learning to program in Lua, Lua Programming, Lua Programming




Articles and tips on this subjectupdated
4753Lua, Tcl, Python, C and C++ courses - at our Melksham HQ or on your site - forward from July 2017
30th June was the final day of the Well House Consultants (financial) year, the end of an era and the start of a fresh new one. There's a summary at [here] of the dozen (yes - 12!) changes in our lives this yeat and the end of Friday was perhaps the biggest change point. YES - we still offer (and indeed ...
2017-07-02
 
4573Classic style OO code - in Lua
Lua tables can hold not only data values, but also functions - those functions defining behaviours. See [here]. However, if you have a lot of tables (data items) all of which require the same code attached, that's not efficient and so you can set up a metatable - a subsidiary table of extra information ...
2015-11-05
 
4572Tables with values and code in Lua - looks like an object?
In Lua, we can use variables to hold both data and code, and we can use tables to hold groups of values (numbers and strings) and code assiciated with those variables too. A new example - [here] on our web site - shows how I can define such a table:   south = {cars=3,seats=78,time="06:38",destiny="Southampton"} add ...
2015-11-05
 
4273Dot or Colon separator between table name and member in Lua - what is the difference?
When do I use a dot and when do I use a colon in referencing a table member in Lua? It's a good question and one that we answer (and demonstrate to help make clear) on our Lua Programming Course. Dot refers to a table member. So if I write   source.read() I am saying "run the read function ...
2014-05-06
 
4248Metatables, Metamethods, classes and objects in Lua
In Lua, almost everything (!) is held as a table. You don't have lists, hashes, dicts, arrays, objects, namespaces - you have tables. It helps consistency, in helps keep the language small - but at times it does mean you have to be a little bit more aware of how the features of tables can be combined ...
2014-03-20
 
4117Is Lua an Object Oriented language?
What makes an "Object Oriented Language"? Well - there are certain features you need, such as references, type handing, inheritance support and polymorphism if you're going to write code in a completely OO way, but there are also ways of following the OO paradigm in significant parts with just about ...
2013-06-15
 
3730What is a metatable? How do I set one up? How do I use them? Lua
What is a metatable? In Lua, you can hold a whole bundle of information about something in a table, which gives you a convenient "handle" to that information - be that information in the form of values, or behaviours (i.e. conventional variable of code). This is illustrated in earlier articles on Lua ...
2012-05-13
(longer)
3727Using Lua tables as objects
Since tables in Lua can hold data members of different types, they can be used as the base structure for an Object Oriented design approach, where each table represents an object with numbers, strings, other tables ("objects") and callable blocks ("functions" or methods) as members. And if you take ...
2012-05-11
 
3694Special __ methods you can use in Lua metatables
In Lua, you can associate an additional table (known as a metatable) with each table of data and code you have. By doing so, repeated values and functions that apply across as whole series ("type", "class") of tables only have to be defined once. A very neat way for Lua to provide the OO facilities ...
2012-04-12
 
3683Weak references in Lua - what are they, and why use them?
In programming languages where memory is allocated dynamically (that applies to most of the languages we teach), there has to be a mechanism to gather up memory that is no longer used as the program is running so that it can be re-allocated later within the same program, and that mechanism is usually ...
2012-04-07
 
3524Metaclasses (Python) and Metatables (Lua)
"A Metaclass (Python) or Metatable (Lua) adds extra code / data / behaviour to an object, table, or class." What does that mean? It means that I can define a behavior that I want to be shared, that I want to define at run-time, that I want to amend ... in a separate module / zone / table from my main ...
2011-12-03
 
3396Tables as Objects in Lua - a gentle introduction to data driven programming
In Lua, everything that's a collection is held in a table. During the Lua Course I have been giving this week, I set up a whole series of tables, object-like, to hold details of each station on the British main rail network. As well as data, the table holds code / code references, and by adding different ...
2011-08-17
 
3142Private and Public - and things between
A public train service is one which is available for anyone to travel on; a private one only takes limited passengers as invites / made available by the operator. And there could be intermediate levels too - I understand that at times the British Transport Police protect the last train from Weymouth ...
2011-01-25
(longer)
2710__index and __newindex in Lua - metatable methods
When you call up an element in a table in Lua, it's like accessing an array element - and it works well. But you can go further by associating a metatable with a table - that's another table with a whole lot of extras - and that allows you to alter the behaviour of the original table. Since Lua works ...
2010-04-05
 
2701Is Lua an Object Oriented language?
Lua isn't an object oriented languages in the sense that you define classes, create objects with a new method, define interfaces and declare variables to be private or protected ... but never the less is a tool in which you can use object oriented techniques with (indeed) a freedom and flexibility that ...
2010-04-02
 
2703Lua Metatables
In a recent article ([here]), I asked if Lua was an object oriented language - and I concluded that it supports the OO paradigm very well, even though it doesn't have keywords like "class", "private" or "extends". At the end of that article, I mentioned metatables - a subject I have carefully sidestepped ...
2010-04-02
 
2455Lua examples - coroutines, error handling, objects, etc
I have presented a bespoke Lua course over the last three days ... and it has lead me to some interesting new examples which I'm sharing here. Object Orientation is more a way of thinking and coding than a language feature in Lua - when you set up a piece of data / structure / object, you do so in a ...
2009-10-17
 
2359A fresh example - objects the Lua way
One of the requirements we often have in our web server log file analyses is to connect together a series of accesses from a single IP address into a "visit", and this formed an example on Tuesday's Lua course. In Lua, you can overload (i.e. redefine) operators so that they work differently on tables ...
2009-08-13
 
2318For Lua Programmers AND for Town Planners
I'm always happy to be able to make multiple use of the same piece of work, and so it is with an example written last week. READ THIS SIDE ... if you are a Lua or game programmer I was asked to provide an exercise during the Lua course that related to game playing - where a series of elements (of ...
2009-08-04
(longer)
1819Calling base class constructors
In all object oriented languages, you have a facility called inheritance where you can define one type of thing ("class of object") based on another, and the newly defined class ("subclass" or "extended class") takes the initial ("base") class as it starting point. In your code for your base class, ...
2008-10-03
 
1743First class functions in Lua lead to powerful OO facilities
"In Lua, function names are first class variables." Ok - so what does that mean? At the most direct / simplest, it means that a function can be stored in a variable - so that you can write function oik()   print ("does the real work!")   end and then action = oik action() to ...
2008-08-07
 
1699If you are learning Lua, here are some more examples
During some courses, I write few (if any) new examples in front of my delegates, rather relying on the standard ones in the training notes (which I wrote anyway!) and making small changes and adjustments to illustrate how things work. On other courses, though, I write a some new examples from scratch ...
2008-08-05
 
1692Towards 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 ...
2008-06-30
 
Examples from our training material
Cube.lua   Definition of a cube object
Dice.lua   Definition of a dice object - Inheritance
Sphere.lua   Definition of a sphere object
acobj   The OO paradigm applied in Lua
alog   Program to use visit objects
anim_o1.lua   Tables to associate functions with data
animals.lua   Using a metatable to define print, <, etc
cat   adding in extra behavious
couples   Moving towards Lua objects - Metatables
cuboid   Illustrates use of a class of objects
dibdab   We can keep funtions in a table too
ii5   bus and train tables, and printing metatable
index_123   __index and __newindex
kitten   Setting up tables and printing them out - control
lottaob   Polymorphism in Lua - table of objects of different types
meta1   preparations for metatables - this is just a table
meta2   Simple use of a metatable to create a class
meta3   creating and using a table of tables("objects")
meta4   redefining operators
metamid   Adding user methods via a metatable
metapetite   Overriding an operator on a table
metasmall   Metatable to change a table characteristics
metatiny   Metatables - the mechanism
newCube.lua   Definition of a class in Lua
north   Comparing dot and colon
oblet   Hello Object World
obtable   Polymorphism in Lua - table of objects of different types
overload_add.lua   Operator (+) and to_string overloading, Lua
rail5   a table for each station
service_12   table for data and functionallity
tabby   Define special behaviours ...
train_objects   using tables as classic objects
trains   introducting metatables
trains_control   pre-metatables
visit.lua   definition of visit objects
xlog.lua   Access log - exercise framework
zoo.txt   Sample data file for animal demo
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from [here].
Topics covered in this module
Mutable and Immutable Values.
MetaMethods and functions as members
Metatables, __index, __add and __lt
Polymorphism and inheritance
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Ruby, Lua, Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses at our training centre in Melksham, Wiltshire, England. It's surprisingly cost effective to come on our public courses - even if you live in a different country or continent to us.

We have a technical library of over 700 books on the subjects on which we teach. These books are available for reference at our training centre.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/U107.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb