|
| Previous: 2.9.1 Object Oriented Programming | TOC | Index | Back | Next: 2.9.3 Objects |
The general forms of the class declaration are as follows:
CLASS classname() declarations END CLASS classname(required, ...) declarations END
The context of a class is delimited by the class header (consisting of the keyword CLASS, the name of the class, and the depdendency list in parentheses) and the keyword END. Inside of this context, there may be any number of declarations. These types of declarations are allowed in class contexts:
Nested classes are not defined in the T3X object model.
All declarations between CLASS and END are local to the class. Therefore, classes add an additional level of scoping between the global level and the procedural level. All data objects and procedures declared inside of a class are only visible inside of that class. The names of entities declared at class level may be reused outside of the scope of the class. Hence different classes may define data objects, procedures and even methods with equal names. The following example illustrates this principle.
CLASS a() VAR flag; PUBLIC flip() flag := \flag; END ! the scope of class A ends here. CLASS b() VAR flag; PUBLIC flop() flag := \flag; END
This example defines two classes A and B, each defining a variable named flag. At the end of the scope of A, all declarations of A become invisibe (encapsulated) and so the name flag may be reused in B. Since the procedure flip is contained in the same scope as flag, it may access the flag of A. In the same way, the procedure flop may access the flag of B. The two variables named flag are different entities, since they are contained in different classes.
Like structures, classes are merely templates for data objects. They describe the layout of a data structure plus a set of methods which may be used to manipulate that structure. The size of a class is computed in the same way as the size of a structure: it is equal to the sum of the sizes of all class members. In expressions, the name of a class is a constant evaluating to the size of the class. Classes without any instance variables have a size of one machine word.
The only way of changing the state of an object from the outside is to send it a message. OO systems which do not allow to change the state of an object without sending a message are said to employ strict encapsulation. Strict encapsulation in T3X is illustrated in the following figure. No direct communication with the inner layer of an object can be established, but only the outer layer containing methods and class constants is accessible.
+--------------------------------------------+ | | | M e t h o d s | | | | +--------------+ | | | Variables | | | +--------------+ | | | Constants | | | +--------------+ | |--------------| Structures |--------------| | +--------------+ | | | Procedures | | | +--------------+ | | | Objects | | | +--------------+ | | | | C l a s s C o n s t a n t s | | | +--------------------------------------------+ |
| Fig.2 Encapsulation |
In addition, T3X supports a simplified form of methods called class constants. Class constants may be thought of as lightweight methods returning a constant value. They allow to export values and structures without having to send a full message. Class constants are explained later in this chapter.
| Previous: 2.9.1 Object Oriented Programming | TOC | Index | Back | Next: 2.9.3 Objects |