Skip to content

Latest commit

 

History

History
63 lines (43 loc) · 2.64 KB

7.md

File metadata and controls

63 lines (43 loc) · 2.64 KB

LERN/LS/7

This example consists of a single file, LS/7.iss

Enter the command run lern/ls/7 to run the example.

Object-Oriented Programming with LavishScript

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 variables:

objectdef fruit
{
    variable string Name="Apple"
    variable string Color="Red"
}

Creating our custom object with variable

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.

Accessing our custom object's variables

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}

Exercises

  1. Replace the Name and Color values defined within the fruit, and run the script again!

  2. Rename the Name and Color variables, and try again!

  3. Variables can be assigned a value with the = syntax. Try assigning a value to WhatFruit (such as variable 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!