Objects and Classes

Objects and Classes

> LeekScript Tutorial

Welcome to the documentation on object-oriented programming (OOP) which consists of creating classes and objects to organize your code. This feature was added from LeekScript 2.

Objects

Objects are values ​​grouping several values ​​each associated with a name. These values ​​are called properties or fields.

Creating an object

To create an object, we use the following syntax:

In this example, the object object has two properties, name and age.

To create an empty object:

Access to a property

The dot . allows access to a property of an object:

An alternative syntax exists: object["prop"], allowing to use a string value to access a property:

Modifying a property

Once accessed, a property can be modified like a normal variable:

Object Comparison

The == and === operators only compare if the object is the same. They return false even if the two objects have the same properties:

List of properties

Classes

A class is a model for creating objects.

Defining a class

We define a Weapon class with the 4 fields named this way:

Creating an object with a class

The new is actually optional, a class is similar to a function and can be called directly to create an object:

You can retrieve the class of an object using .class:

instanceof

The instanceof keyword is used to check if an object is an instance of a class:

Builders

A constructor is a function defined in a class allowing to create an object from a list of parameters. We define a constructor like this:

Using a constructor:

Methods

A method is a function defined in a class and usable on an object of this class. We define a method like this:

We use a method with the syntax:

The object["member"] syntax also works:

It is not advisable to use it, the risk of making a mistake is quite strong.

Overload

It is possible to overload methods by simply changing the number of parameters

You can then use the method with one or two parameters of your choice:

Inheritance

The notion of inheritance makes it possible to create a hierarchy of classes. We use this syntax with the extends keyword. We are trying to declare a "base" class from which other classes can be built. Classes that inherit 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 will be present in the two child classes. But Chip has a 5th field, cooldown specific to chips.

If we instantiate these three objects and display them, we can see the different members of each class:

Our class Chip has added a cooldown member, which therefore only appears in its instance and not the others!

An inherited class will have the same type as the base class. For example if an object is an instance of the Weapon class, this object is also an instance of the Item class:

The inherited classes also retrieve the methods and from the parent class. To call a parent constructor:

To call a parent method:

If you override a method without calling the parent method with super, you completely erase the base implementation.

A small example to sum it all up quickly:

Access Levels

It is possible to define access levels for each member of a class. There are 3 possible access levels: public (the default), protected and private.

Static Members

A static member (field or method) is a member that belongs to a class instead of an object of the class.

Static fields are initialized once (in the first round), like globals.

Definition of a static field:

Static method:

string() method

If a class contains a string() function, this is called automatically when the object needs to be converted into a string: