LZX Classes

A class is a blueprint or a template to create objects of identical type. If you have an Employee class, you can create any number of Employee objects. To create Street objects, you need a Street class. A class determines what kind of objects you get. For example, if you create an Employee class that has age and position attributes, all Employee objects created out of this Employee class will have age and position attributes as well. No more no less. The class determines the object.

In summary, classes are an OOP tool that enables programmers to create the abstraction of a problem. In OOP, abstraction is the act of using programming objects to represent real-world objects. As such, programming objects do not need to have the details of real-world objects. For instance, if an Employee object in a payroll application needs only be able to work and receive a salary, then the Employee class needs only two methods, work and receiveSalary. OOP abstraction ignores the fact that a real-world employee can do many other things including eat, run, kiss, and kick.

Classes are the fundamental building blocks of an OpenLaszlo program. All program elements in LZX must reside in a class and every class must be saved as an lzx file. You need to consider three things when writing a class:

  • the class name

  • the attributes

  • the methods

In the first few chapters of this book, you will learn how to use standard classes that come with the LZX compiler. Chapter 12, “Extending Classes” discusses how to write your own classes.

Note

In UML class diagrams, a class is represented by a rectangle that consists of three parts: the topmost part is the class name, the middle part is the list of attributes, and the bottom part is the list of methods. (See Figure 1.4) The attributes and methods can be hidden if showing them is not important.


Figure 1.4. The Employee class in the UML class diagram


Attributes

Attributes are variables. For example, the Employee class in Figure 1.4 has two attributes, age and salary. An attribute can reference a simple data type such as an integer or a complex type such as an object.

Methods

Methods define actions that the objects (or instances) of a class can do. A method has a declaration part and a body. The declaration part consists of a return value, the method name, and a list of arguments. The body contains code that perform the action.

To declare a method, use the method tag.

<method name="methodName" [args="listOfArguments"]>

The args attribute is optional.

For example, here is a method named addNumbers that accepts two arguments.

<method name="add" args="a,b">
    return (a + b);
</method>

Note

Actually, there are two other class members in LZX, events and handlers, which will be explained in Chapter 5, “Event Handling.”


Class Members in UML Class Diagrams

Figure 1.4 depicts a class in a UML class diagram. The diagram provides a quick summary of all attributes and methods. You could do more in UML. UML allows you to include attribute types and method signatures. For example, Figure 1.5 presents the Book class with five attributes and one method.

Figure 1.5. Including class member information in a class diagram


Note that in a UML class diagram a colon separates an attribute and its type. A method’s argument list is presented in parentheses and its return type is written after a colon.

Case Sensitivity

OpenLaszlo 4 applications can be compiled into swf7, swf8, or DHTML. If the version is not specified, swf7 will be used. Identifiers in LZX programs are case-sensitive. For example, having two global variables called myData and MyData will generate a compile error.

LZX Programs

An LZX program is also a valid XML document. If you are not familiar with XML, please take time to read and understand Appendix A, “Introduction to XML.”

Inside an LZX program, you use programming syntax similar to JavaScript. Appendix B provides a brief tutorial on JavaScript.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset