8 of 15
Classes
A class groups data (fields) with the functions that use it (methods). private fields can only be used inside the class. Create an object by calling the class:
pleno
class Temperature {
private celsius: Float;
constructor(celsius: Float) {
this.celsius = celsius;
}
fn fahrenheit(): Float {
return this.celsius * 9.0 / 5.0 + 32.0;
}
}
print(Temperature(20.0).fahrenheit()); // 68RunA class can extend another and reuse its methods, and implements an interface that lists the methods it must have.
Your turn
Give Stack a pop() method that removes and returns the last item, and a size() method.