As of now, I'm still working on classes. Here's what the current code looks like:
local class Test {
// We can define private and public variables like so:
private a, b, c = 1, 2, 3
public d, e, f = 4, 5, 6
// There are also public static and public shared variables.
// Static variables are variables that cannot be changed (I know in other languages static means something different, but I feel as though it makes more sense for it to be this way).
public static g, h, i = 7, 8, 9
// Shared variables are variables that if one object changes it, all other objects have the same changed value. Therefore, the variable is 'shared' between objects.
public shared j, k, l = 10, 11, 12
// We can also define variables like this. It's a preference thing, really.
private
one = 1
two = 2
three = 3
public
four = 4
five = 5
six = 6
// Common function declaration
private function ABC() {
// When we refer to private variables, we don't use 'self.' Instead, we write them as they are.
print(a, b, c)
}
public function DEF() {
// When we refer to public variables, we do use 'self.' as to refer to the individual object.
print(self.d, self.e, self.f)
}
}
This is some example code for classes once they are fully complete:
local class Animal {
public name = "Unknown"
public species = "Unknown"
public sound = "Silence"
private age = 0
private shared totalAnimals = 0
public static kingdom = "Animalia"
public static function makeSound() {
print(self.sound)
}
public static function getOlder(amount: number) {
age += amount
}
public static function getAge() {
return age
}
public static function getTotal() {
return totalAnimals
}
private function __init() { // Ran every time a new object is created.
totalAnimals++ // All objects from this class now have this updated variable.
}
}
local Dog = new Animal(species: "Dog", sound: "Bark")
Dog.name = "Stray Dog"
Dog.makeSound() // Prints "Bark"
// We can put the type of the object in () during a class declaration.
local class Cat (feline) extends Animal {
public static override species = "Cat"
public static override sound = "Meow"
private function __init() {
super.makeSound() // super refers to the class it extends
}
}
local cat1 = new Cat() // Prints "Meow"
if type(cat1) == "feline" { // True, as we assigned the object's type as 'feline'
print("I think it's a cat.")
} else {
print("Dunno what it is.")
}
print("Number of animals: " ..cat1.getTotal()) // prints "Number of animals: 2"
Also, as a side note, I added a new operator. It's for strings, really.
local str = "test"
str.="ing"
print(str) // Prints "testing"
Edited by Detective_Smith, 30 December 2015 - 09:10 PM.