Exception handling
Function overloading
Operator overloading
Non-class functions
Non-object variables
Properties
Runtime type information
[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.
Objects are always allocated dynamically, on the heap, so an object reference is really a pointer to the object. The programmer is responsible for creating objects and for freeing them at the appropriate time. To create an object, use a class reference to call a constructor, for example: Obj := TSomeClass.Create;
Most constructors are named Create, but that is a convention, not a requirement of Delphi. You will sometimes find constructors with other names, especially older classes that were written before Delphi had method overloading. For maximum compatibility with C++ Builder, which does not let you name constructors, you should stick with Create for all your overloaded constructors.
To get rid of the object when your program no longer needs it, call the Free method. To ensure that the object is properly freed, even if an exception is raised, use a try-finally exception handler. (See Chapter 1, Delphi Pascal, for more information about try-finally.) For example: Obj := TSomeOtherClass.Create;
try
Obj.DoSomethingThatMightRaiseAnException;
Obj.DoSomethingElse;
finally
Obj.Free;
end;
When freeing a global variable or field, always set the variable to nil when freeing the object so you are not left with a variable that contains an invalid pointer. You should take care to set the variable to nil before freeing the object. If the destructor, or a method called from the destructor, refers to that variable, you usually want the variable to be nil to avoid any potential problems. An easy way to do this is to call the FreeAndNil procedure (from the SysUtils unit): GlobalVar := TFruitWigglies.Create;
try
GlobalVar.EatEmUp;
finally
FreeAndNil(GlobalVar);
end;
Each object has a separate copy of all of its fields. A field cannot be shared among multiple objects. If you need to share a variable, declare the variable at the unit level or use indirection: many objects can hold separate pointers or object references that refer to common data.
Inheritance
A class can inherit from another class. The derived class inherits all the fields, methods, and properties of the base class. Delphi supports only single inheritance, so a class has one base class. That base class can have its own base class, and so on, so a class inherits the fields, properties, and methods of every ancestor class. A class can also implement any number of interfaces (which are covered later in this 上一页 [1] [2] [3] [4] [5] 下一页 [系统软件]InstallShield Express for delphi制作安装程序定… [系统软件]The GRETA Regular Expression Template Archive [系统软件]OLE with the internet explorer [系统软件]14.5.10.1 Object creation expressions [常用软件]InstallShield Express制作Delphi数据库安装程序 [常用软件]Firefox: What’s the next step? [VB.NET程序]VB.Net中文教程(8) 对象(Object)基本概念 [VB.NET程序]The UDPChat Source(VB.NET) [Delphi程序]为什么选择Delphi.Net ? [Delphi程序]《关于VisiBroker For Delphi的使用》(4)
|