Class
Classes are “template” to create objects.
Class define methods (functions) and properties (variables) of object.
Class start with ^
To access object’s assets, use dot operator.
Ex:
^Human:
$name
@print_info:
@echo “$name – $email \n”
Then we could create object
$people_1 :@new ^Human
$people_1>>$name: John
$people_1>>$email: “john@gmail.com”
$people_1>>@print_info
> John – john@gmail.com
Constructor
Constructors usually add data to object when create a new one, you could create several using new. GreenTea create a constructor based on the class properties count.
Ex:
^Human:
$name
@print_info:
@echo $name ” – ” $email \n”
^Human @new John, “john@gmail.com”
$?>>(@print_info)
> John – john@gmail.com
@new ^Human, John
$?>>(@print_info)
> John –
Access modifiers
(not supported yet, all are now public)
Green Tea support private and public (default) modifier.
Private asset could not be access outside object
Ex:
^Human:
$name
private $email
@print_info:
@echo “$name – $email \n”
@new ^Human, John, “john@gmail.com”
$?>>@print_info
> John – john@gmail.com
@new ^Human, John, “john@gmail.com”
@echo $?>>$name
> John
@new ^Human, John, “john@gmail.com”
@echo ($?>>$email)
(Error – permission denied)
Inherit
One class could inherit some other classes using operator << . When doing so, child class could add assets, overload method of the parent class. But could not remove or change type of the parent one’s assets.
With class Human above, we could have:
^Student << ^Human:
$point
@print_info:
@echo “$name – $email – $point \n”
$student1: (@new ^Student, John, “john@gmail.com”)
$student1>>$point: 4
$student1>>(@print_info)
> John – john@gmail.com – 4
The following are being developed
Static
Static assets are asset of the class, user don’t need to create object to access those static assets.
^Human:
static $spicies: Homo Sapien
static @getSpicies:
return $spicies
^Human>>(@getSpicies)
> Homo Sapien
Convert from array to object:
(not supported yet)
An array with all keys satisfied variable’s name rules could be converted to object with no class and all public properties.
Ex:
$obj: (@to_obj [“name”=>”John”, “age”-=> 24])
Generate class from no class object
(not supported yet)
An object with no class could generate a class using function @gen_class.
Ex:
^Human1 : @gen_class $obj
Now $obj belong to class ^Human1.