Object-Orientation with XOTcl Components and Object Introspection - Tutorial

In this section you will learn about the dynamic and object-oriented aspects of XOTcl programming in XOTclIDE.

Load Sample Application

Start XotclIDE:

[artur@rybnik xotclIDE]$ ./XotclIDE

Now load the component SampleComponent [1] that is included in the XOTclIDE distribution. Choose the menu Component->Load Package in Component Browser. From the dialog-box select the “SampleComponent” package to load. Now you should see the following in your Component Browser window:

Figure 2.7. Sample Component in Component Browser

Sample Component in Component Browser

The Component has two classes (Railroad and Wheel). You can browse the methods of these classes to see how the component works.

Create an Instance of a Class

Now try creating some instances of the Railroad class. Select the Railroad class in Class view and choose the menu Class->Create Instance. You will see the following dialog

Figure 2.8. Create Instance Dialog

Create Instance Dialog

In this dialog you can specify the arguments (or additional arguments) for the init method (passed to init method or parameters). The class definition of Railroad is

Class Railroad -parameter {{speed 4}}

You could modify the start speed by specifying a different speed value (try "-speed 10"). For now you can simply push the OK button and use default speed 4.

Object Inspector

The new created object of class Railroad builds a window and starts to run.

Figure 2.9. Sample Railway application

Sample Railway application

In addition XOTclIDE can show another window called an object inspector browser. This window shows the internal structure of XOTcl Objects. You can see an Object's variables (attributes), subobjects (aggregated objects) and methods.

You can modify an Object's state by changing a variable in the browser. Choose the speed variable. You should see the value 4 in your edit-area view. Type 20 in this area and press Control-S or choose menu Edit->Save You should see the locomotive speed up.

Figure 2.10. Object Inspector

Object Inspector

You can also invoke methods on an object directly in the Object Inspector. Select the emergencyBreak method and choose menu Method->Invoke.

Figure 2.11. Methods in Object Inspector

Methods in Object Inspector

You can also specify which methods you should see - only those from Railroad or also those inherited from other Objects or Superclasses. You can even see the mixin methods available on the object.



[1] Example by Richard Suchenwirth translated from Tcl to XOTcl by Gustaf Neumann and modified by me. First published on tclwiki.