Our first example
Aims of the program example
In this example, we are going to develop a program that lists the names of the classes of the package ("Package" metaclass), and for each class, provides a list of public methods.
Realization principle
The J method, which we will call "listClasses", must be defined on the "Package" metaclass. This method will run through all the classes of the package that is being processed, and for each class, it will list methods with public visibility.
Note: This example is for the "listClasses" command on packages.
J program
Package:listClasses () Method definition
{
OwnedElementClass // Going through the Package
classes
{
StdOut.write(NL, "CLASS:", Name);
PartOperation.<select (Visibility == Public)
// Going through the method's public classes
{
StdOut.write(NL, " Public method:", Name);
}
}
} // End of "listClasses
Description of the terms used
|
Term |
Nature |
Definition |
|
Package |
Metamodel class |
Package |
|
NL |
Constant |
Return (New Line) |
|
OwnedElementClass |
object (Class[ ]) |
Classes of the current package |
|
StdOut |
Predefined variable |
Standard output |
|
PartOperation |
object(Operation[ ]) |
The operations of the class |
|
Visibility |
object - (String attribute) |
Visibility of an operation |
|
Name |
object - (String attribute) |
Name of the current object |
|
listClasses |
J method |
Defined method |
|
String |
basic class |
Character string |
|
write |
Predefined method |
Writing in a file |
Execution
If a "Human_Society" package is composed of "Man" and "Woman" classes with the "walk" and "eat" methods, the "listClasses" message sent to the "Human_Society" object will give the following result:
CLASS: Man
Public method
:eat
Public method
:walk
CLASS: Woman
Public method
:eat
Public method
:walk