Hi @Jvm38,
Yes, you are absolutely right regarding locals, and you can create ‘stronger’ interfaces by using them, and then have ‘setters’ and getters’ in the class…
With respect to an example (not great, but hopefully works):
Person = class()
function Person:init(name) self.name = name end
Book = class()
function Book:init(author) self.author = author end
The point is whether the two classes have an ‘is a’ or ‘has a’ relationship…
If you had made author a member of Book, then when you came to have other objects that had ‘people’ e.g. a Film, Podcast or whatever, then you re-use Person…and your Author can exist in multiple Books, if that specific Person instance changes, then ALL Books pick up the change…
Also, if your Person class needs to be extended then you just extend Person, and all classes using it automatically pick up the new fields/functionality…
(Of course, books have multiple authors so you would actually have a table and an :addAuthor() function, and maybe later on you’d add :addReviewer(Person), :addProofreader(Person))
Note that you will also see the term Composition, which is similar to Aggregation but is more of an ‘is part of’ relationship, this implies that the ‘owning’ class manages the lifecycle of an object…
E.g. from the above example, a Person may be an author of multiple books, if a book say, goes out of print, the Person still exists, a better example is a Person with an Address, if a person moves, the Address (in the real world) still exists…
A Composition is more like:
Engine = class()
Car = class()
function Car:init()
self.engine = Engine()
end
The Engine ‘is part of’ a Car, if the car is scrapped, then that affects the Engine, not a great example, you could remove an Engine, but hopefully you get the idea…
So in a nutshell, work out the relationship:
IS A: consider inheritance, but ONLY if the interface is ‘clean’, and very few are… Consider a Vehicle base class, by the time you have worked out you want to have subclasses of Car, Bike, Boat and Plane, your base class has very little functionality, Boats don’t have wheels, or engines perhaps, only Planes have wings…your Vehicle base class ends up with very little reusable functionality after trying to subclass all the flavours of Vehicle, maybe it works, Vehicles have Manufacturers, and RegistrationNumbers, but even so, it is still better to have those as separate classes (IMHO) because the more you try and create subclasses of WheeledVehicle, WaterVehicle etc., the more mangled your inheritance tree gets, e.g. when you get to AmphibiousVehicle!!! ;))
HAS A, or IS PART OF: Create separate classes and ‘slot’ them into other classes as self. = Thing(…) either by passing them into :init() or creating them inside :init
Brookesi