Classes
A class is a declaration of a constructor of objects with a specific type. Each object has zero or more attributes (instance variables) with zero or more behaviours (methods), that are defined inside a specific class or inherited from super-classes.
1
class Person (name, age, address) {
2
method position {
3
# GPS.locate(self.address)
4
}
5
6
method increment_age(amount=1) {
7
self.age += amount
8
}
9
}
10
11
var obj = Person(
12
name: "Foo",
13
age: 50,
14
address: "St. Bar"
15
)
16
17
say obj.age # prints: 50
18
say obj.name # prints: "Foo"
19
say obj.address # prints: "St. Bar"
20
21
obj.name = "Baz" # changes name to "Baz"
22
say obj.name # prints: "Baz"
23
24
obj.increment_age # increments age by 1
25
say obj.age # prints: 51
Copied!

Class attributes

The attributes of a class can be either specified as parameters, or declared with the has keyword.
1
class Example(a, b) {
2
has c = 3
3
has d = a+c
4
}
5
6
var obj = Example(1, 2)
7
8
say obj.a #=> 1
9
say obj.b #=> 2
10
say obj.c #=> 3
11
say obj.d #=> 4
Copied!

Class initialization

Extra object-initialization setup can be done by defining a method named init, which will be called automatically called whenever a new instance-object is created.
1
class Example (a, b) {
2
3
has r = 0
4
5
method init { # called automatically
6
r = a+b
7
}
8
9
method foo {
10
r
11
}
12
}
13
14
var obj = Example(3, 4)
15
say obj.foo #=> 7
Copied!

Class variables

The syntax ClassName!var_name can be used for defining, accessing or modifying a class variable.
1
class Example {
2
3
Example!hidden = 'secret' # global class variable
4
5
method concat (str) {
6
str + ' ' + Example!hidden
7
}
8
}
9
10
var x = Example()
11
var y = Example()
12
13
say x.concat('foo') #=> 'foo secret'
14
say y.concat('bar') #=> 'bar secret'
15
16
Example!hidden = 'public' # changing the class variable
17
18
say x.concat('foo') #=> 'foo public'
19
say y.concat('bar') #=> 'bar public'
Copied!
The modification of a class variable can be localized by prefixing the declaration with the local keyword:
1
local Example!hidden = 'local value'
Copied!

Class inheritance (experimental)

Inheritance of behaviors and attributes, by a given class, is declared with the < operator, followed by the name of the class from which the current class inherits:
1
class Animal(String name, Number age) {
2
method speak { "..." }
3
}
4
5
class Dog(String color) < Animal {
6
method speak { "woof" }
7
method ageHumanYears { self.age * 7 }
8
}
9
10
class Cat < Animal {
11
method speak { "meow" }
12
}
13
14
var dog = Dog(name: "Sparky", age: 6, color: "white")
15
var cat = Cat(name: "Mitten", age: 3)
16
17
say dog.speak #=> woof
18
say cat.speak #=> meow
19
say cat.age #=> 3
20
say dog.ageHumanYears #=> 42
21
say dog.color #=> white
Copied!
Multiple inheritance is declared with the << operator, followed by two or more class names, separated by commas:
1
class Camera { }
2
class MobilePhone { }
3
class CameraPhone << Camera, MobilePhone { }
Copied!
Last modified 1yr ago