Difference between revisions of "CLASS"
Yvonnemilne (Talk | contribs) |
Yvonnemilne (Talk | contribs) |
||
(3 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
==Purpose== | ==Purpose== | ||
Create a user-defined class | Create a user-defined class | ||
Line 6: | Line 4: | ||
==Syntax== | ==Syntax== | ||
− | CLASS <class name> | + | CLASS <class name> [OF | AS | EXTENDS <base class> [, ...]] |
− | + | ||
− | [OF | AS | EXTENDS <base class> [, ...] | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
ENDCLASS | ENDCLASS | ||
Line 22: | Line 10: | ||
==See Also== | ==See Also== | ||
− | [[ADDPROPERTY()]], [[CLASS - Methods]], [[CLASS - Parameters]], [[CLASS - Properties]], [[CLASS - Scoping]], [[CREATEOBJECT()]], [[DEFINE CLASS]], [[DISPLAY CLASSES]], [[DODEFAULT()]], [[LIST CLASSES]], [[METHOD]], [[NEWOBJECT()]], [[OBJECT()]], [[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== | ||
− | |||
− | |||
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 41: | Line 27: | ||
====OF | AS | EXTENDS <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 | AS | EXTENDS 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. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==Example== | ==Example== | ||
<code lang="recital"> | <code lang="recital"> | ||
− | class | + | 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 | endclass | ||
− | + | class car of product | |
− | + | private color | |
− | + | endclass | |
− | + | ||
− | + | ||
− | + | ||
− | + | class motorbike as product | |
− | class | + | private size |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
endclass | 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 | endclass | ||
− | + | // create new objects | |
− | + | myobject = new vehicle("Ford", 20000) | |
− | + | myobject.display() | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | myobject = new motorbike("Honda", 1500) | |
− | + | myobject.display() | |
− | + | ||
− | + | ||
</code> | </code> | ||
Latest revision as of 11:33, 13 January 2010
Contents
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