• Aucun résultat trouvé

CLASS OBJECT

Dans le document til Smalltalk (Page 197-200)

Inspectors, Notifiers, Debuggers

6.2 CLASS OBJECT

Anobject consists of a representation and operations (or methods) that it responds to.

It is an instance whose class can be determined by sending it the message class; e.g., 1.2e3 class will reply with Float. The class is the repository for all the information about the instances; Le., it stores both the representation information and the operations. However, the information in an individual class may notbecomplete; part of the information may reside in another class called the superclass.

In general, a class may have a superclass, which in turn may have its own superclass, which again has a superclass. This sequence culminates in a final class that has no superclass - this class is Object, the ultimate superclass of all classes. In general, many classes can have the same superclass - hence the relationship is a tree-structured hierarchy as shown in Fig. 6.1. Actually, Smalltalk permits classes to have several superclasses, leading to a concept called multiple inheritance. However, there are no examples in the system - we will not consider the concept further in this section.

The representation information and the operations associated with an instance are obtained by concatenating the partial information stored in each of the classes in the superclass chain that starts with the object's class and culminates in Object.

In the sections that follow, we first consider the detailed representation of an object We investigate this representation ignoring the effects of the hierarchy. Then we consider Smalltalk's notion of bindings as it relates to parameter passing and assignments so that we can better appreciate the power of this representation. Next, we review this representation in the context of the hierarchy. We then investigate the operations provided by class Object, a protocol that is inherited by all classes in the system.

6.2.1 The RepresentationofanObject

Since an object consists of a representation and operations, it consists of anything that can be manipulated. In particular, any object can be inspected; e.g., 1957 inspect, #(1 234) inspect, 'hello' inspect, Integer inspect. Objects include such things as characters, integers, strings, arrays, ordered collections, sets, and classes themselves.

named instance variable1 named instance variable 2 named instance variable 3

• ••

indexed instance variable1 indexed instance variable 2 indexed instance variable 3

• •

Figure 6.2 The representation of an object.

Chapter 6 Objects 181

However, there are things that are not objects. For example, a variable is not an object. Variables cannotbemanipulated as separate entities. They cannotbeinspected nor can theybestored into arrays, for example. Of course, the value bound to a variable can be inspected and stored into an array. This is not the same thing. If variables were objects, one wouldbeable to store one into an array and legitimately claim "this array contains a variable - independently, the variable also contains a value." Since variables cannotbemanipulated, they are not objects.

In more detail, an object (see Fig. 6.2) consists of zero or more fields called instance variables partitioned into two groups: named instance variables and indexed instance variables. The named instance variables precede the indexed instance variables.

When a class is defined, the names of all named instance variables mustbespecified along with an indication as to whether or not indexed instance variables are permitted.Ifno indexed instance variables are permitted, all instances of the class will be the same size.

Otherwise, distinct instances canbedifferent sizes. Depending on the choices taken, several combinations are possible:

CIa_ with objectll containing no inatence v.iab....

Examples include Object, True, False, UndefinedObject, and InputSensor.

CIe_ with objectll conteining only named inatence veriab....

Most classes in the system and most user created classes will fall into this category.

Class. with objectll containing only indexed inatenc:e variebl• .

Examples include classes like Array, String, Symbol, LargePositivelnteger, and LargeNegativelnteger.

CIe_ with objectll containing both named and indexed instance variabl• . Examples include OrderedColiection, Dictionary, Set, and SortedCollection.

Such classes are normally created with the browser. The easiest way is to find any class that already has the required structure and modify its definition. For example, the following definitions were obtained by investigating the class definitions for Fraction, Array, WordArray, String, and OrderedCollection:

Fraction (a class with only named instance variables) Number aubela. .: #Fraction

inatenceVariableNamea: 'numerator denominator' cla..VariabIeNemea: "

poolDictionariea: "

category: 'Numeric-Numbers'

Array (a class with only indexed instance variables that can contain arbitrary objects) ArrayedColiection variableSubclaaa: #Array

inatanceVariableNemea: "

cla. .VariabIeNemea: "

poolDictionariea: "

category: 'Collections-Arrayed'

I'illI

WordArr.y (another class with only indexed instance variables capable of containing only words)

ArrayedCollectionv.ri.bl.WordSubcl. . .:#WordArray inaUnceV.riebleNemee: "

e....V.rieb..Nemee: "

pooID~"":"

_tegory:'Graphics-Support'

String(another class with only indexed instance variables capable of containing only bytes)

ArrayedCollectionv.riebl.Byt.Subc....:#String inat8nceV.riebleNemee: "

e....V.riebleNemee: "

pooID~"":"

e.tegory:'Collections-Text'

Ord.redCoII.etion(a class with both kinds of instance variables) SequenceableCollectionv.ri.bl.Subel•••:#OrderedCollection

in.tenceV.rieb"Nemea:'firstlndex lastlndex' e....V.riebleNemee: "

pooID~riea:"

e.tegory:'Collections-Sequenceable'

Asyou can see, classes without indexed instance variables are created with a method that begins subclass: ; the alternative uses methods variableSubclass: ... , variableWordSubclass: , or variableByteSubclass:... (indexed instance variables respectively contain arbitrary objects, word-sized integers, or byte-sized integers).

Named instance variables are normally accessed by referencing the variables by name.

More specifically, when the receiver of a message is, say, an ordered collection, the corresponding method that executes can reference firstlndex or lastlndex by name. Indexed instance variables are accessed via the subscripting operations basicAt: and basicAt:put:.

See the section on accessing and modification operations for more details.

anObjectbe.ieAt:anlnteger

Returns the value of the indexed instance variable at index anlnteger. Legal index values range between1and anObjectb••ieSiz•.An error is reported if the index is not an integer or if it is out of range.

anObjectb••ieAt:anlntegerput:anotherObject

Changes the value of the indexed instance variable at index anlnteger to anotherObject and returns anotherObject. Legal index values range between 1and anObjectb••ieSiz•.An error is reported if the index is not an integer or if it is out of range.

As a user, it is importanttohave this image of an object as a record with an arbitrary number of fields, some named and some indexed. On the other hand, the low-level operations that provide direct access to these fields should only be used for implementing higher level facilities.

Chapter 6 Objects 183

6.2.2 Bindings: Assignments and Parameter Passing

In this section, we wish to consider the meaning of an assignment such as af-b

since it can lead to confusion if it is not properly understood. We can explain it from two perspectives: from the logical point of view, which concentrates on whatitmeans, and from the implementation point of view, which concentrates on howitis done.

In a language like Pascal, C, or Ada, an assignment like a f- b is interpreted as"copy b into a" and implemented by "copying the contents of b into the space occupied by a."

Thus, a and b must be the same type and, most importantly, the same size. This is a very restrictive requirement. For example, it makes it impossible for the arbitrary elements of a set data type to be manipulated unless the element types were previously specified by the user - it also makes it difficult to mix the element types.

In Smalltalk, a f- b is interpreted as"bind a to the same object that b is bound to."

From the logical point of view, assignments do not copy - they simply rebind. From the implementation point of view, all variables contain pointers to objects; assignments physically copy pointers but they do not copy the objects. Fig. 6.3 illustrates this pictorially.

a

b

a

b

---~-{

Object!

---~,

Obj·e1 2

Before a f -b

Objectl

Object 2 After a f -b

Figure 6.3 The meaning of assignment.

]

]

]

To repeat, before the assignment, a is bound to object! and b is bound to object2.

After the assignment, a is also bound to object2 - hence a and b are bound tothe same

Dans le document til Smalltalk (Page 197-200)