#Data Class Example
The following code shows an example of a complete (but small) data class.
/* Data class that specifies a task with a certain execution time. */ data class TaskSpecification extends Object variables executionTime : Integer methods setExecutionTime(time: Integer) : TaskSpecification executionTime := time; return self getExecutionTime : Integer return executionTime printString: String return "Task(execution time: " + executionTime printString + ")"
The data class definition starts with the declaration of its name and its inheritance relation.
data class TaskSpecification extends Object
data class are fixed keywords and the name of the new data class is
extends followed by the data class name
Objects states that this data class inherits from data class
Object. The inheritance declaration is optional. When it is omitted, it is implicitly assumed that the class inherits from the root data class
Object, from which all data classes directly or indirectly inherit. The data class
Object itself is a native data class (there are more) and it does not need to be declared explicitly to be used in a model.
variables executionTime : Integer
variables is followed by a list of declarations of instance variables, the attributes of the instances of the class. In this case just one with the name
executionTime, which is declared to be of type
Integer. This expresses that the variable is expected to be bound only to objects of type
Integer (i.e., instances of class
Integers, or to the object
nil). Note that it is up to the individual tools to decide to what degree the type declarations are enforced, statically, or at run-time. The POOSL-IDE performs some static type checking, but does not enforce strong static typing, because that would limit the expressiveness or succinctness of the language.
The section starting with the keyword
methods defines the methods of the data class, in this case:
setExecutionTime(time: Integer) : TaskSpecification executionTime := time; return self
The first method provides the users of the object access to the
executionTime attribute. Note that the instance variables of a data object are strongly encapsulated. They cannot be accessed directly by any other objects. Access can be given indirectly by methods like this one (and the next).
The first line defines the method signature. The name of the method (
setExecutionTime), its formal parameters (between parentheses) as a comma separated list of variable declarations. Anyone calling this methods needs to provide the right parameters, in this case one object of type
Integer. This is followed by a colon and then the name of a data type. This type expresses what type of data object the method is expected to return. All data methods always return exactly one data object. Both method parameters and returned data objects are always passed by reference, never by value. The declaration is followed by a sequence of statements or expressions.
executionTime := time assigns the object passed by parameter
time to the instance variable
executionTime. The last statement executed in the method results in the value that is returned. The keyword
self evaluates to a reference to the object executing the method. Hence, this method returns a reference to the data object evaluating the method itself. This is often convenient, because it allows the caller of the method to immediately call another method of the same object in the same expression.
getExecutionTime : Integer return executionTime
The next method is the counterpart of the previous method that allows also the instance variable to be read by other objects.
printString: String return "Task(execution time: " + executionTime printString + ")"
The last method is a generic trick used in the POOSL language and tools to allow data objects to produce textual representations of themselves. It is defined in class
Object and overriden in many data classes to provide more detailed representations. Not the use of the
+ operator to concatenate