共用方式為


Classes and Objects: The Building Blocks of Applications

Classes and objects are closely related, but they are not the same. A class contains information about how an object should look and behave. A class is the blueprint or schematic of an object. The electrical schematic and design layout of a telephone, for example, would approximate a class. The object, or an instance of the class, would be a telephone.

Objects Have Properties

An object has certain properties, or attributes. For example, a phone is a certain color and size. When you put a phone in your office, it has a certain position on your desk. The receiver can be on or off the hook.

Objects you create in Visual FoxPro also have properties that are determined by the class the object is based on. These properties can be set at design time or at run time.

For example, some of the properties that a check box can have are listed in the following table:

Property Description
Caption The descriptive text beside the check box.
Enabled Specifies whether a user can choose the check box.
ForeColor The color of the caption text.
Left The position of the left side of the check box.
MousePointer How the mouse pointer looks when over the check box.
Top The position of the top of the check box.
Visible Specifies whether the check box is visible.

Objects Have Associated Events and Methods

Each object recognizes and can respond to certain actions called events. An event is a specific and predetermined activity, initiated by either a user or the system. Events, in most cases, are generated by user interaction. For example, with a phone, an event is triggered when a user takes the receiver off the hook. Events are also triggered when the user presses the buttons to make a call.

In Visual FoxPro, user actions that trigger events include clicks, mouse moves, and key presses. Initializing an object and encountering a line of code that causes an error are system-initiated events.

Methods are procedures that are associated with an object. Methods are different from normal Visual FoxPro procedures: methods are inextricably bound with an object and are called differently than normal Visual FoxPro procedures are called.

Events can have methods associated with them. For example, if you write method code for the Click event, that code is executed when the Click event occurs. Methods can also exist independently of any events. These methods must be explicitly called in code.

The event set, while extensive, is fixed. You can't create new events. The method set, however, is infinitely extendible.

The following table lists some of the events associated with a check box:

Event Description
Click User clicks the check box.
GotFocus User selects the check box by clicking it or tabbing to it.
LostFocus User selects another control.

The following table lists some of the methods associated with a check box:

Method Description
Refresh The value of the check box is updated to reflect any changes that may have occurred to the underlying data source.
SetFocus The focus is set to the check box just as though the user had pressed the TAB key until the check box was selected.

See Also

Object-Oriented Programming | Classes in Visual FoxPro | Preparation for Class Creation | Creating Classes | Modifying a Class Definition | Subclassing a Class Definition | Operating the Class Designer | Class Member Protection and Hiding | Specifying Design-Time Appearance | Creating, Copying, and Removing Class Library Files | Adding Classes to Forms | Default Property Setting Override | Container Hierarchy Object Referencing | Setting Properties | Calling Methods | Event Response