Similar to a class, an interface can only be declared at the outermost layer of a program or unit (that is, the interface section, that is, globally visible) and cannot be declared in a process or function.
The Declaration format of the interface type is as follows:
Type interfaceName = interface (ancestorInterface) // keyword interface
[{GUID}] // globally unique identifier
MemberList // member list
End;
(AncestorInterface) and [{GUID}] are optional. The interface declaration and class are similar, but there are the following constraints:
1. memberList can only contain methods and attributes. Fields are not allowed in the interface.
2. Because the interface does not have a field, the read and write attributes must specify a method.
3. All interface members are public ). Visibility (private, protected, etc.) and storage (such as stored, default, nodefault) are not allowed. (However, an array attribute can contain the keyword default. Please refer to the description at the end)
4. The interface has no constructor or destructor. It cannot be instantiated, except for its methods implemented through classes.
5. methods cannot be declared as virtual, dynamic, abstract, or override. This is because the interface does not implement its own methods.
See an example of an interface declaration:
Type
IMalloc = interface (IInterface)
[{00000002-0000-0000-C000-000000000046}]
Function alloc (size: integer): pointer; stdcall;
Function realloc (P: pointer; Size: integer): pointer; stdcall;
Procedure free (P: pointer); stdcall;
Function getsize (P: pointer): integer; stdcall;
Function didalloc (P: pointer): integer; stdcall;
Procedure heapminimize; stdcall;
End;
In some interface declarations, the keywordThe interface is replaced by dispinterface.. The answer is that this (also specified by dispid, read only, write only) is related to a special platform and is not used in Linux programming.
Some other personal opinions are also helpful: interfaces are also used to solve the confusion caused by multi-inheritance. In Delphi, a derived class can inherit only one class, but can inherit multiple interfaces at the same time.
In Object Pascal, the reserved word class is used to define the class data type. The syntax format of the class type definition is as follows: type <type Name> = class [(<base type Name>)] <class member list> end; where, select the base type name to indicate the direct ancestor class of the class. The class type can specify an ancestor type, indicating that the type is inherited from the specified Ancestor Type. In Delphi, if the base class is not specified, the default parent class is the 'rob-ject class, that is, directly from. The YObject class derives a new class. The 'l' Object class is defined in the System unit. For example, the following defines a Student class: type student = class name: String [8]; number: Integer; sex :( male, fema.1e); age: Integer; end; note: different from other data types, the class type definition can only appear in the type definition section of the outermost scope of the Program Unit or Unit, it cannot be defined in the variable description part or in a process or function. Therefore, the scope of the class type is always global. 2. The field of the class shows that the type definition is similar to the record type definition, but the class type can have three types of members: fields, methods, and attributes. The field in the class type is also the data part of the class. Its definition syntax is the same as the definition syntax of the field in the record. The field type can be various data types, or even another class type. In the preceding example, only four fields of the student class are defined. 3. The method of a class is a process or function that executes a specified operation on an object. The operation scope of a method can only be data within an object or data that can be accessed by an object. The method declared in the class type is actually a forward definition, that is, only the prototype of the method is defined in the class definition, and the method is defined in the Implementation area of the program. When defining a method, you can directly use the fields defined in the class without referencing a qualifier. When calling a method, Object Pascal implicitly passes a parameter Self, which is a pointer to the Object instance of the output method, equivalent to the This pointer in C ++.
PS: Explain the array attribute, also from Delphi help, keyword array properties
First look at the example:
Property strings [index: integer]: String...; default;
In this way, we can use object [Index] instead of object. property [Index]; however, you must note that only one property such as this can exist, otherwise you will know what will happen.