Class
See Also: DataFlex Classes, Composite, End_Class, End_Object, Forward, Function, Object, Procedure, Property
Purpose
To create a new DataFlex object class. A class defines a DataFlex object's behavior.
Syntax
Class {sub-class} Is A {super-class}
:
End_Class
Where:
- {sub-class} is the name of the new class to create; and {super-class} is the name of the class that the new class is based upon.
- {sub-class} may be between 1 and 4096 characters in length, must start with a letter, and may not contain spaces. Recommended characters are 0-9, a-z, A-Z, and _ (underscore).
What It Does
The class command creates a new class {sub-class} with all the properties, function, and procedure methods of its parent class, {super-class}. These inherited characteristics are then modified in the succeeding commands (until the End_Class command) to produce the new class.
{sub-class} may be any unique class name. Objects of the new class are created using the Object command. The {sub-class} name should be descriptive of its use in the program.
{super-class} is the class from which the new class inherits. The new class begins with all of the procedures, functions, properties, and key definitions of the parent class. The job of the Class command is to add, change, or cancel the abilities of {super-class} to achieve the goals of the new {sub-class}.
{super-class} must be defined in the program before it is used as a parameter to the Class command.
Class definitions are commonly placed in package files, so that the code can be reused; however, this is not a requirement. Definitions of classes of broad applicability are best placed in package files so that many programs may use them.
Within the class definition, there are declarations of procedures and functions. These methods define an object's behavior. An object can also store its state in properties. Property declarations are made in the Construct_Object message, the message sent by the system when an object is created. Class definitions are terminated by the End_Class command.
See Composite to learn about Composite classes.
Example
The following example demonstrates sub-classing and inheritance:
Class cBasicButton Is A Button
Procedure Proc_1
Showln "Overridden: This line never prints."
End_Procedure
Procedure Proc_2
Showln "Forwarded: Inside Proc 2 of cSuperClass"
End_Procedure
Procedure Proc_3
Showln "Inside Proc 3 of cSuperClass"
End_Procedure
End_Class
Class cMyButton Is A cBasicButton
Procedure Proc_1 // Override Proc 1
Showln "Override. Inside Proc 1 of cSubClass"
End_Procedure
Procedure Proc_2 // Forwarding example.
Showln "Before forwarding. Inside Proc 2 of cSubClass"
Forward Send Proc_2
Showln "After forwarding. Inside Proc 2 of cSubClass"
End_Procedure
Procedure Proc_4 // Procedure only declared in cSubClass.
Showln "Inside Proc 4 of cSubClass"
End_Procedure
End_Class
Object oTest Is A cMyButton
Procedure OnClick
Showln "This program demonstrates inheritance"
Send Proc_1
Send Proc_2
Send Proc_3
Send Proc_4
End_Procedure
End_Object
The following example demonstrates declaring a new class with new properties:
Class cOK_Button Is A Button
// Constructor:
Procedure Construct_Object
Forward Send Construct_Object // very important!
Property Integer piClickCount 0
Property String psBusyLabel "-Busy-"
End_Procedure
End_Class
This example declares a class, cOK_Button, with two new properties, piClickCount and psBusyLabel. Note that the properties are declared inside the class constructor method, Construct_Object. An important part of the Construct_Object method is the "Forward Send Construct_Object" statement. This is the line that ensures that all of the definitions in the superclass's constructor are inherited by the new class.
Notes
- New properties must be declared in the class constructor method
Construct_Object. Refer to the DataFlex Language Guide for more information on declaring properties. - Important! Class declarations may not be nested.