The Fore programming language is object-oriented. Classes are used to describe objects, store data and implement work algorithms. A class definition includes constants and fields that store object data and class members that use this data: constructors, properties, methods (procedures/functions) and events.
Each class member and the class itself can have an access modifier that determines whether it can be used in other code blocks.
Public Class TUserInfo: Object
//Fields to store values inside class object
_FirstName, _LastName: String;
//Object constructor
Public Constructor Create(Name: String = "");
Begin
_FirstName := Name;
End Constructor Create;
//Object properties
Public Property FirstName: String
Get
Begin
Return _FirstName
End Get
Set
Begin
_FirstName := Value
End Set
End Property FirstName;
Public Property LastName: String
Get
Begin
Return _LastName
End Get
Set
Begin
_LastName := Value
End Set
End Property LastName;
//Methods that are available only inside class
Sub Serialize;
Begin
//...
End Sub Serialize;
Sub Deserialize;
Begin
//...
End Sub Deserialize;
End Class TUserInfo;
To determine custom classes, inherit them from a class. As a parent class, the Object basic class can be used or the already defined custom class. By default, a class has the Private access modifier. The class can implement a number of interfaces. This means that a class must contain implementation of all the members described in interfaces.
The class prior description is absent. Any class can be used if it is described in this unit or in any of the imported units. In this case any class parent must be determined before it is inherited by the descendant classes.
All classes have the Create standard constructor. Use the New statement to create a new object and call the Create method. The constructor is used to initialize a new object instance. The method can be called for the existing object, in this case only constructor code will be executed (no memory will be used and no object fields will be initialized). All objects will be deleted automatically if there are no object links.
A constructor body may contain the Inherited call, which will call the parent class constructor.
Public Constructor CreateNew;
Begin
Inherited Create;
//...
End Constructor CreateNew;
To get a link to the object, for which this method is called, within the class method use the Self identifier. The predefined Self identifier is relevant only for class methods, attempts to use it elsewhere in the program result in a compilation error.
The Shared directive specifies that this method or field are a class method or a class field respectively (static class members). Static methods and static fields are not a part of a particular class instance. Regardless of the number of created class instances, there is only one copy of the static field. Static fields start existing after they are referred to for the first time and stop existing after the application domain is shut down.
NOTE. Values of static fields are unique within different connections to the same repository.
Non-static members can use any static or non-static class members. Static members can use only static class members. The Self identifier can be used to address static members within a class or it is possible to simply indicate the static member name.
The Final directive specifies that this method cannot be redefined in the children of this class.
Only delegates can be used as an event type. Delegates define what kind of signature a method shall have to be the specified event handler. An assignation operation is only available for the events with a method name in its right part. The method signature must match the delegate signature. The assignation operation is used to assign a new event handler.
Class SampleClass: Object
Private Shared Temp: Integer;
Public Shared Sub Test;
Begin
Temp := TestEx(1);
//...
End Sub Test;
Public Shared Function TestEx(Index: Integer): Integer;
Var
i: Integer;
Begin
//...
Return i;
End Function TestEx;
Public Sub CreateSettings(Count: Integer);
Begin
//...
If Count < 1 Then
//Call method via Self
Self.Test;
Else
//Call method without using Self
Temp := TestEx(Count);
End If;
End Sub CreateSettings;
Public Final Property GetValue: Integer
Get
Begin
//...
Temp := Correct(Temp);
//...
Return Temp;
End Get
End Property GetValue;
Private Function Correct(Value: Integer): Integer;
Var
i: Integer;
Begin
//...
Return i;
End Function Correct;
End Class SampleClass;
See also:
Fore Language Guide | Creating Objects | Class Properties | Delegates and Events | Interfaces