Mascara Try it Download Buy About Documentation Blog

Class

Syntax definition [Help]
class classname [ ! ] [ .< typeparameters > ]
       [ extends baseclass ]
       [ implements interface-list ] {
   [ classmembers ]
}
Example:
class Car {}
 
class Volvo extends Car {
   // properties
   var color = "red";
   var model : String;
 
   // constructor
   function Volvo(model : String) : super() {
       this.model = model;
   }
 
   // methods
   function drive() {}
}

Classes are instatiated with new:

var v = new Volvo("100");
If no extends clause is present, the class extends Object directly.

You can declare classes with type parameters, see parameterized types.

A class defintion may contain a constructor, properties, accessors, methods and static blocks (in no particular order). Members can have accessmodifiers.

A constructor is a method with the same name as the class, which is executed when the class is instantiated. See constructors. The constructor is optional; if no constructor is provided a default constructor (with no parameters) is generated.

The optional exclamation mark after the class name indicates that the type is non-nullable by default. Se nullability.

Classes can implement interfaces.