var MyByte: Byte; Name: String; procedure DoSomething; function Whatever: Byte;Can be summed up to a single object. You specify an oject by using the "object"-directive:
type PMyObject = ^TObject; TMyObject = object MyByte: Byte; Name: String; procedure DoSomething; function Whatever: byte; end;Note that this does not declare the oject you will later use. It merily provides a type (think of it as a "template"). You can use this template to create objects from it. To do this use:
var MyObject: PMyObject; begin MyObject:= TMyObject.Create; //... MyObject.Free; end."MyObject" is what you can work with. You also need to tell Delphi to create the object (create is called constructor). This will reserve memory form it. When you are done using it, you should free this memory using MyObject.Free; (free is destructor).
What makes objects powerful is that they can inherit variables and methods (constructors, destructors, functions and procedures) from other objects. If you need an object that is just the same as MyObject but has an additional FirstName: String variable, you can use inheritance to achieve this:
type TMySecondObject = object(TMyObject) FirstName: String; end;There is no need to reprogram all stuff that you already specified in TMyObject.
The object concept is taken from the Turbo Pascal days. The components in Delphi are all classes. Classes are very similar to objects. The main difference is in the declaration. Classes are always Pointers, you do not need to declare this any more. PMyObject as a class would look like this:
type TMyClass = class MyByte: Byte; Name: String; procedure DoSomething; function Whatever: byte; end;Inheritance works the same way as with objects. There are different sections in a class: private, protected, public, and published. Look up the differences in the Delphi help.
Please e-mail me with any comments!
© 27.12.96 Sebastian
Boßung