29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14. The Magnitude Class Hierarchy<br />

14.1 <strong>Introduction</strong><br />

This chapter considers the magnitude class <strong><strong>an</strong>d</strong> those classes which inherit from it. This includes the<br />

Number hierarchy. We also consider the classes Character, Time, <strong><strong>an</strong>d</strong> Date.<br />

14.2 The class Magnitude<br />

The Magnitude class is the abstract super class of all classes which possess some concept of a size<br />

<strong><strong>an</strong>d</strong> the comparison of objects of different size (see Figure 14.1). This me<strong>an</strong>s that all types of numbers<br />

inherit (eventually) from Magnitude, although they are in fact all subclasses of <strong>an</strong>other class<br />

Number, which acts as the abstract superclass for all numbers. This abstract class inh erits all of its size<br />

related behavior from the Magnitude class. Other classes which have some concept of size <strong><strong>an</strong>d</strong> thus<br />

c<strong>an</strong> be compared via logical operators such as include Time, Date <strong><strong>an</strong>d</strong> Character. The<br />

subclasses of Magnitude will be considere d later in this chapter, in this section we will concentrate<br />

on the Magnitude class.<br />

Figure 14.1: The top of the Magnitude class hierarchy<br />

The Magnitude class provides protocol for comparing objects whic h possess size <strong><strong>an</strong>d</strong> which c<strong>an</strong><br />

use their size to r<strong>an</strong>k themselves. The relationship of this size to their peers is class dependent <strong><strong>an</strong>d</strong> is not<br />

defined by the Magnitude class. The comparing protocol in Magnitude defines the methods < <strong><strong>an</strong>d</strong> = :<br />

• = all of which are defined in terms of <<br />

<strong><strong>an</strong>d</strong> =. The protocol also defines max: <strong><strong>an</strong>d</strong> min: messages used to return either the maximum or<br />

minimum of two numbers respectively. For example:<br />

10 max: 15.<br />

10 min: 15.<br />

The first expression returns the result 15 <strong><strong>an</strong>d</strong> the second 10. Magnitude also provides a test method<br />

which checks to see if a value is between two other values. For example:<br />

10 between: 1 <strong><strong>an</strong>d</strong>: 9.<br />

118

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!