Class Statement

Declares the name of a class and defines its implementation.

Syntax

[ modifier ] Class class_name [ < type_list > ] [ Is base_class ] [ Does trait_name ]...
    [ Where generic_constraints ] ...
    [ statements ]
End [ Class ]

Parts

modifier
Optional
  • Abstract – The class must be extended by a class that is not Abstract.
  • Final – The class cannot be extended.
class_name
Necessary identifier that names the class.
type_list
Optional – One or more identifiers separated by commas and contained in angle brackets (< >).
base_class
Optional name of another class from which this class inherits methods and properties.
Does
Optional, repeatable – Specifies which traits a class implements.
trait_name
The name of a trait, a way to define behavior. See Does Clause (Traits) for more information.
Where
Optional, repeatable – Specifies constraints to place on type arguments. Requires a type_list. You can place one at the end of the first line. Or you can place one or more on separate lines before any statements.
generic_constraints
One or more constraints on a generic parameter separated by commas. See Where Clause (Generics) for more information.
statements
Optional statements that define the members of the class.
End
Completes the statement. You can also use End Class.

Instructions

A Class statement defines a data type known as a class. Classes are fundamental building blocks in object-oriented programming (OOP). See Objects and Classes for more information.

You can place a Class only within certain contexts. These contexts include Program and Library modules, other classes, and Object and Trait declarations. You cannot place a class within a procedure. See Declaration Contexts and Default Access Levels for more information.

Allowed members

Examples

Listing 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
    Begin
      Get
        Return my_name
    End Property
    Property Age As UInt16
    Begin
      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