begin
inherited Create(Owner); // Initialize inherited parts
Width := 65; // Change inherited properties
Height := 65;
FPen := TPen.Create; // Initialize new fields
FPen.OnChange := PenChanged;
FBrush := TBrush.Create;
FBrush.OnChange := BrushChanged;
end;
The first action of a constructor is usually to call an inherited constructor to initialize the object's inherited fields. The
constructor then initializes the fields introduced in the descendant class. Because a constructor always clears the
storage it allocates for a new object, all fields start with a value of zero (ordinal types), nil (pointer and class types),
empty (string types), or Unassigned (variants). Hence there is no need to initialize fields in a constructor's
implementation except to nonzero or nonempty values.
When invoked through a class-type identifier, a constructor declared as virtual is equivalent to a static constructor.
When combined with class-reference types, however, virtual constructors allow polymorphic construction of
objectsthat is, construction of objects whose types aren't known at compile time. (See Class references.)
Note:
For more information on constructors, destructors, and memory management issues on the .NET platform,
please see the topic Memory Management Issues on the .NET Platform.
Dostları ilə paylaş: