HelpNDoc's scripting language supports a subset of Pascal's object-oriented programming features. This guide provides an overview of the object type system in HelpNDoc, with concise examples on how to define classes, create objects, and access their properties and methods.
Defining Classes and Objects
In HelpNDoc's scripting language, you define objects using classes. A class is a blueprint that defines the properties (data) and methods (functions) that the objects created from the class will have.
Syntax
type
TClassName = class
private
// Private members
public
// Public members
end;
Example
type
TPerson = class
private
FName: String;
FAge: Integer;
public
function GetName: String;
procedure SetName(AName: String);
function GetAge: Integer;
procedure SetAge(AAge: Integer);
end;
function TPerson.GetName: String;
begin
Result := FName;
end;
procedure TPerson.SetName(AName: String);
begin
FName := AName;
end;
function TPerson.GetAge: Integer;
begin
Result := FAge;
end;
procedure TPerson.SetAge(AAge: Integer);
begin
FAge := AAge;
end;
In this example, TPerson is a class with private fields FName and FAge, and public methods to set and get these fields.
Creating and Initializing Objects
To create an object (instance) of a class, use the Create method. Always remember to free the object after use to avoid memory leaks.
Example
var
Person: TPerson;
begin
Person := TPerson.Create;
try
Person.SetName('Alice');
Person.SetAge(30);
// Use the object
finally
Person.Free;
end;
end;
This code creates a TPerson object, sets its name and age, and then frees the object.
Accessing Methods and Properties
You can define properties in a class to provide a more intuitive way of accessing and modifying private fields.
Updated Class with Properties
type
TPerson = class
private
FName: String;
FAge: Integer;
public
property Name: String read FName write FName;
property Age: Integer read FAge write FAge;
end;
Example Using Properties
var
Person: TPerson;
begin
Person := TPerson.Create;
try
Person.Name := 'Bob';
Person.Age := 25;
finally
Person.Free;
end;
end;
Properties Name and Age allow for simplified access to the private fields.
Basic Inheritance
Inheritance allows you to create a new class based on an existing one, inheriting its properties and methods. The new class can also add new features or override existing ones.
Example
type
TEmployee = class(TPerson)
private
FPosition: String;
public
property Position: String read FPosition write FPosition;
end;
In this example, TEmployee inherits from TPerson and adds a Position property.