![]()
[3]
Generic types (templates)
![]()
Built-in support for threads
![]()
![]()
Message passing
![]()
Built-in assembler
![]()
[4]
Inline functions
![]()
The following sections explain each of these language features in more detail.
Classes
A class declaration is a kind of type declaration. A class declaration describes the fields, methods, and properties of the class. You can declare a class in an interface or implementation section of a unit, but the methods--like any other function or procedure--are defined in the implementation section. You must implement a class''''s methods in the same unit as the class declaration.
A class declaration has one or more sections for different access levels (private, protected, public, published, or automated). Access levels are discussed later in this chapter. You can mix sections in any order and repeat sections with the same access level.
Within each section, you can have any number of fields, followed by method and property declarations. Method and property declarations can be mixed together, but all fields must precede all methods and properties within each section. Unlike Java and C , you cannot declare any types nested inside a class declaration.
A class has a single base class, from which it inherits all the fields, properties, and methods. If you do not list an explicit base class, Delphi uses TObject. A class can also implement any number of interfaces. Thus, Delphi''''s object model most closely resembles that of Java, where a class can extend a single class and implement many interfaces.
TIP:
The convention in Delphi is that type names begin with the letter T, as in TObject. It''''s just a convention, not a language rule. The IDE, on the other hand, always names form classes with an initial T.
A class reference is an expression that refers to a specific class. A class reference is not quite a first class object, as it is in Java or Smalltalk, but is used to create new objects, call class methods, and test or cast an object''''s type. A class reference is implemented as a pointer to a table of information about the class, especially the class''''s virtual method table (VMT). (See Chapter 3 for the complete details of what''''s inside a VMT.)
The most common use for a class reference is to create instances of that class by calling a constructor. You can also use a class reference to test the type of an object (with the is operator) or to cast an object to a particular type (with the as operator). Usually, the class reference is a class name, but it can also be a variable whose type is a metaclass, or a function or property that returns a class reference. Example 2-2 shows an example of a class declaration.
Example 2-2: Declaring a Class and Metaclass
type
TComplexClass = class of TComplex; // metaclass type
TComplex = class(TPersistent)
private
fReal, fImaginary: Double;
public
constructor Create(Re: Double = 0.0); overload;
constructor Create(Re, Im: Double); overload;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
function AsString: string;
published
property Real: Double read fReal write fReal;
property
end;
Objects
An object is a dynamic instance of a class. The dynamic instance contains values for all the fields declared in the class and all of its ancestor classes. An object also contains a hidden field that stores a reference to the object''''s class.
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




