How to program in Grimport?

Ask us for help with programming in Grimport TRANSLATE THIS PAGE WITHOUT TOUCHING THE CODES

To Go Further

 

Classes / Objects


In Grimport, as in any other object-oriented language, there is a concept of classes and objects. A class is a collection of data and methods. The data and methods of a class are used to represent a real-world object or a concept.

Create a class

To create a class, we use the class keyword.

Here is an example of a class. The name of the class is Person which has three attributes, name, surname and state:

class Person 
{ 
	def name  
	def surname  
	def state  
}

Create an object

An object is created from a class.
To create an object, specify the name of the object, and use the keyword new followed by the class name:

class Person 
{ 
	def name 
	def surname 
	def state 
}

person1 = new Person()

You can also create several objects of one class:

class Person 
{ 
	def  name 
	def  surname 
	def  state 
}

person1 = new Person() person2 = new Person()

Pointers

To access the attributes of your objects, you can use the dot syntax (.). In this example, we assign a first name to both objects:

class Person 
{ 
	def  name 
	def  surname 
	def  state 
}

person1 = new Person() person2 = new Person() person1.name = Michel person2.name = Jack

Constructors

A constructor is a special method used to initialise objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
The constructor name must match the class name.

class  Vehicle 
{ 
	def brand   
	// Create a class constructor for the class Vehicle  
	Vehicle()
	{    
		brand = "Ford"  
	} 
}

def myCar = new Vehicle()       // Create an object of class Vehicle console(myCar.brand) // -> Ford

Constructors can also take parameters which are used to initialise attributes:

class  Vehicle 
{ 
	def brand = "Ford"  
	Vehicle(x)
	{    
		brand = x  
	} 
}

def myCar = new Vehicle("Mustang")   console(myCar.brand) // -> Mustang

You can have as many parameters as you like:

class  Vehicle 
{ 
	def  modelYear 
	def  modelName

Vehicle(year, name) {  modelYear = year modelName = name } } def myCar = new Vehicle(1969, "Mustang") console(myCar.modelYear + " " + myCar.modelName)
// -> 1969 Mustang

Inheritance

It is possible to inherit attributes and methods from one class to another. Inheritance allows a child class (or subclass) to reuse the code or properties of a parent (or superclass).
A child class can have at most one parent class.

To inherit from a parent class, use the extends keyword.

In the example below, the Car class (subclass) inherits the attributes of the Vehicle class (superclass):

class Vehicle 
{ 
	def brand = "Ford" 
}

class Car extends Vehicle { def model = "Mustang" } myCar = new Car() console(myCar.brand + " " + myCar.model) // -> Ford Mustang

Now that you know what inheritance is between several classes you can more easily understand the next part.

 

 


Next ❯ ❮ Previous