Difference between revisions of "Class"

From Lazarus wiki
Jump to navigationJump to search
m (Reverted edits by Reverse22 (Talk); changed back to last version by Pernecker)
Line 40: Line 40:
 
end;
 
end;
 
</delphi>
 
</delphi>
 
[http://www.prlog.org/11289974-phone-number-lookup-verizon-phone-number-reverse-lookup-to-get-information-you-need-quickly.html reverse lookup]
 
 
[http://thetvtopc.com/Reverse_Cell_Phone_Lookup_Number reverse phone lookup cell]
 

Revision as of 18:46, 2 January 2012

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. Additionally, any of your class's constructors must specify inherited in their body. A class can have several constructors, but only one destructor.

<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> but if you declare the variable as a class whitout initialisation of the object and you want create the object dynamicly you need to do : <delphi> var

   classInstance: TMyClass;

implementation begin

   classInstance := TMyClass.Create;

end; </delphi>


<delphi> constructor TMyClass.Create; begin

 inherited;
 SomeVar := 6;

end; </delphi>