with date from std:dateclass Memberprivate var string id@publicvar firstnamevar lastnamevar birthdayendfunc Member(firstname, lastname, birthday, id)this->firstname = firstnamethis->lastname = lastnamethis->birthday = birthdaythis->id = idendendvar member1 = Member("John", "Doe", Date(1,1,1970), "25aca5a7-cbfa-47ed-aeb5-f96cb1eb46ee")//Create a new member instance
A class without a constructor can still be instantiated. In the following example the class Calculator has two fixed methods which can be accessed without the class needing to be instantiated.
class Calculatorfixed func add(a,b)put a + bendfixed func sub(a,b)put a - bendendvar calc = Calculator //assign calc to the class protocalc->add(2,2)
A class marked fixed can not be instantiated. All functions or structs declared in it, are fixed. It may contain a constructor (but it can't be accessed without using reflection). One can declare a non-fixed class inside a fixed class and vice-versa.
fixed class Calculatorclass Adderfunc add(a,b)put a + bendendfunc add(a,b)put Adder()->add(a,b)endfunc sub(a,b)put a - bendendCalculator->add(2,2)Calculator->Adder()->add(2,2) //This works too because we declared a non-fixed (instantiable) class inside the fixed class
with console fixed from std:ioclass outerClassclass innerClassfunc innerClass()out("Hello from inner class")endendendvar innerClass = outerClass->innerClass() //Outputs: Hello from inner class
In Hades, it is possible to declare a class in a function. This function can only be instantiated (provided it's an instantiable class) in said function but can be returned and therefore used outside the function scope.
func getPerson(fn,ln)class Person@publicvar firstnamevar lastnameendfunc Person(firstname,lastname)this->firstname = firstnamethis->lastname = lastnameendendput Person(fn,ln)end
with console fixed from std:ioclass Motherfunc talk()out("I am female")endendclass Fatherfunc talk()out("I am male")endend
class Daughter < MotherendDaughter()->talk() //Outputs: I am female
When functions overlap each other when inheriting from multiple members, the order of the members to inherit from dictates which function is taken.
class Child < Mother, FatherendChild()->talk() //Outputs: I am male/*Because the Father class was inherited after the Mother class,the talk function of Father overwrites the talk function in Mother*/
If you still want to make the code from the example above work, you need to call the method on Mother explicitly. The built-in super function allows you to do exactly that.
To override a function, use the
func! keyword. This overrides functions and functions groups with function guards.
with console fixed fromclass OverrideChild < Fatherfunc! talk()out("I am a child")endendOverrideChild()->talk() //Outputs: I am a child