This example consists of a single file, LS/7.iss
Enter the command run lern/ls/7
to run the example.
Object-Oriented Programming is a concept designed to allow a developer to tie available functionality to a type of data.
In LavishScript, this is done with an objectdef
, short for object definition. An objectdef
is the same thing as a class
in many other object-oriented programming languages.
A valid objectdef
has a name following it on the same line, and has a pair of code block braces {
and }
. As with all LavishScript code block braces, these braces must each be on their own line!
Like this:
objectdef fruit
{
}
In this example, we begin to explore the functionality of objects by creating one that simply contains two variable
s:
objectdef fruit
{
variable string Name="Apple"
variable string Color="Red"
}
With fruit
defined as an objectdef
, creating a fruit
variable is now done the same as creating a string
variable:
variable fruit WhatFruit
This provides us with a fruit
variable called WhatFruit
. WhatFruit
, in turn, contains two variables
-- Name
and Color
.
At this point, we have a variable called WhatFruit
, and nested inside of that variable we have two string
variables, called Name
and Fruit
.
To access a nested variable, we use a Data Sequence with the name of the first variable, a .
(dot), and then the name of the nested variable, like this:
echo Name: ${WhatFruit.Name}
That's is why it is called a Data Sequence: WhatFruit.Name
is the sequence that leads us to the nested data we actually desired. It is essentially a look-up address, used by LavishScript to locate the desired value -- much like a postal service uses your mailing address, a web browser uses a URL, and so on.
To access a different variable from the same object, we simply use the other variable's name instead.
echo Color: ${WhatFruit.Color}
-
Replace the
Name
andColor
values defined within thefruit
, and run the script again! -
Rename the
Name
andColor
variables, and try again! -
Variables can be assigned a value with the
=
syntax. Try assigning a value toWhatFruit
(such asvariable fruit WhatFruit="Hello World!"
), and run the script again!- Did anything seem to change? Why do you suppose? We'll come back to this soon!