Difference between revisions of "Class"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
A highly structured data [[type]] in Object [[Pascal]] dialects such as Delphi or the [[ObjFPC]] dialect. Classes are able to contain [[variable]]s, [[constructor]]s, [[destructor]]s, [[function]]s, [[procedure]]s, and properties using access scopes. Another interesting thing about classes is that they free the programmer from the need for [[pointer]]s and [[reference]]s. They are automatically handled by the [[compiler]] at compile time. Classes are able to inherit and to be inherited by other classes. For runtime purposes, any class not specifying a parent class automatically inherits from TObject, as it has required components for all classes.
+
A highly structured data [[type]] in Object [[Pascal]] dialects such as Delphi or the [[ObjFPC]] dialect. Classes are able to contain [[variable]]s, [[constructor]]s, [[destructor]]s, [[function]]s, [[procedure]]s, and properties using access scopes. Another interesting thing about classes is that they free the programmer from the need for [[pointer]]s and [[reference]]s. They are automatically handled by the [[compiler]] at compile time. Classes are able to inherit and to be inherited by other classes. For runtime purposes, any class not specifying a parent class automatically inherits from TObject, as it has required components for all classes. Because of TObject's dependency, any [[subclass]]'s [[destructor]] must have the [[override]] directive.
  
 
<delphi>
 
<delphi>
Line 9: Line 9:
 
         constructor Create; overload;
 
         constructor Create; overload;
 
         constructor Create(Args: array of Integer); overload;
 
         constructor Create(Args: array of Integer); overload;
         destructor Destroy; virtual;
+
         destructor Destroy; override;
 
         function GetSomeVar;
 
         function GetSomeVar;
 
         procedure SetSomeVar(newvalue: Integer);
 
         procedure SetSomeVar(newvalue: Integer);

Revision as of 23:58, 30 March 2009

A highly structured data type in Object Pascal dialects such as Delphi or the ObjFPC dialect. Classes are able to contain variables, constructors, destructors, functions, procedures, and properties using access scopes. Another interesting thing about classes is that they free the programmer from the need for pointers and references. They are automatically handled by the compiler at compile time. Classes are able to inherit and to be inherited by other classes. For runtime purposes, any class not specifying a parent class automatically inherits from TObject, as it has required components for all classes. Because of TObject's dependency, any subclass's destructor must have the override directive.

<delphi> type

   TMyClass = class(TObject)
   private // self access only
       FSomeVar: Integer;
   public // access by anything
       constructor Create; overload;
       constructor Create(Args: array of Integer); overload;
       destructor Destroy; override;
       function GetSomeVar;
       procedure SetSomeVar(newvalue: Integer);
   published // special type of public scope
       property SomeVar: Integer read GetSomeVar write SetSomeVar default 0;
   end;

</delphi>

<delphi> var

   classInstance: TMyClass.Create;

</delphi>

<delphi> constructor TMyClass.Create; begin

 SomeVar := 6;

end; </delphi>