Classes

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



Delphi Prism is entirely based on classes. Similar to Java or C#, in Delphi Prism, you must create classes; no code, variables or constants exist anywhere else (see note below).

A class is a data structure that may contain data members (constants and variables, called fields), function members (methods, properties, events, indexers, and delegates) and nested types. Class types support inheritance, a mechanism through which a derived class can extend or specialize the class on which it is based.

Note: global variables are supported because Delphi Prism creates an implicit Global Class, provided that the project options allow globals.

Delphi Prism provides the ability to implement generic types (for 2.0 framework or later).

Sub Topics


Class types

Sealed Classes

Sealed prevents the class from being descended from. See final to only prevent selective methods of a class from being overriden. Syntax:

type
  MyClass = public sealed class
   ...
  end;

Abstract Classes

An abstract class is defined as having one or more abstract methods. Such classes cannot be instantiated and are provided as the base for descendant classes. An abstract method does not require an implementation and descendants will provide an override.

Note: descendant classes must provide an implementation for all the abstract methods, or they will be abstract themselves.

An alternative to an abstract class is to set the non-implemented methods as empty instead of abstract. This then allows the class to be used itself.

A further alternative is to consider using interfaces instead.

All classes that inherit or define abstract members become abstract, however the abstract modifier has to be used to avoid the warnings.

type
  MyClass = public abstract class
   ...
    method DoSomething; abstract;
  end;

Partial Classes

The definition of a class can be split over two or more files, each containing:

type
  MyClass = partial class
    ...
  end;

Each file defines a separate part of the class. Repeating the same element (e.g. methods with an identical signature) in several classes causes an error. If ancestors (classes or interfaces) are specified, the list does not need to match in all files.

Consider the following:

type
  Foo = partial class(MyBase, ISomething)
  end;
...
  Foo = partial class(ISomethingElse)
  end;

Foo will now descend from MyBase, and implement both interfaces. However the "public, sealed, abstract" keys must match in all files.

This facility is particularly useful for systems that generate (and regenerate) code automatically - it allows user methods to be separated from the code which the user does not need to see.

C# introduces this feature with 2.0 and its form designer will no longer mix "designer generated code" with the main code - the form class will be split into two files, one for user code and one for the generated code.

Other applications might split an overly complex class into several files. For example, it could be useful to keep an interface implementation separate from the rest of the code or even split a class into two to allow two developers to work on it simultaneously.

Static Classes

A class that only has static members. The class itself will be sealed/abstract and cannot be created.

type
  MyClass = static class
    class method Test;
  end;

Static classes are a safe and convenient way of declaring a class containing static methods that cannot be instantiated.

Note: since static classes can only contain static ("class") members, all members defined in a static class will automatically be static, whether they explicitly specify the class keyword or not.


See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox