Official ZPE/YASS documentationStructures and classes
A structure is a data type in which many inner values can be specified. It provides a framework for all variables and functions.
structure person $name = null $age = 0 $email = null function set($n, $a, $e) $name = $n $age = $a $email = $e end function end structure
Instead of the full word
being used, the keyword
struct may be used.
A structure must be declared using the
Variables and internal functions can be accessed using the object
->, as shown below:
function main($args) $s = new person() $t = copyof person() //Run an internal function within a structure $t->set("Jack", 20, "firstname.lastname@example.org") //Obtain and print a variable from a structure print($t->$name) //Set a variable in a structure $t->$name = "Joseph" end function
Prior to version 1.5.3, the object pointer literal was called the pointer
literal and was represented by
=> (fat arrow).
The fat arrow was moved to association using maps and associative arrays.
Structures may also be constructed with parameters as of version 1.5.2
of ZPE. The important thing is that they have
a function named
structure person $name = null $age = 0 $email = null function _construct($n, $a, $e) $name = $n $age = $a $email = $e end function end structure function main($args) $p = new person("John", 20, "email@example.com") end function
structure Person $name = null $age = 0 $email = null function _output() return $name & " " & $age end function end structure
Remember, in ZPE, structures are templates or prototypes for new objects and objects are the instantiated versions of these structures.
There are other special functions that can be used within an object such
_output or the
structure Person $name = null $age = 0 $email = null function _construct($n, $a, $e) $name = $n $age = $a $email = $e end function function _output() return this->$name & "(" & this->$email & ")" end function function _compare($o) return $o->$name == this->$name end function end structure function main($args) $p1 = new Person("John", 20, "firstname.lastname@example.org") $p2 = new Person("John", 48, "email@example.com") //Will return true print($p1 == $p2) end function
Version 1.6.4 of ZPE added ZPEObjectNativeFunctions as an option which allows functions defined within Java to add native Java-based function calls on objects.
Object inheritance is a major part of object oriented programming. ZPE performs late object inheritance in that it is done at runtime. This has a slight performance hit but saves over adding more properties to a single structure.
structure Animal $type = "Animal" function _output() return this->$type end function end structure structure Person inherits Animal $name = null //Using the name of the structure as the function turns the function into a constructor as of version 1.8.5 function Person($n) $name = $n this->$type = "Human" end function end structure function main($args) $p1 = new Person("Johnny Baxter") $p2 = new Person("Nick Harding") //Will return Human both times print($p1) print($p2) end function
ZPE 1.8.5 also added in the option to use the
extends keyword instead
inherits to make it more like Java or C.
ZPE 1.8.5 also added the option to use the class or structure name as a function name to create a constructor as shown in the example above.