RosettaCodeData/Task/Classes/TIScript/classes.ti

36 lines
1.0 KiB
Plaintext

class Car
{
//Constructor function.
function this(brand, weight, price = 0) {
this.brand = brand;
this.weight = weight || 1000; // Resort to default value (with 'or' notation).
this._price = price;
}
property price(v) // computable property, special kind of member function
{
get { return this._price; } // getter section
set { this._price = v; } // setter section
}
function toString() { // member function, method of a Car.
return String.printf("<%s>",this.brand);
}
}
class Truck : Car
{
function this(brand, size) {
super(brand, 2000); // Call of constructor of super class (Car here)
this.size = size; // Custom property for just this object.
}
}
var cars = [ // Some example car objects.
new Car("Mazda"),
new Truck("Volvo", 2, 30000)
];
for (var (i,car) in cars) // TIScript allows enumerate indexes and values
{
stdout.printf("#%d %s $%d %v %v, %v %v", i, car.brand, car.price, car.weight, car.size,
car instanceof Car, car instanceof Truck);
}