Suppose you also want to represent the vehicle of a number of other persons. To do this you may of course define car objects like BirgersCar
for the car of each person you may need. The car of Liza may be e.g. defined as follows:
LizasCar: obj
licenseNumber: val 30227766
kerbWeight: val 1550
payLoad: var integer
addPassenger:
payload := payLoad + 80
The description of Liza’s car is almost identical to the description of Birger’s car except for the values of licenseNumber
and kerbWeight
. In general, we would like to avoid repeating the same code at different places.
We may describe a class that defines a template/pattern for cars in general as in the following example:
class Car:
licenseNumber: val 0
kerbWeight: val 0
payLoad: var integer
addPassenger:
payload := payLoad + 80
The keyword class
specifies that we define a class as opposed to an object using obj
.
The problem with this class definition is that we have specified licenseNumber
and kerbWeight
to 0 (zero). Birger’s car, Liza’s car and other cars have different values for these two data-items.
To handle this, we may make licenseNumber
and kerbWeight
be parameters of class Car
. The actual values of these data.-items may then be supplied when we create objects from class Car
. Class Car
with parameters is shown below:
class Car(licenseNumber: val integer, kerbWeight: val integer):
payLoad: var integer
addPassenger:
payload := payLoad + 80
Data-items specified with the parentheses are the parameters of the class — in this case licenseNumber
and kerbWeight
. The actual values of these data-items to be supplied later may be integer values, and they are tjerefore defined to be of type integer.
Class Car
may be used to create different car objects as in the example below:
BirgersCar: obj Car(20116677,1500)
LizasCar: obj Car(30227766,1550)
A figure showing BirgersCar
and LizasCar
.
Like a class, a method may have parameters as illustrated by the following example:
Added example with method parameters as such parameters are used in the chapter on data-items.
class Car(licenseNumber: val integer, kerbWeight: val integer):
payLoad: var integer
addPassenger:
payload := payLoad + 80
addLuggage(weight: var integer):
payLoad := payLoad + weight
A method, addLuggage
has been addded and addLuggae
has a parameter weight
, which represents the weight of the luggage. The weight
is addded to payLoad
.
Birger’s car and Liza’s car are examples of phenomena that we would like to represent in our vehicle registration system as objects.
Class Car
on the other hand represents the concept of a general Car from our application domain. The concept Car is a generalized idea covering all possible cars such as Birger’s car and Liza’s car.
Perhaps a new figure extending the one from Objects with reals cars, the concept of Car and a model with a class and car objects.
There is of course much more to say about classes than currently written here like a statement part.
Perhaps the current chapters 2-4 should just be sections in an Chapter on a quick introduction to basic concepts. We may then in subsequent chapters describe objects and classes in details!?
In the next chapter, the terms phenomenon and concept are described in more detail.