Mega Code Archive

 
Categories / Delphi / Compiler Directives
 

Published - starts a published externally accessible section of a class

type Class declaration Published Field | Property | Method declaration {...} end; Description The Published directive starts a declaration section of a class definition. In a published section, Fields, Properties and Methods are declared to be accessible to this class, classes descending from it, and code using object instances of the class. And information about these accesses are recorded in the run time information for the class. In Object Oriented terms, a class object is seen as a black box. The internal operations are not relevant. Public and published fields, properties and methods are the externally visible part of an object - a controlled access to the internals of the class. Because public and published access provides a linkage of sorts to external code, you should avoid wherever possible making changes to these sections. Notes Warning : avoid making fields published - it is always better to define a property to access them instead. This provides some decoupling from the internals of the class. Only one Constructor may be declared as published - overloaded versions must be defined as public. Published properties cannot return arrays. Related commands Function Defines a subroutine that returns a value Private Starts the section of private data and methods in a class Procedure Defines a subroutine that does not return a value Property Defines controlled access to class fields Protected Starts a section of class private data accesible to sub-classes Public Starts an externally accessible section of a class Type Defines a new category of variable or process Example code : A simple example // Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses Forms, Dialogs, Classes, Controls, StdCtrls, SysUtils; type // Define a base TSquare class : TSquare = class private // Only known internally squareArea, squareWidth, squareHeight : Integer; Published // Known externally : has run time info also property width : Integer read squareWidth; property height : Integer read squareHeight; property area : Integer read squareArea; constructor Create(width, height : Integer); end; // Define the form class used by this unit TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions // Create the TSquare object constructor TSquare.Create(width, height: Integer); begin // Save the width and height squareWidth := width; squareHeight := height; // And calculate and save the square area squareArea := width * height; end; // Main line code procedure TForm1.FormCreate(Sender: TObject); var mySquare : TSquare; begin // Create a square object mySquare := TSquare.Create(30, 40); // What are the square attributes ? : ShowMessageFmt('Square area = %d * %d = %d', [mySquare.width, mySquare.height, mySquare.area]); end; end. Square area = 30 * 40 = 1200