Class: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
{{Class}} | {{Class}} | ||
A '''class''' is a highly structured data [[Type|type]] in Object [[Pascal]] dialects such as Delphi or the [[Mode ObjFPC|ObjFPC]] dialect. Classes are able to contain [[Variable]]s, [[Constructor]]s, [[Destructor]]s, [[Function|function]]s, [[Procedure]]s, and [[Property|properties]] using access scopes. | A '''class''' is a highly structured data [[Type|type]] in Object [[Pascal]] dialects such as Delphi or the [[Mode ObjFPC|ObjFPC]] dialect. Classes are able to contain [[Variable]]s, [[Constructor]]s, [[Destructor]]s, [[Function|function]]s, [[Procedure]]s, and [[Property|properties]] using access scopes. | ||
Line 46: | Line 46: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{Data types}} | {{Data types}} |
Revision as of 17:15, 18 July 2016
│
Deutsch (de) │
English (en) │
français (fr) │
русский (ru) │
A class is 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 run-time 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.
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: Integer;
procedure SetSomeVar(newvalue: Integer);
published // special type of public scope
property SomeVar: Integer read GetSomeVar write SetSomeVar default 0;
end;
var
classInstance: TMyClass.Create;
but if you declare the variable as a class without initialization of the object and you want create the object dynamically you need to do :
var
classInstance: TMyClass;
implementation
begin
classInstance := TMyClass.Create;
end;
constructor TMyClass.Create;
begin
inherited;
SomeVar := 6;
end;
simple data types |
|
---|---|
complex data types |