Class Statement

Declares the name of a class and gives its implementation.

Syntax

[ modifier ] Class class_name [ type_list ] [ Is base_class ] [ Does trait_name ]…
    [ Where generic_constraints ]…
    [ statements ]
End [ Class ]

Parts

modifier
Optional one of these:
  • @Abstract – The class must be extended by a class that is not abstract.
  • @Open – The class can be extended.
class_name
Mandatory name for the class.
type_list
Optional one or more names with commas between each, all between brackets ([ ]). See Type List for more information.
base_class
Optional name of a class from which this class inherits methods and properties.
Does
Optional keyword that you can use again and again.
trait_name
The name of a trait, a construct that specifies behavior. See Does Clause (Traits) for more information.
Where
Optional, one or more times – Specifies constraints to put on type arguments from type_list. You can put one at the end of the first line, or you can have one or more, each on different lines, before statements.
generic_constraints
One or more constraints on a generic parameter, with a comma between each. See Where Clause (Generics) for more information.
statements
Optional statements that declare the members of the class.
End
Completes the statement. You can also use End Class.
You can change the syntax of this part. See @Option Directive for more information.

Instructions

The statement Class makes a data type known as a class. Classes are important constructs in object-oriented programming (OOP). See Objects and Classes for more information.

You can put a class only in some contexts. These contexts include modules (Program and Library), other classes, and the constructs Object and Trait. You cannot put a class in a procedure. See Declaration Contexts and Default Access Levels for more information.

Permitted members

Examples

Example 1

Program ClassExample
Require IO

Class Person
    Var my_name As String, my_age As UInt16
    Constructor name As String, age As UInt16
        my_name = name
        my_age = age
    End Constructor
    Property Name As String
    Get
        Return my_name
    End Property
    Property Age As UInt16
    Get
        Return my_age
    End Property
End Class

Person #a, "Alice", 24
Person #b, "Bob", 42

PrintLine #a.Name & " is " & #a.Age
PrintLine #b.Name & " is " & #b.Age

' Output:
' Alice is 24
' Bob is 42

See also