Difference between revisions of "CLASS"

From Recital Documentation Wiki
Jump to: navigation, search
(Example)
 
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=CLASS=
 
 
 
==Class==
 
Objects
 
 
 
 
==Purpose==
 
==Purpose==
 
Create a user-defined class
 
Create a user-defined class
Line 11: Line 4:
  
 
==Syntax==
 
==Syntax==
CLASS <class name>
+
CLASS <class name> [OF | AS | EXTENDS <base class> [, ...]]
 
+
[OF <base class> [, ]]
+
 
+
[PUBLIC | PRIVATE]
+
 
+
[DYNAMIC]
+
 
+
[NOTIFY]
+
 
+
[PROPAGATE]
+
  
 
ENDCLASS
 
ENDCLASS
Line 27: Line 10:
  
 
==See Also==
 
==See Also==
[[ADDPROPERTY()]], [[CLASS - Methods]], [[CLASS - Parameters]], [[CLASS - Properties]], [[CLASS - Scoping]], [[DEFINE CLASS]], [[CREATEOBJECT()]], [[DODEFAULT()]], [[METHOD]], [[NEWOBJECT()]], [[REMOVEPROPERTY()]]
+
[[ACLASS()]], [[ADDPROPERTY()]], [[AMEMBERS()]], [[CLASS - Methods]], [[CLASS - Parameters]], [[CLASS - Properties]], [[CLASS - Scoping]], [[COMPOBJ()]], [[CREATEOBJECT()]], [[DEFINE CLASS]], [[DISPLAY CLASSES]], [[DODEFAULT()]], [[FOREACH]], [[LIST CLASSES]], [[LOADOBJECT()]], [[METHOD]], [[NEWOBJECT()]], [[OBJECT()]], [[PRINT_HTML()]], [[PRINT_JSON()]], [[PRINT_R()]], [[PRINT_XML()]], [[REMOVEPROPERTY()]], [[REQUIRE_ONCE()]], [[SAVEOBJECT()]], [[SQL SELECT]], [[WITH]]
  
  
 
==Description==
 
==Description==
The Class construct is a fundamental part of the object-oriented programming language (OOPS) which is part of the Recital/4GL.  The OOPS language supports encapsulation, inheritance (single and multiple), polymorphism, complex member scoping, property notification, and method propagation.
 
 
 
Fundamental to object-oriented programming is the concept of Objects and classes.  An object is a self-contained unit of data and functions that manipulate that data.  A class is a specification of an object.  A class contains memory variable specifications known as properties and functions that perform actions on the object known as methods.  An object is an instance of a class.
 
Fundamental to object-oriented programming is the concept of Objects and classes.  An object is a self-contained unit of data and functions that manipulate that data.  A class is a specification of an object.  A class contains memory variable specifications known as properties and functions that perform actions on the object known as methods.  An object is an instance of a class.
  
Line 42: Line 23:
 
myobject = new myclass()
 
myobject = new myclass()
  
The CLASS...ENDCLASS construct is used to create a user-defined class.  The beginning of a class is specified with CLASS <class name>, where <class name> can be any valid name up to 32 characters.  The ENDCLASS command is used to complete the class construct.  The CLASS...ENDCLASS construct is built using the commands describe in this section.  Any Recital/4GL command can be used in building methods in the class, however these commands cannot be used outside the method definition inside a CLASS...ENDCLASS construct.
+
The CLASS...ENDCLASS construct is used to create a user-defined class.  The beginning of a class is specified with CLASS <class name>, where <class name> can be any valid name up to 32 characters.  The ENDCLASS command is used to complete the class construct.  The CLASS...ENDCLASS construct is built using the commands describe in this section.  Any Recital command can be used in building methods in the class, however these commands cannot be used outside the method definition inside a CLASS...ENDCLASS construct.
  
====OF <base-name> [, ...] ====
+
====OF | AS | EXTENDS <base-name> [, ...]====
A key feature of the object-oriented code is reusability through a mechanism called inheritance, that is, one class can inherit the members and their implementation from another class.  Building new classes out of existing classes allows for the reusing of proven classes and the incorporation of system classes into user defined classes.  Inheritance enables developers to build a hierarchy of descending objects.  The inheriting class is called a derived class, and the class from which the derived class inherits is called a base class.  The OF clause is used to inherit the <base name>.  You can inherit multiple classes by specifying a class name comma separated list.
+
A key feature of the object-oriented code is reusability through a mechanism called inheritance, that is, one class can inherit the members and their implementation from another class.  Building new classes out of existing classes allows for the reusing of proven classes and the incorporation of system classes into user defined classes.  Inheritance enables developers to build a hierarchy of descending objects.  The inheriting class is called a derived class, and the class from which the derived class inherits is called a base class.  The OF | AS | EXTENDS clause is used to inherit the <base name>.  You can inherit multiple classes by specifying a class name comma-separated list.
 
+
====PUBLIC | PRIVATE====
+
A user-defined class by default is defined as PRIVATE, which means that it will be visible only at the level where the class is instantiated.  If the PUBLIC clause is specified, then when the class is instantiated, it will be visible at all levels until it is RELEASED from memory.
+
 
+
====DYNAMIC====
+
The DYNAMIC keyword provides the ability to add property names to objects at runtime.
+
 
+
====NOTIFY====
+
The Recital/4GL supports what is known as Property notification.  If you specify the NOTIFY clause all properties defined will have notify set.  Property notification is an essential element in the real-time interaction with system objects.  When a property that has been defined with the NOTIFY clause is read, and the class contains a method called GETPROPERTY, that method is called.  The property name is passed to the GETPROPERTY method as a parameter.
+
 
+
====PROPAGATE====
+
Any methods which are called, which have the PROPAGATE attribute set, cause a cascading execution effect down through the class hierarchy.  After the method is executed, a search is made in all of the sub-objects (if any) that are defined as properties within the current object.  If a method with the same name is found and that method has the NOTIFY attribute specified, then that method is called.
+
 
+
The CONSTRUCTOR and the DESTRUCTOR methods always act as if sub-objects have the PROPAGATE and the NOTIFY attributes set.  This allows any properties that have been specified as sub-objects to operate correctly when an object is created with the NEW operator.
+
 
+
All classes have an inbuilt ADDPROPERTY 'factory method'.  This can be used as an alternative to the ADDPROPERTY() function to add properties to an object at runtime.
+
  
  
 
==Example==
 
==Example==
 
<code lang="recital">
 
<code lang="recital">
class Null dynamic
+
class product
     property CHARACTER
+
     public name
     property NUMERIC
+
     public price
 +
    procedure init(cName, nPrice)    // constructor
 +
        name = cName
 +
        price = nPrice
 +
    endproc
 +
    procedure display()
 +
        echo "Product name is " + name + ", price is " + price + "\n"
 +
    endproc
 
endclass
 
endclass
  
oNULLDATA = new Null()
+
class car of product
oNULLDATA.CHARACTER = ""
+
    private color
oNULLDATA.NUMERIC = 0
+
endclass
oNULLDATA.DATE = {  /  /    }
+
oNULLDATA.LOGICAL = .f.
+
display memory
+
  
// Use of the Notify Keyword
+
class motorbike as product
class CharWin
+
     private size
    public notify:
+
    property lVISIBLE as logical
+
    property nROW, nCOL, nENDROW, nENDCOL
+
  public:
+
    property cWINDOW
+
    property cCOLOR
+
 
+
    method Constructor
+
        parameters cWINDOW, cCOLOR, nROW, nCOL, nENDROW, nENDCOL
+
        define window &cWINDOW ;
+
            from nROW, nCOL to nENDROW,nENDCOL ;
+
            color &cCOLOR panel float grow
+
        this.cWINDOW = cWINDOW
+
        this.cCOLOR  = cCOLOR
+
        this.nROW    = nROW
+
        this.nCOL    = nCOL
+
        this.nENDROW = nENDROW
+
        this.nENDCOL = nENDCOL
+
    return  && Constructor
+
 
+
    method SetProperty
+
        parameter cNAME
+
        if lower(cNAME) = [lvisible]
+
            if this.lVISIBLE
+
                activate window &(this.cWINDOW)
+
            else
+
                hide window &(this.cWINDOW)
+
            endif
+
        endif
+
     return  && SetProperty
+
 
endclass
 
endclass
  
clear screen
+
class vehicle extends car, motorbike
oMYWIN = new CharWin("mywin", "w/r",2,20,15,60)
+
    public manufacturer = "Ford"
dialog box "Calling a NOTIFY property"
+
     public yearDesigned
oMYWIN.lVISIBLE = .t.
+
     procedure init(cName, nPrice)    // constructor
?
+
         manufacturer = cName
? "   Welcome to the world of Objects"
+
         name = cName
?
+
         price = nPrice
 
+
    endproc
// Example of Constructor & Destructor
+
    procedure display()
class OpenTable
+
         dodefault()
     property cALIAS
+
        echo "Manufacturer is " + manufacturer + ", price is " + price + "\n"
     property nRECNUM
+
     endproc
 
+
    method Constructor
+
        parameters cTABLENAME, cTAGNAME
+
         local cTMPALIAS
+
         cTMPALIAS = basename(cTABLENAME)
+
         cTMPALIAS = iif(at('.',cTMPALIAS) = 0, cTMPALIAS, left(cTMPALIAS,at('.',cTMPALIAS) - 1))
+
         if select(cTMPALIAS) = 0
+
            use &(cTABLENAME + iif(empty(cTAGNAME), '', " order " + cTAGNAME)) in workarea()
+
        else
+
            select select(cTABLENAME)
+
            set order tag &cTAGNAME
+
        endif
+
        this.cALIAS = alias()
+
        this.nRECNUM = recno()
+
     return  && Constructor
+
 
+
    method Destructor
+
      close &(this.cALIAS)
+
    return  && Destructor
+
 
+
 
endclass
 
endclass
  
set exclusive off
+
// create new objects
oCOMPANY = new OpenTable("/usr/recital/unixdeveloper/demo/state.rdb", "state")
+
myobject = new vehicle("Ford", 20000)
? oCOMPANY.cALIAS
+
myobject.display()
? oCOMPANY.nRECNUM
+
 
+
//Example of ADDPROPERTY method
+
class Box
+
endclass
+
  
oDIALOG = new Box()
+
myobject = new motorbike("Honda", 1500)
oDIALOG.AddProperty("myprop", "hello world")  
+
myobject.display()
dialog box oDIALOG.myprop
+
release oDIALOG
+
 
</code>
 
</code>
  
 
==Products==
 
==Products==
Recital Database Server, Recital Mirage Server, Recital Terminal Developer
+
Recital Server, Recital  
 
[[Category:Documentation]]
 
[[Category:Documentation]]
 
[[Category:Commands]]
 
[[Category:Commands]]
 +
[[Category:Objects]]
 +
[[Category:Objects Commands]]

Latest revision as of 11:33, 13 January 2010

Purpose

Create a user-defined class


Syntax

CLASS <class name> [OF | AS | EXTENDS <base class> [, ...]]

ENDCLASS


See Also

ACLASS(), ADDPROPERTY(), AMEMBERS(), CLASS - Methods, CLASS - Parameters, CLASS - Properties, CLASS - Scoping, COMPOBJ(), CREATEOBJECT(), DEFINE CLASS, DISPLAY CLASSES, DODEFAULT(), FOREACH, LIST CLASSES, LOADOBJECT(), METHOD, NEWOBJECT(), OBJECT(), PRINT_HTML(), PRINT_JSON(), PRINT_R(), PRINT_XML(), REMOVEPROPERTY(), REQUIRE_ONCE(), SAVEOBJECT(), SQL SELECT, WITH


Description

Fundamental to object-oriented programming is the concept of Objects and classes. An object is a self-contained unit of data and functions that manipulate that data. A class is a specification of an object. A class contains memory variable specifications known as properties and functions that perform actions on the object known as methods. An object is an instance of a class.

The NEW operator is used to define a new object based on a class. The class name must be postfixed with parentheses when the new operator is used. The syntax is therefore as follows:

<object> = NEW <class>()

e.g. myobject = new myclass()

The CLASS...ENDCLASS construct is used to create a user-defined class. The beginning of a class is specified with CLASS <class name>, where <class name> can be any valid name up to 32 characters. The ENDCLASS command is used to complete the class construct. The CLASS...ENDCLASS construct is built using the commands describe in this section. Any Recital command can be used in building methods in the class, however these commands cannot be used outside the method definition inside a CLASS...ENDCLASS construct.

OF | AS | EXTENDS <base-name> [, ...]

A key feature of the object-oriented code is reusability through a mechanism called inheritance, that is, one class can inherit the members and their implementation from another class. Building new classes out of existing classes allows for the reusing of proven classes and the incorporation of system classes into user defined classes. Inheritance enables developers to build a hierarchy of descending objects. The inheriting class is called a derived class, and the class from which the derived class inherits is called a base class. The OF | AS | EXTENDS clause is used to inherit the <base name>. You can inherit multiple classes by specifying a class name comma-separated list.


Example

class product
    public name
    public price
    procedure init(cName, nPrice)    // constructor
        name = cName
        price = nPrice
    endproc
    procedure display()
        echo "Product name is " + name + ", price is " + price + "\n"
    endproc
endclass
 
class car of product
    private color
endclass
 
class motorbike as product
    private size
endclass
 
class vehicle extends car, motorbike
    public manufacturer = "Ford"
    public yearDesigned
    procedure init(cName, nPrice)    // constructor
        manufacturer = cName
        name = cName
        price = nPrice
    endproc
    procedure display()
        dodefault()
        echo "Manufacturer is " + manufacturer + ", price is " + price + "\n"
    endproc
endclass
 
// create new objects
myobject = new vehicle("Ford", 20000)
myobject.display()
 
myobject = new motorbike("Honda", 1500)
myobject.display()

Products

Recital Server, Recital