Classes

Top  Previous  Next

Classes 

Classes all derive from the root TObject class and follow the classic Delphi syntax. They're reference types, see also Records.

Named constructors are supported, as well as class methods, meta-classes, virtual methods, properties and destructors. Properties can optionally be array properties, and can feature an explicit index.

Classes can implement Interfaces and they can be partial.

You can also declare class methods with "method" as in the Oxygene language, in addition to "procedure" and "function".

Additionally classes can be marked as "external", in which case they're meant to expose classes that are not implemented in the script, and unlike interfaces, then can defined fields.

 

hm_clip0026

 

Named constructors are supported, as well as class methods, meta-classes, virtual methods, properties and destructors. 

Properties can optionally be array properties, and can feature an explicit index. 

You can also declare class methods with method as in the Oxygene language, in addition to procedure and function. 

Classic constructor syntax is supported, but you can also specify a default constructor and use the new keyword to 

instantiate classes. 

Both lines of code below are equivalent: 

 

   obj  :=  TMyObject.Create(parameter);
   obj  :=  new  TMyObject(parameter);

 

Visibilities are private, protected, public  and published. Private  and protected  in Smart correspond to strict  private and strict  protected in Delphi. 

Classes can implement Interfaces. (See example in the Interfaces section.) 

 type

  TMyClass = class(TControl)

    private

      { private declarations here }

    protected

      { protected declarations here }

    public

      { public declarations here }

    published

      { published declarations here }

  end;

 

private member is invisible outside of the unit or program where its class is declared. In other words, 

a private method cannot be called from another module, and a private field or property cannot be read 

or written to from another module. By placing related class declarations in the same module, you can 

give the classes access to one another's private members without making those members more widely 

accessible. For a member to be visible only inside its class, it needs to be declared strict private.

 

protected member is visible anywhere in the module where its class is declared and from any 

descendent class, regardless of the module where the descendent class appears. A protected method 

can be called, and a protected field or property read or written to, from the definition of any method 

belonging to a class that descends from the one where the protected member is declared. Members 

that are intended for use only in the implementation of derived classes are usually protected.

 

public member is visible wherever its class can be referenced.

 

Strict Visibility Specifiers

In addition to private and protected visibility specifiers, the Delphi compiler supports additional visibility 

settings with greater access constraints. These settings are strict private and strict protected visibility. 

These settings can be used in Win32 applications.

 

Class members with strict private visibility are accessible only within the class in which they are declared. 

They are not visible to procedures or functions declared within the same unit. Class members with strict 

protected visibility are visible within the class in which they are declared, and within any descendent class, 

regardless of where it is declared. Furthermore, when instance members (those declared without the class 

or class var keywords) are declared strict private or strict protected, they are inaccessible outside of 

the instance of a class in which they appear. An instance of a class cannot access strict protected or 

strict protected instance members in other instances of the same class.

Delphi's traditional private visibility specifier maps to the CLR's assembly visibility. Delphi's protected 

visibility specifier maps to the CLR's assembly or family visibility.

 

Note: The word strict is treated as a directive within the context of a class declaration. Within a class 

declaration you cannot declare a member named 'strict', but it is acceptable for use outside of a class 

declaration.

 

Published Members

Published members have the same visibility as public members. The difference is that run-time type 

information (RTTI) is generated for published members. RTTI allows an application to query the fields 

and properties of an object dynamically and to locate its methods. RTTI is used to access the values of 

properties when saving and loading form files, to display properties in the Object Inspector, and to 

associate specific methods (called event handlers) with specific properties (called events).

 

Published properties are restricted to certain data types. Ordinal, string, class, interface, variant, and 

method-pointer types can be published. So can set types, provided the upper and lower bounds of 

the base type have ordinal values from 0 through 31. (In other words, the set must fit in a byte, word, 

or double word.) Any real type except Real48 can be published. Properties of an array type 

(as distinct from array properties, discussed below) cannot be published.

 

Some properties, although publishable, are not fully supported by the streaming system. These include 

properties of record types, array properties of all publishable types, and properties of enumerated types 

that include anonymous values. If you publish a property of this kind, the Object Inspector will not 

display it correctly, nor will the property's value be preserved when objects are streamed to disk.

All methods are publishable, but a class cannot publish two or more overloaded methods with the 

same name. Fields can be published only if they are of a class or interface type.

 

 

See Partial Classes

 

See External Classes