-
Notifications
You must be signed in to change notification settings - Fork 3
Classes and OOP
Hassium allows you to declare your own classes as well as your own functions. Classes are objects that can have functions, properties, traits, and other classes declared inside them, as well as have object attributes.
The first type of classes we will be looking at are static classes. Static meaning that you are directly calling attributes of the class. The syntax of a class is the following: class <name> [: inherit1, inherit2, ... ] { <body> }
Throughout this section we will be creating a calculator class, start by opening a new file and typing the following:
class Calculator {
}
This declares a new class called Calculator
, with nothing inside it. Let's start by adding a function for adding:
class Calculator {
func add (x : int, y : int) : int {
return x + y;
}
}
Now if we wish to call the add
method from main
, we can simply do:
func main () {
println (Calculator.add (4, 6));
}
The .
is the attribute access operator, and it looks inside the Calculator
class (which is an object), and looks for an attribute called add
. When it finds it, it then invokes it with the arguments 4 and 6. Now let's go ahead and populate the rest of this class (I've taken out the newlines for readability):
class Calculator {
func add (x : int, y : int) : int { return x + y; }
func sub (x : int, y : int) : int { return x - y; }
func mul (x : int, y : int) : int { return x * y; }
func div (x : int, y : int) { return x / y; }
}
From main
or anywhere else that has access to the Calculator
class, these four methods can be called.
Creating an instance of a class is a basic tenant of Object Oriented Programming (OOP). When you create an instance (or instantiate) a class, you are creating a new object that has all the attributes of the parent object and is of type of the parent object. You can instantiate a class by invoking the class, returning back the new objectified class and executing the constructor, a method called before the new object is returned.
The constructor is a function called new
. Let's revisit the Calculator concept again, but this time create an object that takes in two numbers in the constructor and preforms operations on them. First we need to add our constructor that accepts two numbers:
class Calculator {
func new (x : int, y : int) {
this.x = x;
this.y = y;
}
}
You will instantiate this in the main method by doing the following:
func main () {
calc = new Calculator (4, 7);
}
The new
keyword is used to invoke the class, but is redundant an optional. 4 and 7 are passed in as arguments to the constructor, which assigns them to values in this
. this
is a self-reference, or a way to access the instance of a class. If I were to instantiate multiple copies of calc
, this
would mean something different in each one.
Now we can re-add in our methods from the calculator class, but that use the values provided by the constructor:
class Calculator {
func new (x : int, y : int) {
this.x = x;
this.y = y;
}
func add () : int { return this.x + this.y; }
func sub () : int { return this.x - this.y; }
func mul () : int { return this.x * this.y; }
func div () { return this.x / this.y; }
}
Now we can start calling these methods in an object oriented manor:
func main () {
calc1 = new Calculator (4, 5);
calc2 = new Calculator (7, 15);
println (calc1.add ()); # prints 9
println (calc1.sub ()); # prints -1
println (calc2.mul ()); # prints 105
println (calc2.div ()); # prints 0.46666
}