> LeekScript
Welcome to the documentation on object-oriented programming (OOP), which consists of creating classes and objects to organize your code. This feature has been available since LeekScript 2.
Objects are values that group several values, each associated with a name. These values are called properties or fields.
To create an object, use the following syntax:
In this example, the object object has two properties, name and age.
To create an empty object:
The dot . gives access to a property of an object:
There is an alternative syntax, object["prop"], which lets you use a string value to access a property:
Once accessed, a property can be modified like a normal variable:
The == and === operators only check whether both sides are the same object. They return false even if the two objects have the same properties:
Note: === is deprecated as of LeekScript 4, because == is strict there (for example, null == 0 is no longer true).
A class is a template for creating objects.
Here is how to define a Weapon class with 4 named fields:
The new keyword is actually optional: a class works like a function and can be called directly to create an object:
You can get the class of an object with .class:
The instanceof keyword checks whether an object is an instance of a class:
A constructor is a function defined in a class that creates an object from a list of parameters. You define a constructor like this:
Using a constructor:
A method is a function defined in a class that can be used on any object of that class. You define a method like this:
You call a method with this syntax:
The object["member"] syntax also works:
It is not recommended, though: it is quite easy to make a mistake with it.
> Note: methods can also use Annotations.
You can overload methods simply by changing the number of parameters:
You can then call the method with either one or two parameters:
Inheritance lets you create a hierarchy of classes, using the extends keyword. The idea is to declare a "base" class from which other classes can be built. Classes that inherit from it receive all the members (variables) and methods (functions) of the base class, and can redefine them or add new ones.
In this example, the parent class Item has two child classes, Weapon and Chip. Item has 4 members (id, name, cost and damage), which are present in both child classes. But Chip has a 5th field, cooldown, specific to chips.
If you instantiate these three classes and display the objects, you can see the members of each class:
Our Chip class added a cooldown member, so it only appears in its own instances and not in the others!
A child class also has the type of its base class. For example, if an object is an instance of the Weapon class, it is also an instance of the Item class:
Child classes also inherit the methods of the parent class. To call a parent constructor:
To call a parent method:
If you redefine a method without calling the parent method with super, the base implementation is completely replaced.
A quick example to sum it all up:
You can set an access level for each member of a class. There are 3 possible access levels: public (the default), protected and private.
public (default): the member can be accessed directly, by anyone.protected: the member can only be accessed in the class and its child classes.private: the member can only be accessed in the class itself.A static member (field or method) is a member that belongs to the class itself rather than to an object of the class.
Static fields are initialized only once (on the first turn), like global variables.
Defining a static field:
Static method:
To make a class member read-only, so that it cannot be modified, use the final keyword. Such a member can only be initialized where it is declared or in the constructor.
If a class has a string() method, it is called automatically whenever the object needs to be converted to a string:
> Note: you can define a string method with default parameters; however, when the object is concatenated, the method is called without arguments.
> Note 2: when a class has no string() method but has a parent class, the parent's method is used. That is why it can be useful to use this.class.name instead of hard-coding the class name.
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.