19th Chapter-delphi Custom Parts development (i) (4)

Source: Internet
Author: User
Tags integer

Since a user cannot change the value of a property in the public section at design time, the Class property cannot appear in the Object Inspector window.

⑷ defines design-time interfaces

A part of an object is declared as published, and that part is public and produces Run-time type information. However, only the properties defined by the published section can be displayed in the Object Inspector window. The published part of the object defines the design-time interface of the object. The design-time interface contains all the features that the user wants to customize at design time.

The following is an example of a published property because it is published and can therefore appear in the Object Inspector window:

Tsamplecomponent = Class (Tcomponent)

Private

Ftemperature:integer; {Concrete implementation is private}

Published

Property Temperature:integer read Ftemperature write ftemperature; {writable}

End

3. Dispatch method

The concept of dispatch (Dispatch) is used to describe how your application determines what code to execute when calling a method, and when you write the code that invokes the object, it looks no different than any other process or function call, but the object has three different ways of dispatching.

The types of these three delivery methods are:

Static of

Virtual the

Dynamic of

Virtual methods work the same way as dynamic methods, but they are implemented differently. Both are quite different from static methods. Understanding the various delivery methods is useful for creating parts.

⑴ static method:

If there is no special declaration, all object methods are static ... Static methods work as normal procedures and function calls. At compile time, the compiler determines the method address and joins it with the method.

The basic advantage of static methods is that delivery is fairly fast. Because the compiler determines the temporary address of the method and is directly associated with the method. Virtual and dynamic methods, in contrast, find the address of a method at run time using an indirect method, which takes a long time.

Another difference between static methods is that when you inherit from another type, you do not make any changes, this means that if you declare an object that contains a static method and then inherit the new object from that object, the descendant object has the same method address as the ancestor object, so the static method does the same work regardless of the actual object.

You cannot override a static method, and a static method that declares the same name in a descendant object replaces the Ancestor object method.

In the following code, the first part declares two static methods, the second part, and the method that declares the same name in place of the first part.

Type

Tfirstcomponent = Class (Tcomponent)

Procedure move;

Procedure Flash;

End

Tsecondcomponent = Class (Tfirstcomponent)

Procedure move; {Despite the same declaration, it differs from the inherited method}

function Flash (Howoften:integer): Integer; {Same as Move method}

End

⑵ virtual method

Calling a virtual method is the same as calling any other method, but the dispatch mechanism is different. Virtual methods support redefining methods in descendant objects, but the calling method is exactly the same, the address of the virtual method is not determined at compile time, but the address of the method is found at run time.

To declare a new method, add the virtual instruction after the method declaration. The virtual instruction in the method declaration creates a portal in the object virtual method table (VMT) that holds all the addresses of the object class that have virtual methods.

When you get a new object from an existing object, the new object gets its own vmt, it contains the VMT portal of all the ancestor objects, and then adds the virtual method declared in the new object. Descendant objects can overwrite any inherited virtual methods.

Overriding a method is to extend it, not replace it. Descendant objects can redefine and implement any method declared in an ancestor object. However, you cannot overwrite a static method. Overrides a method to increment the override instruction at the end of the method declaration, where the use of override will result in a compilation error:

The method does not exist in the ancestor object

The same method is static in the Ancestor object

The declaration does not match the ancestor object (such as name, parameter)

The following code shows two simple parts. The first part declares three methods, each using a different dispatch method, the second part inherits the first part, replaces the static method, and overrides the virtual method and the dynamic method.

Type

Tfirstcomponent = Class (Tcustomcontrol)

Procedure move; {static Method}

Procedure Flash; Virtual {Virtual method}

Procedure Beep; Dynamic {Dynamic virtual method}

End

Tsecondcomponent = Class (Tfirstcomponent)

Procedure move; {A new method is declared}

Procedure Flash; Override {Overriding inherited methods}

Procedure Beep; Override {Overriding inherited methods}

End

⑶ Dynamic method

Dynamic methods are a slightly different dispatch mechanism from virtual methods. Because dynamic methods do not have an object-VMT entry, they reduce the amount of memory consumed by objects. Dispatch dynamic method is slower than delivery general virtual method. Therefore, if the method call is frequent, you may want to define it as a virtual method.

When you define a dynamic method, you add the dynamics instruction after the method declaration.

Unlike the object virtual method creation portal, the dynamic method assigns a number to the and store the address of the corresponding code, the dynamic method list contains only the newly added and overridden method portals, and the delivery of the inherited dynamic method is by looking up a list of dynamic methods for each ancestor (in reverse order with inheritance), Therefore, dynamic methods are used to process messages, including Windows messages. In fact, the message processing process is dispatched in the same way as the dynamic method, except that the method is defined differently

⑷ Objects and pointers

In Object Pascal, objects are actually pointers. The compiler automatically creates object pointers for the program, so in most cases you don't have to consider objects as pointers. But this is important when you pass the object as a parameter. Typically, a passing object is by value, not by reference, that is, when an object is declared as a parameter of a procedure, you cannot use the var argument because the object is already a pointer reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.