Re: Newbie question: Writing your own class
- From: "alanglloyd@xxxxxxx" <alanglloyd@xxxxxxx>
- Date: 16 Jan 2006 00:04:48 -0800
I'm not sure where you're coming from (what languages) but some basic
concepts of Delphi OO construction and use are ...
A class is a template for a actual instance of an object. The instance
is made by a class's Create constructor method which allocates memory
for that object. The instance's memory is freed by a Destroy destructor
method, which is not called externally. That is done by a Free method
(called externally) which checks for non-nullity of the instance before
calling Destroy.
Classes "descend" from other classes, and the single original
pre-historic ancestor of all classes is TObject (note that " = class;"
is the same as " = class(TObject)").
Classes have methods (procs & functions), properties and fields.
A single chunk of executable code is provided for all instances of an
object (a reference to the instance is provided by a hidden parameter
named "Self"). Properties have separate storage for every instance.
Properties are accessed either directly of via an "accessor method".
Field are variable internal to the object.
So your class should/could be ...
TTest = class(TObject)
private
FSomeDef : string;
FDefinition : string;
function GetDefinition : string; // accessor get method
procedure SetDefinition(AValue : string); // accessor set method
public
property Definition : string read GetDefinition write
SetDefinition; // access by accessor method
property SomeDef : string read FSomeDef write FSomeDef; // direct
access
end;
constructor TTest.Create;
begin
inherited Create; // not necessary for Tobjec descendants but for
every other descendant of TObject
end;
function TTest.GetDefinition : string;
begin
Result := FDefinition;
end;
procedure TTest.SetDefinition(AValue : string);
begin
if AValue <> FDefinition then
FDefinition := AValue;
end;
var
MyTest : TTest; // variable to hold an instance reference
procedure TForm1.Button1Click(Sender : TObject); // some initiating
event handler
begin
MyTest := TTest.Create;
MyTest.Definition := 'MyDefinition';
MyTest.SomeDef := 'Some Def';
end;
procedure TForm1.Button2Click(Sender : TObject); // some other
displaying event handler
begin
ShowMessage('Definition : ' + MyTest.Definition + #13 +
'SomeDef : ' + MyTest.SomeDef);
end;
procedure TForm1.FormClose(Sender : TObject);
begin
MyTest.Free; // free the instance you have created - a TObject (your
class' parent) has a Free method
end;
The form you are doing this test on is assumed to be Form1. "Get",
"Set", "F", and "T" are prefixes used by convention in Delphi.
You will see that the set & get methods or the "F"-prefixed field
variables are used to hide direct access to the property. The "private"
and "public" keywords are used by the compiler to limit access to the
fields and methods (hiding access to elements of the object are
paradigms of OO).
Enjoy <g>.
Alan Lloyd
.
- References:
- Newbie question: Writing your own class
- From: Morten Taarland
- Re: Newbie question: Writing your own class
- From: Rob Kennedy
- Newbie question: Writing your own class
- Prev by Date: Re: Array of controls?
- Next by Date: Identifying Sender: TObject
- Previous by thread: Re: Newbie question: Writing your own class
- Next by thread: Re: Newbie question: Writing your own class
- Index(es):
Relevant Pages
|