Type Members
In Oxygene, Class or Record (and, to a more limited fashion, Interfaces) can contain various types of members that define the type, its data and its behavior.
Within the type declaration, which starts with the class
, record
or 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:
- Fields
- Methods and Iterators
- Properties
- Events
- Constants
- Constructors
- Finalizers
- Iterators
- Custom Operators
- Nested Types
You might also want to read up on:
- Invariants
- Mapped Members
- Explicit Interface Implementations
- Member Modifiers
- Member Visibility Levels
Instance Members
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;
Static Members
Members can optionally be marked as static, using either the static
Member Modifier or by being prefixed with the class
keyword.
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;
See Also
self
Expressions