Defining New Classes (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Using the object model Index

Although there are many classes in the object hierarchy, you are likely to need to create additional classes if you are writing object-oriented programs. The classes you write must descend from TObject or one of its descendants.

The advantage of using classes comes from being able to create new classes as descendants of existing ones. Each descendent class inherits the fields and methods of its parent and ancestor classes. You can also declare methods in the new class that override inherited ones, introducing new, more specialized behavior.

The general syntax of a descendent class is as follows:

Type
  TClassName = Class (TParentClass)
    public
      {public fields}
      {public methods}
    protected
      {protected fields}
      {protected methods}
    private
      {private fields}
      {private methods}
end;

If no parent class name is specified, the class inherits directly from TObject. TObject defines only a handful of methods, including a basic constructor and destructor.

To define a class:

  1. In the IDE, start with a project open and choose File > New > Unit to create a new unit where you can define the new class.
  2. Add the uses clause and type section to the interface section.
  3. In the type section, write the class declaration. You need to declare all the member variables, properties, methods, and events.
TMyClass = class; {This implicitly descends from TObject}
public
.
.
.
private
.
.
.
published {If descended from TPersistent or below}
.
.
.

If you want the class to descend from a specific class, you need to indicate that class in the definition:

TMyClass = class(TParentClass); {This descends from TParentClass}For example: type TMyButton = class(TButton)
  property Size: Integer;
  procedure DoSomething;
end;
  1. Some editions of the IDE include a feature called class completion that simplifies the work of defining and implementing new classes by generating skeleton code for the class members you declare. If you have code completion, invoke it to finish the class declaration: place the cursor within a method definition in the interface section and press Ctrl+Shift+C (or right-click and select Complete Class at Cursor). Any unfinished property declarations are completed, and for any methods that require an implementation, empty methods are added to the implementation section.If you do not have class completion, you need to write the code yourself, completing property declarations and writing the methods.Given the example above, if you have class completion, read and write specifiers are added to your declaration, including any supporting fields or methods:
type TMyButton = class(TButton)
  property Size: Integer read FSize write SetSize;
  procedure DoSomething;
private
  FSize: Integer;
  procedure SetSize(const Value: Integer);

The following code is also added to the implementation section of the unit.

{ TMyButton }
procedure TMyButton.DoSomething;
begin
end;
procedure TMyButton.SetSize(const Value: Integer);
begin
FSize := Value;
end;
  1. Fill in the methods. For example, to make it so the button beeps when you call the DoSomething method, add the Beep between begin and end.
{ TMyButton }
procedure TMyButton.DoSomething;
begin
  Beep;
end;
procedure TMyButton.SetSize(const Value: Integer);
begin
  if fsize < > value then
  begin
    FSize := Value;
    DoSomething;
  end;
end;

Note that the button also beeps when you call SetSize to change the size of the button.

See Also