inside smalltalk

Download Inside Smalltalk

If you can't read please download the document

Post on 12-Feb-2017

232 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

  • HI: i

    Universitiit BernInstitut fUr Informatik undangewandte Mathematik

    Langgassstrasse 513012 BERN

    InsideSmalltalkVolume I

    ....,,----_._-----.,

    WiI' R. LaLondeSchool ofComputer ScienceCarleton University

    John R. PughSchool ofComputer ScienceCarleton University

    UN1VERSITAT BERN1t-!STITUT FUR INFORMATIK

    UNO ANGEWANDTE MATHEMATIKBibliothek

    I Signatur: 2[; 4A. !o ~ JI~--.-...--

    til Prentice-Hall International. Inc.

  • This edition may be sold only in those countries to whichit is consigned by Prentice-Hall International. It is not tobe re-exported and it is not for sale in the U.S.A., Mexico,or Canada.

    II (t) 1990 by PRENTICE-HALL, INC._ A Division of Simon & Schuster=- Englewood Cliffs, N.J. 07632

    All rights reserved. No part of this book may bereproduced, in any form or by any means,without permission in writing from the publisher.

    Printed in the United States of America

    10 9 8 7 6 5 4 3 2

    ISBN 0-13-468430-3

    Prentice-Hall International (UK) Limited, LondonPrentice-Hall of Australia Pty. Limited, SydneyPrentice-Hall Canada Inc., TorontoPrentice-Hall Hispanoamericana, S.A., MexicoPrentice-Hall of India Private Limited, New DelhiPrentice-Hall of Japan, Inc., TokyoSimon & Schuster Asia Pte. Ltd., SingaporeEditora Prentice-Hall do Brasil, Ltda., Rio de JaneiroPrentice-Hall, Inc., Englewood Cliffs, New Jersey

  • !I

    ~nivers;tiit BernInstltut fUr Informatik undang:wandte Mathematik

    langgassstrasse 513012 BERN

    Table of Contents

    PREFACE

    1 OBJECT-ORIENTED PROGRAMMING1.1 Introduction, 1

    1.2 OOP Is Programming by Simulation, 11.3 Traditional Versus Object-Oriented Programming, 3

    1.3.1 A Traditional Approach, 31.3.2 An Object-Oriented Approach, 41.3.3 Objects Encapsulate State and Operations, 51.3.4 Objects Communicate via Message-Passing, 6

    1.4 OOP Is Programming with Abstract Data Types, 71.5 OOP Is Programming via Classification, 91.6 OOP Is Programming with Polymorphism, 11

    1.6.1 Static Versus Dynamic Binding, 12

    1.7 OOP Is Programming with Inheritance, 131.7.1 Specialization and Generalization, 14

    1.8 Summary, 181.9 Glossary, 18

    2 SMALLTALK FUNDAMENTALS2.1 Introduction, 212.2 Objects in Smalltalk, 22

    2.2.1 What Is a Smalltalk Object?, 232.2.2 Information Hiding: Internal and External Views of an Object, 232.2.3 Literal Objects, 24

    2.3 Sending Messages, 252.3.1 Unary Messages, 262.3.2 Binary Messages, 272.3.3 Keyword Messages, 272.3.4 Evaluation of Message Expressions, 28

    XI

    1

    21

    iii

  • 2.3.5 Cascaded Messages, 282.3.6 Dynamic Binding and Overloading, 292.3.7 Variables and Assignments, 302.3.8 Allocation and Deallocation of Objects, 32

    2.4 Control Structures with Message-Passing, 322.4.1 Conditional Selection, 332.4.2 Conditional Repetition, 352.4.3 Fixed Length Repetition, 362.4.4 An Example: Testing for Primes, 372.4.5 User-Defined Control Structures, 39

    2.5 Classes, 402.5.1 Designing a New Class, 402.5.2 Class Protocol versus Instance Protocol, 412.5.3 Implementing a Class Description, 422.5.4 Describing a Class, 432.5.5 Describing Methods, 452.5.6 Variables and Scope, 462.5.7 The Pseudo-Variable self, 502.5.8 Methods Can Be Recursive, 51

    2.6 Inheritance, 512.6.1 Method Inheritance, 532.6.2 An Example: Constrained Pens, 532.6.3 The Pseudo-Variable super, 572.6.4 Abstract Classes, 60

    2.7 Summary, 62

    2.8 Exercises, 63

    2.9 Glossary, 64

    3 AN INTRODUCTION TO THE SMALLTALK USER INTERFACE3.1 Introduction, 61

    3.1.1 Smalltalk Provides an Integrated Programmng Environment, 683.1.2 Try it Yourself, 683.1.3 Not All Smalltalks Are Exactly Alike, 693.1.4 Not All Com puters Are Alike, 693.1.5 Pointing Device Mechanics, 70

    3.2 Getting Started, 713.2.1 Activating Smalltalk, 713.2.2 Changing the Active Window, 723.2.3 The 'Ideal' Smalltalk Mouse, 733.2.4 Using Pop-Up Menus, 753.2.5 Making a Menu Selection, 773.2.6 Restoring the Display, 77

    3.3 Manipulating Windows, 773.3.1 Creating New Windows, 773.3.2 Manipulating Windows, 783.3.3 Relabeling Windows, 803.3.4 Scrolling through Windows, 81

    iv

    67

    Inside Smalltalk

  • 3.4 Editing Text, 853.4.1 Inserting Text, 863.4.2 Selecting Text, 863.4.3 Replacing Text, 873.4.4 Deleting Text, 883.4.5 Cut, Copy, and Paste, 883.4.6 Again and Undo, 89

    3.5 Evaluating Smalltalk Expressions, 903.5.1 Evaluating Code in a Workspace Window, 903.5.2 Evaluating Existing Smalltalk Code, 913.5.3 Compilation Errors, 91

    3.6 Quitting from Smalltalk, 933.7 Summary, 943.8 Exercises, 94

    3.9 Glossary, 95

    4 PROGRAMMING WITH BROWSERS 994.1 Introduction, 994.2 System Browsers, 1014.3 Viewing Existing Classes, 102

    4.3.1 Finding a Class, 1044.3.2 Viewing Class Definitions, 1044.3.3 Viewing the Class Hierarchy, 1064.3.4 Viewing the Protocol Supported by a Class, 1074.3.5 Viewing Methods, 1084.3.6 Finding a Method, 1094.3.7 Obtaining Explanations, 109

    4.4 Evaluating Code from within a Browser, 110

    4.5 Adding and Modifying Methods and Classes, 1124.5.1 Modifying Existing Methods, 1124.5.2 Adding New Classes, 1144.5.3 Adding New Methods, 1184.5.4 Adding New Class Categories, 1194.5.5 Modifying Existing Class Definitions, 1194.5.6 Renaming Class Categories, Classes, Method Categories, and Methods, 1214.5.7 Removing Class Categories, Classes, Method Categories, and Methods, 122

    4.6 Specialized Browsers, 1234.6.1 Browsing by Category, Class, Message Category, and Message, 1234.6.2 Browsing the Superclass Chain, 1264.6.3 Browsing Selected Sets of Methods, 127

    4.7 Saving Your Work, 1334.7.1 Filing Out, 1334.7.2 Printing, 1354.7.3 Filing In, 1354.7.4 Using the File List Browser, 1354.7.5 Updating the Smalltalk Image, 1394.7.6 Using the Changes File, 1404.7.7 Surviving a System Crash, 141

    Table of Contents v

  • 4.8 Summary, 141

    4.9 Exercises, 142

    4.10 Glossary and Important Facts, 143

    5 DEBUGGING WITH INSPECTORS, NOTIFIERS, AND DEBUGGERS 1475.1 Introduction, 147

    5.2 Inspecting Objects, 1485.2.1 Inspecting the Instance Variables of an Object, 1505.2.2 Modifying the Values of the Instance Variables of an Object, 1505.2.3 Evaluating Expressions within an Inspector, 1505.2.4 Inspecting the Instance Variables of an Inspected Object, 1515.2.5 Inspecting Dictionaries, 152

    5.3 Error Notification with Notifiers, 1555.3.1 Interpreting NotifierWindows, 1555.3.2 Continuing after an Error Notification, 1575.3.3 User-Generated Notifiers, 1585.3.4 Interrupting a Non-Terminating Computation, 1585.3.5 Setting a Breakpoint, 1585.3.6 Handling Exceptional Conditions, 159

    5.4 Debuggers, 1605.4.1 Viewing an Interrupted Computation with a Debugger, 1605.4.2 Error Correction within a Debugger, 163

    5.5 Summary, 174

    5.6 Exercises, 175

    5.7 Glossary, 176

    6 OBJECTS 179

    6.1 Introduction, 179

    6.2 Class Object, 1816.2.1 The Representation of an Object, 1816.2.2 Bindings: Assignments and Parameter Passing, 1846.2.3 The Inherited Representation of an Object, 1866.2.4 Querying Operations, 1886.2.5 Debugging, Inspecting, and Confirming, 1906.2.6 Meta Operations for Accessing and Modifying Objects, 1956.2.7 Copying Operations: Shallow versus Deep Copies, 1986.2.8 Comparison Operations: Identity versus Equality, 2006.2.9 ReadlWrite Operations: PrintStrings and StoreStrings, 2016.2.10 Meta Operations for Indirect Execution (perform:), 2066.2.11 Advanced Meta Operations, 208

    6.3 Class UndefinedObject, 213

    6.4 Class BlockContext (Blocks for Short), 2146.4.1 Blocks Provide Facilities to Design Control Structures, 2186.4.2 Syntactic Details and Recursive Blocks, 219

    6.5 Class Boolean, 220

    vi Inside Smalltalk

  • 6.6 Designing a New Class: BinaryTree, 2236.6.1 A Standard Design, 2246.6.2 A Non-Standard Design, 229

    6.7 Classes and Metaclasses, 2336.7.1 Multiple Inheritance, 237

    6.8 Summary, 240

    6.9 Exercises, 2406.10 Glossary and Important Facts, 241

    7 THE MAGNITUDE CLASSES7.1 Magnitudes, 245

    7.1.1 Class Magnitude Simplifies the Implementation of New Magnitudes, 246

    7.2 Numbers, 2477.2.1 The Notation for Number Constants, 2477.2.2 Converting Numbers to Strings, 2487.2.3 Converting Strings to Numbers, 2497.2.4 Type Conversion, 2607.2.5 Division, Remainders, Truncation, and Rounding, 2557.2.6 Mathematical Operations, 2587.2.7 Creating a New Subclass of Number, 2597.2.8 Bit Manipulation on Integers, 260

    7.3 Date and Time, 2657.3.1 Class Operations for Dates and Times, 2657.3.2 Conversion Operations for Dates and Times, 2667.3.3 Querying Operations for Dates and Times, 2687.3.4 Arithmetic Operations for Dates and Times, 2687.3.5 Designing an Absolute Time Class, 268

    7.4 Characters, 274

    7.5 Random Streams, 275

    7.6 Summary, 276

    7.7 Exercises, 277

    7.8 Glossary and Important Facts, 277

    8 THE COLLECTION CLASSES8.1 Introduction, 281

    8.1.1 A Logical Organization, 2848.1.2 Creating Collections, 2868.1.3 Comparing Collections, 2908.1.4 Sequencing over Collections, 292

    8.2 The Keyed Collections (Non-Streams), 2988.2.1 Individual Characterizations, 2998.2.2 Constructing New Keyed Collections, 3008.2.3 The Dictionary Protocol, 3048.2.4 The Array and OrderedCollection Integer-Keyed Protocol, 3118.2.5 The String, Symbol, and Text Protocol, 3178.2.6 The Mapped Collection and Run Array Protocol, 3248.2.7 The Interval Protocol, 327

    Table of Contents

    245

    261

    vii

  • 8.3 The Streamable Collections (Streams), 3298.3.1 Individual Characterizations, 3308.3.2 Constructing New Streamable Collections, 3318.3.3 How Read and Write Streams Are Typically Used, 3328.3.4 Read, Write, and ReadWrite Streams. 3348.3.5 File Names, 340

    8.4 The Ordered Classes (Non-Streams an