Within the type declaration, which starts with the
interface keyword and ends with
end, individual members can be declared, separated by semicolons. Optionally, different Visibility Sections can be used to define which parts of the project have access to each member.
Bodies of members that contain code (such as Methods or Properties) can be provided in-line, directly following their declaration, or they can – in more traditional Pascal style – be provided below the type declaration in a separate
implementation section. Please refer to the Code File Structure topic for more details on this.
type MyClass = public class private var fName: String; public property Name: String read fName; method DoSomething; begin DoSomeWork; end; end;
A type can define the following kinds of members:
- Methods and Iterators
- Custom Operators
You might also want to read up on:
- Mapped Members
- Explicit Interface Implementations
- Member Modifiers
- Member Visibility Levels
By default, members of a class are specific to the instance. This means if they provide storage (such as Fields, Properties or Events), that storage is separate for each instance of the class, and if they access storage (such as a Method that reads or writes the value of a field or property) they access the storage of that instance.
Code for instance members has access to the
self Expression which gives access to the current object instance that code is running on. All access to the class's storage goes through this
self reference (be it explicit, or implied).
type MyClass = public class private var fName: String; // each instance of the class has its own copy of fName. public method DoSomething; begin fName := "Hello"; end; end;
Members can optionally be marked as static, using either the
static Member Modifier or by being prefixed with the
For members that provide storage (Fields, Properties or Events), only one copy isn provided for the class itself. Any code that accesses or changes the data, be it in a static or an instance method, will see and change the same value.
Code for static members runs without the context of a specific instance. As such it has access to statically stored data, but cannot access any instance members. Static members can still use the
self Expression, but it will refer to the class, not to an instance.
type MyClass = public class private var fName: String; // each instance of the class has its own copy of fName. var fName2: String; static; // there's only one copy of fName2 public method DoSomething2; static; begin fName2 := "Hello"; // we can't access fName here! end; end;