Вы находитесь на странице: 1из 296

http://www.natahaus.

ru/

. .
-
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

http://www.natahaus.ru/
.
I.

II.

III.

IV.

V.

VI.

VII.

. 1
- . 2
.
, . ,
CRC-
2. CRC-, ,
,
.
, 3 4 ,
Smalltalk, C++, Java, Objective-C Object Pascal ,
. 3
( ), 4
( ). 5 6
. ,
-
.
7, 8 9

. 8, Java,
(API
application program interface). 9

.
. 10 13
.
,
. 10
. ,
. 11
. 12

,
. , 13 .
. -
.
14
-
. 15
. ,
(STL Standard Template Library) ++,
16.
. 17
-
. 18
.
. , 19
.
.
, 3. 20
- .

PDF created with pdfFactory Pro trial version www.pdffactory.com

( -
) (
). 21
, - .
, ,
,
.
. -
.
.
.
, - ,
.
.
Web .


, ,
, ftp ftp.cs.orst.edu, /pub/budd/oopintro.
,
, , , ,
. World Wide Web
http://www.cs.orst.edu/~budd/oopintro.
budd@cs.orst.edu : Professor
Timothy A. Budd, Department of Computer Science, Oregon State University, Corvallis,
Oregon, 97331.


,
, Pascal .
undegraduate level graduate level.
( ) ,
. , ,
, 17,
21 .
.

1 : -
- ()
.
- .
( ) ,
. -
. ,
, -
, ,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
- ,
.
, -
.
,
.

1.1. ?
( )
- :

,
,
;
;
, .

-
,
. :
, ,
, .
-
, ,
(,
[Brooks 1987]), . , .
, ,
, , , , ,
, ,
.
,
, C++ Object Pascal ( Smalltalk Beta).
, , C
Pascal C++ Object Pascal ,
.
, . -
, ,
, .
,
.

1.2.

, .
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, -

. ,
... ,
, ,
,
.
( [Whorf 1956]).
, , ,
. ,
,
, , ,
.

1.2.1.
, ( .
[Pillum 1991]) , ,
, ( )
, , . . -
.
.
,
. , -

, . , ,
. ,
( ), ,
.
, (, ) (
) , .
, .
- (, C++)
- .
- -
, , ,
.

1.2.2.
, ,
.
, ,
, , .
,
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
. .
N , N (
). , - M
(M 510)
.
ACTCGGATCTTGCATTTCGGCAATTGGACCCTGACTTGGCCA...
, ,
Fortran
DO 10 I = 1, N-M
DO 10 J = 1, N-M
FOUND=.TRUE.
DO 20 K = 1, M
IF (X(I+K-1).NE.X(J+K-1)) FOUND=.FALSE.
IF (FOUND) ...
CONTINUE

20
10

, ,
.
, APL.
, .
: Fortran
, APL . , ,
APL- , ,
, .
APL- .
N , , N
M :
A
C
T
C
G
G

C
T
C
G
G
A

T C
C G
G G
G A
A T
T T
. . .
T G G A
G G A C

G
G
A
T
T
C

G
A
T
T
C
T

C
C

C
C

1
2
3
4
5
6

M
M+1
M+2
M+3
M+4
M+5

. -
,
.
T
T

.
G
G
.

.
G
G
.

.
A
A
.

C
C

C
C

. , APL , APL
Fortran. , Fortran
O(M N 2), APL-
O(M N log N) .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, APL
, Fortran, , APL-
. , - , APL
,
. ,
, APL-
. ,
, ,
.

1.2.3.
, , ,
. ,
. , [Pullum 1991].
, , ,
,
. ,

, .
.
30- ,
. 40
50- ,
. ,
[Church 1936], [Post 1936], [Markov 1951], [Turing 1936],
[Kleene 1936] . ,
,
. , ,
.
, .
: , ,
.

,
.
, , -,
1 .
1


. ,
.
, ,
. ,
, ,
. , : ,
, ,
, . ,
. , ,
( )
. . .

PDF created with pdfFactory Pro trial version www.pdffactory.com


. .
, .
1960- , , ,
,
[Bohm 1966].
,
goto .
, ,
, ,
. ,
,
.
.

, .
, ,
, .
,
. ,
. ,
, ( ) . ,
, , , .

:
. , ,
, ,
. ( ) ,
, . .
,
.
, , :
, .

1.3.
-
. : (
Pascal C), ( Prolog) ( Lisp, FP
Haskell) .
.
American Heritage Dictionary of the English Language:
par-a-digm (.) 1. ,
, . 2.

PDF created with pdfFactory Pro trial version www.pdffactory.com

( paradigma paradeigma ,
paradeiknunai , ).
, (, )
. , ,

,
[Kuhn 1970]. ,
, ,
, .
, ,
, .
,
, 1979 ,
[Floyd 1979].
, , ,
, .
-
,
(17071778), . , ,
- ,
.
- ,
, , -
. , , Smalltalk
, [Kay 1977].
, , .
. - ,
.

1.4.
-
, , ,
.
, ( )
. , , ,
, .
:
, ( ) (florist ),
, , (
) , , .

PDF created with pdfFactory Pro trial version www.pdffactory.com

1.4.1 , ,
, - , ,
,
( , ) , .
. ,
, .
,
, .
.
, , , ,
, .

() , , . .

, .
, -
.
-
(), .

(), . (receiver) ,
. ,
.
,
.
,
. : , ,
, .
, ,
. , ,
- , .
,
. , (
) ,
. -
,
.

.
? ,
.
.
,
, .
. (, , ,

PDF created with pdfFactory Pro trial version www.pdffactory.com


).
, ( ,
)
. , , ,
, (
). , (
, ), ,
, .
, ,
. , ,
.
.
,
(
, )
.
, , , .
, (
) (),
. (
) ,
.

1.4.2.
-
.
( ).
,
, .
, .

. 2 ,
.
, ,
.
,
-
:
, ,
, .

1.4.3.
,
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, , , ,
, .
Florist ( ) ,
, () ,
. -
:
, , . ,
, ,
.
.
-
. ,
Object Pascal ( object), (
) , - . .
-
. , -
: ,
. ,
.

1.4.4.
, Florist. ,
(shopkeeper).
, , ,
.
, , . . Florist
, Shopkeeper, ,
Shopkeeper, Florist, , , .

(. 1.1). Florist; Florist
Shopkeeper. , Shopkeeper ,
Human ,
. , Human
(Mammal), ,
(Animal) , ,
.
.
, (, Material Object
Animal) ,
, . 1.2
.
, , , , ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 1.2. ,

, , , ,
, . ,
, .
,
. :
.
( ) ( ),
1 .
, ( Mammal . 1.2).
.

1.4.5.
. ,
,
, (, ) .
,
.
, , , , 1

. 1.2 ,
. . .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
, , ,
. ,
, : ,
, ,
. ,
.
,
.
, .
, , ,
.
,
,
, ,
, .
11.
,
,
. 14. , , ,
, ,
, 17.

1.4.6.
, - -
,
[Kay 1993]:
1. .
2. ( )
, ,
. , .
,
, .
3. , .
4. ,
(, ).
5. () . ,
,
.
6. ,
. ,
, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

1.5.
, ,
, . ,
,
. ,
. ,
( ),
, (. 1.3).
, ,
.
, , ,
, .
, , ,
,
. , ,
NewsWeek:
, NeXT
, , ,
.
, ,
, , .
,
,
,
.

1.5.2.
, ,
.
, ,
.
- .
- -
.
, -
(, C++, Object Pascal Objective-C), ,
( -) . (, Smalltalk Java)
,
.

1.6.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
.
, , ,
.
?
? ,
?
, Fortran, Cobol Algol,
(
).
.
, ,
.
, .

1.6.1.
,
. ,
, .
, ,
[Brooks 1975].
. ,
,
.
:

( ),
.
, .
, .
,
,
, (
).
.
,
.
,
, .
, ,
, . ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

1.6.2.
.
- ,
, .
,
.
: ,
.

,
.
, (,
), ,
. ,
. ,
.
.
. ,

.

:
, ,
.
,
, init, push, pop top.
. :
, . .
, , 1.1.
, , ,
- , .
(
Fortran C, static),
. ,
. ,
datastack,
, ,
. datastack ,

. init, push, pop
top
( ),
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

1.1.
int datastack[100];
int datatop = 0;
void init()
{
datatop = 0;
}
void push(int val)
{
if (datatop < 100)
datastack [datatop++] = val;
}
int top()
{
if (datatop > 0)
return datastack [datatop 1];
return 0;
}
int pop()
{
if (datatop > 0)
return datastack [--datatop];
return 0;
}


, (,
Pascal), ,
. , ,
. , .
, ,
. ,
.
begin
var
datastack : array [1..100] of integer;
datatop : integer;
procedure init; . . .
procedure push(val : integer); . . .
function pop : integer; . . .
. . .
end;


.
,
( ),
, ( )
. ,
,
. (public) , (private)

PDF created with pdfFactory Pro trial version www.pdffactory.com

. , ()
.
[Parnas 1972] .
:
1. , ,
, , , .
2. ,
, .

: ,
. ,
(information hiding).
, .
, ,
, ( ) ?
, ,
, Complex.
, ,
. . .
:
.
,
, . ,
, ,
,
.
,
.


.
, , . ,
(, )
, .
,
, , .
,
,
.
.

. , .
, :

PDF created with pdfFactory Pro trial version www.pdffactory.com

1. .
2. ,
.
3. , ,
.
4. .

2 3 .

. ,
, CLU Ada,
.
. ,
, Smalltalk
, -
. ,
, - ,
,
.

: ,
-
. .
, ,
. :
,
. ( push
, stack
?) , -
,
.
/ .
,
. ,
, , .
push
. ,
.
.
, -
.
,
. ,
.

, ,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

1.7.
,
. ,
, ,
,
. ?
,
.
,
. ,
,
(
).
.
,
.

. ?
.
,
.
-
( )
( ). -
,
. ,
, ,

.

1.8.
- ,
.
.
()
, . .
. ,
,
.
()
().
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
.
( ) .
.
( )
.

.
. , ,
, .
.

,
.
, .

.
, ,
, , .


, -
. ,
. [Kay 1993] ,
Smalltalk
Simula, 60- [Dahl 1966,
Kirkerud 1989]. ,
Simula, ,
()
Smalltalk. Byte 1981
,
Xerox PARC.
, -, 1968 . ,
,
.
, ,
1968 ., ,
Scientific American 1994 [Gibbs 1994].

. , , ,
. , ,
-, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

(. 21)
,
[Whorf 1956].
. ,
, .
.
[Logan 1986],
,
, .
.
[Unger 1987]
.
,
,
[Pullum 1991]. Atlantic Monthly ( 1995)
, , ,
-, ,
. , , ,
( ,
).
.
,
,
, ,
. .
- , ,
- ,
,
.
.

[Church 1936], ,
[Turing 1936]. ,
, , ,
.
[Rogers 1967].
, ,
. . ,
, ,
.
.
(
)
[Wulf 1973] ,
.
[Hanson 1981].

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
- .
: - ?
. [Stroustrup 1988] ,
:

X .
.
, X -.

, . ,
, ,
.
,
, -, .,
, , [Micallef 1988]
[Wegner 1986].
, , , ,
(, Ada), - ,
.
[Cox 1990]
. -
(objective)
, ,
, .
,

. [Cox 1986],
- ,
,
- .

1. -
.
. ,
, .
.
2. .
.
3. ( ,
) ()
.
4. ( ) ,
,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

5. ( ) , ,
,
.

2 : -
: , ,
- ?,
, C++ Object Pascal,
, - , C Pascal.
, ,
, .
- ,
.
- ( ,
, ) ,
, -
. 1,
, .

(responsibility-driven design) [Wirfs-Brock 1989b, Wirfs-Brock 1990].

2.1.
, ( ),
. - (
, ) ,
,
. , ,
. ,
, , ,
.
,
.
1,
, ,
. , ,
.
-
,
, ()
.
- - ,
.
.
,
-
. , ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
.
,
, ,
. ,
, .

.
,
. , ,
( , 6),
,
.
,
.
,
(,
) ,
.
.

2.2.
,
.
:

, ,
.
, .

.

,
:

.
( )
, ,
. ,
.

.

,
-
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
.

2.3. ?
-
? ,
.

,
.
.
, , .
,
( ) ,
, .
( RDD Responsibility-Driven-Design) .

2.4. :
,
.
, , , .
Interactive Intelligent Kitchen Helper
( ) (. 2.1)

. 2.1.
, ,
( -,
, , ).

PDF created with pdfFactory Pro trial version www.pdffactory.com

2.4.1.
(Interactive Intelligent
Kitchen Helper, IIKH) .
, .
, IIKH
, . IIKH
,
.
,
IIKH
. , IIKH
.
,
,
.

, , .

IIKH.
.
. ,
, -
.
.

2.4.2.
. ,
,
. .
, (
).
( ), ,
. , ,
,
, ,
,
. 2.6.2. .
. ,
, , .

2.4.3.
, ,
.

. ,
( ).
,

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
, ,
(). :

;
,
.

.
.

2.5. CRC-
,
.
, . ,
, .


. ,
, .
CRC- Component, Responsibility,
Collaborator (, , ) [Beck 1989].
, CRC.

2.5.1.
CRC-
. , ,
,
.
, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

CRC- , ,
. ,
, .

,
( ).

, ,
, , ,
. ,
.

2.5.2. /
,
.
/. -, :
? : ?
,
, . , ,
.
,
. (
.) , ,
. , ,
.
, -
.
- ,
.

2.5.3.
.
:
.
, .

. ,
- . ,
, ,
,
.
- ,
.
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
, , , ,
, ,
.
. , ,
.
,

, ,
.
CRC- ,
.
, ,
. .
,
, .

2.6.
IIKH. ,
, (. . 2.1).
, Greeter.
, ( , ,
)
.
:
1. , -
.
2. .
3. .
4. .
5. .
.
, .
: ,
. ,
,
Recipe Database. . 2.2

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 2.2. CRC- Greeter


CRC- Greeter.
, ,
.
: Recipe Database
, ,
.

2.6.1.
, .
, , , ,
, ?
,
, (, , )?
(
)? (scroll bars)
? ,
,
(. 2.6.2. ).

, , ,
, .

2.6.2.
, , ,
. .

,
(
).
:

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
.
.

.
, ,
.

. ,
,
. .
.

,
.
,
, .
,
, ,
.
,
.

2.6.3.

. , ,
. .
,
.
- . ,
.
, .
, .
Recipe. ( Recipe
.
-.
.)
, ,
, .
,
( ),
. ,
, .
Greeter. , ,
Plan Manager.
. , Plan Manager
,
.
( ) .

PDF created with pdfFactory Pro trial version www.pdffactory.com

Date.
Date.
Plan Manager
. , Plan Manager
.
Date :
() , (
, , . .). ?
. ,
.
, Meal.
Meal . ,
.
. ,
. , .
, , -
, . ,
Meal .
.
. ,
, ,
? (,
), ? .

.
, ,

(. 2.3). Greeter Plan Manager Recipe Database.
Plan Manager Date, Meal.
Meal Recipe Manager
.

. 2.3. IIKH

2.6.4.
, . 2.3,
.
. .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 2.4 IIKH.
. .

. (, , )
.
.
.

.

.

. 2.4.

2.7.
.
(, ,
).

2.7.1.
, , ,
. . ,
, - Recipe IIKH.
.

, .
. ,
, Recipe, ,
, ,
.
, .
Recipe
. .
, , .

. , Greeter, ,
, - .
, .

2.7.2.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
. , .
. :
.
, . , ,
.
.
,
- .
. , ,
.
. ,
. Recipe.
(, , ),
.
3.

2.7.3.
(coupling)
(cohesion). ,
, .
( )
.
.
Recipe.
,
.
,

.
, ,
() . .
,
. , ,
Recipe Database,
. Recipe Database
(
: ).
, .
,
17.

2.7.4.

. , ,
, , .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, IIKH
. , Meal,
.
Meal browse,
Recipe Database. browse Recipe .
, Recipe
Database .
.
. , ,
, , ,
.
. ;
. , .
, ,
. , .
, ,
. .
,
.
,
, .
,
.

. ,
.
,
,
:

,
,
.

.

,
,
.

2.8.
IIKH.
. .
, . ,
( ),

PDF created with pdfFactory Pro trial version www.pdffactory.com

. , ,
, .
. ,
CRC- , .
. ,
. () , .
,
( , ) .

2.8.1.
, , .
, 1 ,
.
,
.
. ,
, , .
.
,
.
,
[Keller 1990]:

, . :
, .
,
: CardReader Card_reader
cardreader.
. , ,
. TermProcess
(terminal process), ,
(terminate process), , ?
. empty
, ,
( )?
. (0 O, 1 l, 2
Z, 5 S).
, () ,
, , true false. ,
PrinterIsReady , true
, PrinterStatus .
( )
, .
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, CRC-

. CRC- Date
. 2.5. , ,
.
1

? , , .
, . , ,
II, 2 (. ).

, ,

.

. 2.5. CRC- Date

2.9.
, ,
,
.
. ,
,
.
,
. ,
. , ,
,
.
.

.

. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,

.

2.10.
,
. ,
.
.
,
.
(, ),
, .
,
.

, ,
.
,
, .
,
,
, .
.
.

,
. ,
,
. ,
.

2.11.
,
. .
, ( ) .
(stubs)
- .
, IIKH
Greeter.
,
Recipe Database Meal Plan.
. ,
Greeter

PDF created with pdfFactory Pro trial version www.pdffactory.com

(, ,
). .
. ,
Recipe Database ,
. , ,
. .
, .
,
-.
, ,
, .
, ,
.
.
,
, (regression testing).

2.12.
, ,
. , .
.
, :

.
(patches) ,
.
, -
.
. ,
(
). : ,
.
.
, , . .
, .
.

1. ,
. (, ,
, ), ( , , )
(, -, ).
( ).

PDF created with pdfFactory Pro trial version www.pdffactory.com

2.
1.
3. ,
.
.
4. ATM (Automatic Teller Machine).
Teller (,
, , . .),
.
.

3 :
, - , ,
, , ,
. 1. ,
. , ,
.
.
"" .
; 4
.
. 4 ,
. 7.
, ,
. ( )
. (
, ), . ,
,
.
,
(, C++) , , ,
.

3.1.
1 , - ,
, .
.
, ,
. ,
, , "".
, 2.
( )
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, , ,
, ,
.
, stack
, push, pop, top. , ,
stack,
(. 3.1). .

. 3.1. stack
.

, .
.
, , .
, , .
,
. .
, ,
.

3.2.
-
.
:

;
;
;
, , .

, .
-
, . , ""
, .
- Data Managers, Data
State, ,
-. ,
Card , ()
. -
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

Data Sources , (,
). Data Sinks, ,
(, ).
,
, ( )
( ).
View Observer
. (
, ). ,
.
,
.
( )
( ). ,
, .

. ,
.
. ,
, , .
.
(Facilitator Helper) ,
, . ,
,
. , ,
().

3.3. :
,
- ,
.
Card "". Card,
,
.

. 3.2. CRC- Card

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 3.2 CRC-, .
Card .
, ,
.
2, CRC- ,
. ,
.
, ,
( , - ).
CRC-, . 3.3, . ,
(,
" "),
. , .
17.
, CRC ,
. ,
CRC-, . ,
.

. 3.3. CRC- Card

3.4.
1 - ,
,
.
, , , . ,
- ,
.
:

,
, .
,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ,
,
(interface), ,
. (implementation).
, .
, ,
.
Smalltalk ,
, .
,
. ,
, . ,
. ,
, ,
.

3.5.

, .
, -
,
.

3.5.1. Object Pascal


Object Pascal. ,
Apple Computer [Tesler 1985].
Apple Pascal. Object Pascal
Turbo Pascal [Turbo 1988, OBrian 1989].
Borland International.
Macintosh, IBM PC. ,
Borland, Object Pascal.
Delphi Windows-
[Borland 1995]. ,
Turbo Pascal. , ,
.
Object Pascal (unit).
C++ Objective-C ,
. (interface)
(implementation). .
,
.
Card Object Pascal Apple
3.1. interface Pascal.
, const, type var.
(, suits colors).

PDF created with pdfFactory Pro trial version www.pdffactory.com

(record), ,
.
.
, suitValue, suit.
.
, .
.
.
.
. ,
.

3.1. Object Pascal Apple


unit card;
interface
type
suits = (Heart, Club, Diamond, Spade);
colors = (Red, Black);
Card = object
(* *)
suitValue : suits;
rankValue : integer;
faceUp
: boolean;
(* *)
procedure setRankAndSuit (c : integer; s : suits);
(* *)
function color : colors;
procedure draw (win : windows; x, y : integer);
function faceUp : boolean;
procedure flip;
function rank
: integer;
function suit
: suits;
end;
implementation
...
end.

3.2. Deplhi Pascal


implementation
const
CardWidth = 65;
CardHeight = 75;
function Card.color : colors;
begin
case suit of
Diamond: color:= Red;
Heart:
color:= Red;
Spade:
color:= Black;
Club:
color:= Black;
end;
end;
...

PDF created with pdfFactory Pro trial version www.pdffactory.com

end.

3.5.2. Smalltalk
Smalltalk
Smalltalk. , , Smalltalk
,
Smalltalk. ,
.
. ,
, Smalltalk,
[Goldberg 1984, LaLonde 1990b, Korienek 1993, Smith 1995].
,
(. 3.4).
.
. , :
,
.
Smalltalk .
, Smalltalk.
,
. "Graphics-Primitives"
:
, ,
, .

:
Object subclass: #Card
instanceVariableNames: 'suit rank'
classVariableNames: ''
poolDictionaries: ''
category: 'Graphics-Primitives'


, Card Object.
Card . Delphi Pascal,
. Object
.
, - .
Smalltalk ,
.
, .
- (pool variables).
.
. - .

PDF created with pdfFactory Pro trial version www.pdffactory.com

# Card .
.
,
.
.
,
accept . ,
( ).

. ,
, ;
,
.
.
, accept
, . ,
.
setSuit: s rank: r
" suit rank "
suit := s.
rank := r

Smalltalk ,
, (
). , ,
setSuit:rank:. , s r.
Smalltalk ,
:=. ,
, ,
.
Smalltalk .
, (accessor functions).
suit, ,
:
suit
" "
suit

, , , return
. ,
. ,
, ,
(
).
1 13 , (
rank). ( SmallTalk)

PDF created with pdfFactory Pro trial version www.pdffactory.com

. color ( )
. :
color
" "
(suit = #diamond)
ifTrue:
(suit = #club)
ifTrue:
(suit = #spade)
ifTrue:
(suit = #heart)
ifTrue:

[
[
[
[

#red ]
#black ]
#black ]
#red ]

, Smalltalk ,
, (
). , Smalltalk blocks,
, Pascal ( begin,
end). ( . ,
ifTrue
. Smalltalk- .)

3.5.3. Objective-C
Objective-C -
C.
C. ,
C : (
".h") ( C ".c",
Objective-C ".m"). , ( ,
)
.
, ,
( 3.4), .
.
.
. , Objective-C,
Smalltalk,
3.4. Card Objective-C
/*
Card
: Objective-C
: , 1995
*/
# import <objc/Object.h>
/* */
# define
Heart
0
# define
Club
1
# define
Diamond
2
# define
Spade
3
/* */
# define
Red
0
# define
Black
1
/* Card */
@ interface Card : Object
{
int suit;
int rank;

PDF created with pdfFactory Pro trial version www.pdffactory.com

int faceup;
}
/* Card */
- (void)
suit: (int) s rank: (int) c;
- (int)
color;
- (int)
rank;
- (int)
suit;
- (void)
flip;
- (void)
drawAt: (int) and: (int);
@ end

(
).
.
, .
(
, ),
.
, C
Objective-C. import include C,
, , .
Object. define
,
.
@ , Objective-C.
Card.
, .
Objective-C, Smalltalk Delphi Pascal,
; Object
.
, ,
, () .
. Objective-C
C (, ,
. .) . id.
C, , id,
, Null.
, , ,
. "-" () ,
, C.
, . (id)
, . suit (,
, ) integer. flip
void. C ,
( , ).
, . , ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ( ,
, ),
Smalltalk ,
. Smalltalk
,
id. ,
.
( 3.5)
. Objective-C
C. , 3.5 ,
, C.
implementation , .
,
implementation .
3.5. Card Objective-C
/*
Card
: Objective-C
: , 1995
*/
# import "card.h"
# define
cardWidth
68
# define
cardHeight 75
@ implementation Card
- (int)
color
{
return suit % 2;
}
- (int)
rank
{
return rank;
}
- (void)
suit: (int) s rank: (int) c
{
suit = s;
rank = c;
faceup = 0;
}
// ... -
@ end

, implementation ,
. ,
.
, . C,
.

3.5.4. C++
C++, Objecive-C, -
C. C,
( ".h") ( ).

PDF created with pdfFactory Pro trial version www.pdffactory.com

Objective-C, (, ,
) ,
, . C C++
import ( Objective-C),
. card.h
, CARDH (, )
, ifndef (
), CARDH . , card.h
. ,
.
# ifndef CARDH
# define CARDH
. . .
# endif

//

class ( 3.6). C++


C, ,
. private:
, . public:
, .
Objective-C, private
( ,
), (
).
public,
. , , ,
, .
card(suit, int)
, , ,
. ,
.
4.
void, Objective-C, .
, ,
, .
draw halfdraw
. .
ANSI C C++. ,
,
.
window, draw,
. & . ,
( window ), .
3.6. ard C++
enum suits {diamond, club, heart, spade};

PDF created with pdfFactory Pro trial version www.pdffactory.com

enum colors {red, black};


//

//

//
: C++
//
: , 1995
class card{
public:
//
card (suits, int);
//
colors
color
();
bool
faceUp
();
int
rank
();
suits
suit
();
//
void
draw
(window &);
void
halfdraw (window &, int x, int y);
void
flip
();
private:
bool
faceup;
int
r;
//
suits s;
//
};

,
,
. s , ,
suit . r rank
.
,
.
.
//
//
//
//
# include "card.h"
card::card (suits sv, int rv)
{
s = sv;
//
r = rv;
//
faceup = true; //
}
int card::rank()
{
return r;
}

C,
. (
) .
,
.

, C++
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
.
,
,
. ( register, inline-
, .)
inline int Card::rank()
{
return r;
}


, , ,
. ,

.
,
, return.
, , .
.
,
, .
3.7. ard inline-, C++
//

//
: C++
//
: , 1995
class card
{
public:
//
card (suits, int);
card ();
card (const card & c);
//
int
rank()
{return r;}
suits
suit()
{return s;}
colors color();
bool faceUp()
{return faceup;}
//
void
draw (window & w, int x, int y);
void
halfdraw (window & w, int x, int y);
void
flip()
{faceup = ! faceup;}
private:
bool
faceup;
int
r;
//
suits
s;
//
};

, . ,
( ) . ,
,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
,
. ,
, private.
,
.
,
(. 3.7). ,

, , . ,
,
, . (private)
(public),
.
,
, ,
, , .

3.5.5. Java
, Java C++. ,
, ,
Java . , Java
, , , , goto, ( ),
. ,
( C++) " " .
Java ,
World
Wide Web.
Java .
Java ( 3.8)
C++ :

, , .

final.
""
.
3.8. Java

class Card
{
//
final public int red
= 0;
final public int black
= 1;
final public int spade
= 0;
final public int heart
= 1;

PDF created with pdfFactory Pro trial version www.pdffactory.com

final public int diamond


= 2;
final public int club
= 3;
//
private boolean faceup;
private int
r;
private int
s;
//
Card (int sv, int rv)
{ s = sv; r = rv; faceup = false; }
//
public int rank ()
{ return r; }
public int suit()
{ return s; }
public int color ()
{ if ( suit() == heart suit() == diamond )
return red;
return black; }
public boolean faceUp()
{ return faceup; }
//
public void draw (Graphics g, int x, int y)
{
/* ... ... */
}
public void flip ()
{ faceup = ! faceup; }
};

,
- . ( C++ ,
Java.)
private public
.
boolean bool,
++.
(, C++,
, )
.

8 , Java.

1. , ( ) C.
?
2. Smalltalk Objective-C , ,
, .
C++ .
, , ,
.
3. ,
,
. : .
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
.
4. . ,
.
5. ,
. , ,
. ?
6.
C++. addi .
class example1
{
public:
int i;
};
int addi(example1 &x, int j)
{
x.i = x.i + j;
return x.i;
}
class example2
{
public:
int i;
int addi(int j)
{ i = i+j; return i; }
};

7. , C++ Objective-C,
.
?
. ,
, .
8. , : public private
( Java)
( C++, Objective-C Delphi
Pascal)? .

4: ,

3 -
. , , .
, .
, ,
.
.
.
.

, ,
, .
, , ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

4.1.
( )

. 1
, . :

, -
.
, ,
. ,
, .

:
(, ), (,
) ,
.
-
, , ,
. ,
,
, .

4.1.1. Object Pascal


- Delphi Pascal
.
.
Object Pascal , ,
. 3,
, . ,
,
, .
, , ,
(
7). aCard
Card,
.
aCard.draw (win, 25, 37);
. ,
,
. ,
. ,
:
aCard.flip;

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
. ,
, .
.
, -,
. self
. ,
.
, color, ,
:
function Card.color : colors;
var ss : suits;
begin
ss := self.suit;
if (ss = Heart) or (ss = Diamond) then
color:=Red
else
color:=Black;
end;

suit .
,
suitValue. Delphi Pascal ,
Result, (color
).

4.1.2. C++
3,
++, ( )
C++.
(member function); .
. ( ).
, ,
( -
) ( ).
theCard Card,

25 37:
theCard.draw(win, 25, 37);
, ,
- .
, :
if ( theCard.faceUp() )

PDF created with pdfFactory Pro trial version www.pdffactory.com

{
...
}
else
{
...
}

Object Pascal,
, .
, ,
.
- , void,
( ). -,
,
C (), (
).
,
. C++ this ,
.
( ->) .
, color, ,
(
, ):
colors Card::color()
{
switch ( this->suit() )
{ case heart:
case diamond:
return red;
}
return black;
}

this ,
:
void aClass::aMessage(bClass b, int x)
{
//
b->doSomething(this, x);
}

C++ this .

. color
( ) :
colors Card::color()
{
switch ( suit() )
{ case heart:
case diamond:
return red;
}
return black;
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

4.1.3. Java
Java
C++. , this
C++ , Java
( Java !).

4.1.4. Smalltalk
Smalltalk , C++
Object Pascal . -
, .
, .
C++, ,
. C++ Smalltalk
. :
,
, .
aCard Card,
:
aCard flip
3,
. - ,
. aCard
25 37:
aCard drawOn: win at: 25 and: 37
Smalltalk , + *,
, ,
1.
( ),
. :
, .
Smalltalk self
. ,
, .
, color
, suit:

4.1.5. Objective-C
Objective-C
Smalltalk. ,
Smalltalk Objective-C .

PDF created with pdfFactory Pro trial version www.pdffactory.com

Objective-C
. , [ ].
, aCard Card,
(
):
C++ (, +, -, <
=). ,
12.
color
" "
(self suit = #diamond)
ifTrue: [ #red ].
(self suit = #club)
ifTrue: [ #black ].
(self suit = #spade)
ifTrue: [ #black ].
(self suit = #heart)
ifTrue: [ #red ].
1

[ aCard flip ];
Smalltalk, Objective-C .
: , ,
, .
, .
Smalltalk .
, aCard
25 36:
[ aCard drawOn: win at: 25 and: 36 ];
.
,
. ,
newCard aCard:
newCard = [ aCard copy ];
,
C. , C ,
aCard :
if ( [ aCard faceUp ] ) ...
Object Pascal, Objective-C
self .
self ,
. 4.3.3,
-, .

4.2.

,

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ,
, ( )
.
( (heap)), ,
. 4.2.1.
,

.
. ,
,
. (
) .
(static) ,
. ,
static ,
C C++. ,
,
,
.
.
(Pascal)
new(x), . :
type
shape : record
form : (triangle, square);
side : integer;
end;
var
aShape : shape;
begin
new (aShape);
...
end.

,
- ,
.
.
(, C)
. , C
malloc,
. malloc ,
-
. :
struct shape
{
enum {triangle, square} form;
int side;
};

PDF created with pdfFactory Pro trial version www.pdffactory.com

shape *aShape;
...
aShape = (struct shape *)
malloc(sizeof(struct shape));
...


, (
) -
,
. .
,

. .

4.2.2.
,
, .
. ,
C C++ , ,
,
. ,
dispose, C free.
(Java, Smalltalk) ,
(
). ,
.
.
. .
[Cohen 1981].

(
10),
.
(
), .
, ,
, :

, .
(,
).
, .
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
.

, . ,
,
.

4.2.3.
,
-
. : ?
.
, - .
(Java, Smalltalk)
, .
(C++, Object Pascal) ,
, -.
C++, ,
- .
Objective-C , id,
, .
Objective-C
, .

4.2.4.
3 Card ,
, ,
() .
, suit rank (
),
(single-assignment variables)
(immutable variables). ,
, .
,
.
(, C )
,
.
, ; ,
( ,
).
, .
, ,
, ,
,
. -

PDF created with pdfFactory Pro trial version www.pdffactory.com

, , .
, , ,
. 4.3.
,
.
, .

4.3.1. C++
C++ C ( Pascal ),
, .
,
, .
C:
. ,
.
, .
C++
. 3, ,
, .
,
. . , ,
, .
, ,
new -
.
, ,
:
class Complex
{
public:
//
Complex();
Complex(double);
Complex(double,double);
//
...
private:
//
double realPart;
double imaginaryPart;
};

. ,
, .

Complex numberOne;

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
:
Complex::Complex()
{
//
realPart=0.0;
imaginaryPart=0.0;
}

, ,
.
,
C++ ( ,
). C++

.
,
. ,
, ,
, . , ,
. :
Complex pi =
Complex e
Complex i

3.14159265359;
(2.7182818285);
(0.0, 1.0);

,
. :
Complex::Complex(double rp)
{
//
realPart = rp;
//
imaginaryPart = 0.0;
}

, ,
. ,
.

.
,
.
; ,
.
Complex::Complex(double rp) :
realPart(rp), imaginaryPart(0.0)
{ /* */ }

, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

new,
, .
:
Complex *c;
c = new Complex(3.14159265359, -1.0);

,
:
Complex *d = new Complex;
,
.
. , ,
(
, ):
Complex *carray = new Complex[27];

delete ( delete[]
).
C++
const.
. , ,
,
.
C++ ,
, .
.
, .
delete.
- (~).
.
,
. Trace ,
.
(), ,
, Trace
( ).
, ,
.
class Trace
{
public:
//
Trace (*char);
~Trace ();
private:

PDF created with pdfFactory Pro trial version www.pdffactory.com

char *text;
};
Trace::Trace(char *t) : text(t)
{
printf("entering %s\n", text);
}
Trace::~Trace()
{
printf("exiting %s\n", text);
}

,
, Trace
.
:
void procedureA()
{
Trace ("procedure A");
procedureB(7);
}
void procedureB(int x)
{ Trace ("procedure B");
if (x < 5)
{
Trace ("Small case in procedure B");
...
}
else
{
Trace ("Large case in procedure B");
...
};
...
}

Trace
. :
entering procedure A
entering procedure B
entering Large case in procedure B
...
exiting Large case in procedure B
exiting procedure B
exiting procedure A

4.3.2. Java
Java C++ ( ,
) , Java ,

. ,
.
Java null.
new. C++ Java
,
:

PDF created with pdfFactory Pro trial version www.pdffactory.com

//
Card aCard
//
Card bCard


= new Card (Card.spade, 5);

= new Card ();

Java C++ ,
Java . C++
Java ,
.
this:
class NewClass
{
NewClass(int i)
{
// 1-
...
}
NewClass(int i, int j)
{ //
this(i);
//
...
}
}

Java C++. Java


finalize, ,
. finalize ,
, ; ,
, . , (
) finalize.
,
.
Java
new
, .
, .
.
// Complex
a = new ("Comp" + "lex");

3, ,
, final. ,
,
. final const
C++, .
final aCard = new Card(Card.spade, 3);
aCard.flip(); //

4.3.3. Objective-C

PDF created with pdfFactory Pro trial version www.pdffactory.com

Objective-C Smalltalk C.
id,

. (
Smalltalk) new . (, id
typedef id
.)
, id, .
7, Objective-C
.
, ,
.
.
Objective-C
. Smalltalk
. (
, .
-
.
.)
+ (),
. , - (), .
,
Card.
@implementation Card
{
+ suit: (int) s rank: (int) r
{
self = [Card new ];
suit = s; rank = r;
return self;
}
}
@end

, new ,
, Smalltalk. Objective-C
,
. , (
), self
.
self , ,
self
. ( , self
, ,
Objective-C.)
, self ,
suit rank .
Objective-C ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

free ,
. free Object,
:
[ aCard free ];
Objective-C
.

4.3.4. Object Pascal


Object Pascal
new.
. ,
, dispose, ,
.
new dispose .
.
, .
, ,
, .
type
Complex : object
rp : real;
ip : real;
procedure initial (r, i : real);
...
end;
var
aNumber : Complex;
procedure Complex.initial(r, i : real);
begin
rp:=r; ip:=i;
end;
begin
new(aNumber);
aNumber.initial(3.14159265359, 2.4);
...
dispose(aNumber);
end.

, ,
. ,
, (
).
Object Pascal Apple -
, .
( new),
. .
Apple Object Pascal . ,
rp ip

PDF created with pdfFactory Pro trial version www.pdffactory.com

Complex, , initial
.
Object Pascal Delphi C++. ,
private.
Delphi Pascal . 3.5.1,
constructor.
-
, :
aCard := Card.Create(spade, 5);
- ,
. C++
Delphi Pascal ,
.
Delphi Pascal . - (
Destroy) destructor.
, free
self nil
-.
type
TClass = class (TObject)
constructor Create(arg : integer);
procedure doTask(value : integer);
destructor Destroy;
end;
destructor TClass.Destroy;
begin
(* *)
end;

Delphi Pascal
,
, property (). property
(
, ).
. ,
, property
.
property . read,
, write
. (,
- ,
private .)
type
TnewClass = class (TObject)
property readOnly : Integer
read internalValue;
property realValue : Real
read internalReal
write checkArgument;

PDF created with pdfFactory Pro trial version www.pdffactory.com

private
internalValue : Integer;
internalReal : Real;
procedure CheckArgument (arg : Real);
end;

4.3.5. Smalltalk
Smalltalk .
new -, .
- 18.
, - ,
,
.
, new -, ,

, . ,
Card:
aCard := Card new.
Smalltalk
. , ,
.
, .
Smalltalk . , -.
- - 18.
, -
-. new.
new ,
. -
, ,
.
- suit:rank:. ()
Card suit:rank: (
- Card) .
suit: s rank: r newCard
" "
newCard := Card new.
" "
newCard suit: s rank: r.
" , "
newCard

Card , ,
:
aCard := Card suit: #diamond rank: 4.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
( Smalltalk).
,
.
-, .
() ,
.
Smalltalk
. , suit:rank:, private.
, .
,
.
Smalltalk
. ,
( ).
,
. ,
Set 1, 2, 3. ,
aSet, .
:
aSet := Set new add: 1; add: 2; add: 3.

1. copy Card 3.
Card ,
,
copy.
2.
,

? (:
.
.)
3. . , C++,
. ,
Smalltalk Objective-C, .
? ?
? .
4.
4.2.2 ,
?
5. . [Appel 1987] ,
,
.
. , ,
, ?
6. (- )
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

5: :

( ,
) , . ,
.
.
, -
. .

5.1.
, ,
. .
8 ,
. . 5.1, .

(., , [Griswold 1983, Budd 1987, Berztiss 1990]).
-
?
- .
,
: ?

- . ,
. , , ,
. , ,

. ,
, , .
, -
, , .
, 1, ,
. ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 5.1.

5.1.1. ,
, , ,
? ,
, , .

.
, . ,
, ,
: . ,
: , ; ,
; .
N 1
N, .
.
, .
Queen () CRC-,
(, ), . 5.2.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 5.2. CRC- Queen

5.2.
,
:
, .
[Hanson 1981], [Bertiss 1990].
.
, (-)
, , ( -
) (. ).
canAttack ,
.
function queen.canAttack(testRow, testColumn) -> boolean
/* */
if row = testRow then
return true
/* */
columnDifference := testColumn column
if (row + columnDifference = testRow) or
(row columnDifference = testRow)
then return true
/* , ? */
return neighbor.canAttack(testRow, testColumn)
end

PDF created with pdfFactory Pro trial version www.pdffactory.com

5.2.1.
. initialize
, .
findSolution, .
, advance
() .
N
( N1).
, . , -
, , .
.
.
function queen.initialize(col, neigh) -> boolean
/* */
column := col
neighbor := neigh
/* 1 */
row := 1
end

5.2.2.
, ,
. , . ,
. , ,
() .
function queen.findSolution -> boolean
/* */
while neighbor.canAttack (row, column) do
if not self.advance then
return false
/* */
return true
end

4, self
. , , ,
advance .

5.2.3.
advance . ,
1.
, ,
1.
function queen.advance -> boolean
/* */
if row < 8 then
begin
row := row + 1
return self.findSolution

PDF created with pdfFactory Pro trial version www.pdffactory.com

end
/* */
/* */
if not neighbor.advance then
return false
/* 1- */
row := 1
return self.findSolution
end

.
print, .
procedure print
neighbor.print
write row, column
end

5.3.

. .
,
. , Smalltalk Objective-C,
, Object Pascal,
C++ Java,
.

5.3.1. : Object Pascal


Object Pascal Apple
. ,
Queen Queen. ,
.
Queen .
C++
12.
type
Queen = object
(* *)
row : integer;
column : integer;
neighbor : Queen;
(* *)
procedure initialize (col : integer; ngh : Queen);
(* *)
function canAttack (testRow, testColumn : integer) :
boolean;
function findSolution : boolean;
function advance : boolean;
procedure print;
end;

Delphi (
). Borland

PDF created with pdfFactory Pro trial version www.pdffactory.com

(public) (private) , -,
initialize.
TQueen = class (TObject)
public
constructor Create (initialColumn : integer;
nbr : TQueen);
function findSolution : boolean;
function advance : boolean;
procedure print;
private
function canAttack (testRow, testColumn : integer) :
boolean;
row : integer;
column : integer;
neighbor : TQueen;
end;

, ,
Pascal . : Pascal
return. : , ,
. findSolution advance, ,
. (, Delphi Pascal
Pascal , and () or (),
C++. , Delphi
.)
function Queen.findSolution : boolean;
var
done : boolean;
begin
done := false;
findsolution := true;
(* *)
if neighbor <> nil then
while not done and neighbor.canAttack(row, column) do
if not self.advance then
begin
findSolution := false;
done := true;
end;
end;
function Queen.advance : boolean:
begin
advance := false;
(* *)
if row < 8 then
begin
row := row + 1;
advance := self.findSolution;
end
else begin
(* *)
(* *)
if neighbor <> nil then
if not neighbor.advance then
advance := false
else begin
row := 1;
advance := self.findSolution;
end;

PDF created with pdfFactory Pro trial version www.pdffactory.com

end;
end;

,
.
() , . Apple
Object Pascal, , . neighbor i
, , lastQueen
.
begin
neighbor := nil;
for i := 1 to 8 do
begin
(*
*)
new (lastQueen);
lastQueen.initial (i, neighbor);
if not lastQueen.findSolution then
writeln(no solution);
(* *)
neighbor := lastQueen;
end;
(* *)
lastQueen.print;
end;
end.

,
, Delphi .
Delphi :
begin
lastQueen := nil;
for i := 1 to 8 do
begin
(* *)
lastQueen := Queen.create(i, lastQueen);
lastQueen.findSolution;
end;
//
lastQueen.print;
end;

5.3.2. : C++

C++
. Queen.
. , Object Pascal
C++ ,
.
class Queen
{
public:
//
Queen (int, Queen *);
//
bool findSolution();

PDF created with pdfFactory Pro trial version www.pdffactory.com

bool advance();
void print();
private:
//
int row;
const int column;
Queen *neighbor;
//
bool canAttack (int, int);
};

Delphi Pascal, initialize


. .
. column const.
. ;
( ), .
,
- .
lastQueen . null . ,
. ,
neighbor,
; lastQueen .
void main()
{
Queen *lastQueen = 0;
for (int i = 1; i <= 8; i++)
{
lastQueen = new Queen(i, lastQueen);
if (! lastQueen -> findSolution())
cout << "no solution\n";
}
lastQueen -> print();
}

, .
A.

column, -
, const.
neighbor, .
Queen::Queen(int col, Queen *ngh)
: column(col), neighbor(ngh)
{
row = 1;
}

neighbor ,
, , .
findSolution.

Object Pascal .

PDF created with pdfFactory Pro trial version www.pdffactory.com

bool Queen::findSolution()
{
while (neighbor && neighbor->canAttack(row, column))
if (! advance())
return false;
return true;
}

advance ,
. ,
findSolution, .
bool Queen::advance()
{
if (row < 8)
{
row++;
return findSolution();
}
if (neighbor && ! neighbor->advance())
return false;
row = 1;
return findSolution();
}

5.3.3. : Java
Java C++. Java
, public private .
Queen; .
class Queen
{
//
private int row;
private int column;
private Queen neighbor;
//
Queen (int c, Queen n)
{
//
row = 1;
column = c;
neighbor = n;
}
public boolean findSolution()
{
while (neighbor != null &&
neighbor.canAttack(row, column))
if (! advance ())
return false;
return true;
}
public boolean advance()
{
. . .
}
private boolean canAttack(int testRow, int testColumn)
{
. . .
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

public void paint (Graphics g)


{
. . .
}
}

C++ Java
Queen, Queen. ,
neighbor, .
Java ,
,
. paint , .
class Queen
{
. . .
public void paint (Graphics g)
{
// x, y
int x = (row 1) * 50;
int y = (column 1) * 50;
g.drawLine(x+5, y+45, x+45, y+45);
g.drawLine(x+5, y+45, x+5, y+5);
g.drawLine(x+45, y+45, x+45, y+5);
g.drawLine(x+5, y+35, x+45, y+35);
g.drawLine(x+5, y+5, x+15, y+20);
g.drawLine(x+15, y+20, x+25, y+5);
g.drawLine(x+25, y+5, x+35, y+20);
g.drawLine(x+35, y+20, x+45, y+5);
g.drawLine(x+20, y+20, 10, 10);
//
if (neighbor != null)
neighbor.paint(g);
}
}

, :

Java , .
8,
Applet . , init
, paint .
mouseDown, ,
.
QueenSolver :
public class QueenSolver extends Applet
{ private Queen lastQueen;
public void init()
{ lastQueen = null;
for (int i = 1; i <= 8; i++)

PDF created with pdfFactory Pro trial version www.pdffactory.com

lastQueen = new Queen(i, lastQueen);


lastQueen.findSolution();

}
}
public void paint(Graphics g)
{ //
for (int i = 0; i <= 8; i++)
{
g.drawLine(50 * i, 0, 50 * i, 400);
g.drawLine(0, 50 * i, 400, 50 * i);
}
//
lastQueen.paint(g);
}
public boolean mouseDown(java.awt.Event evt, int x,
int y)
{
//
lastQueen.advance();
repaint();
return true;
}
}

, public,
.

5.3.4. : Objective-C
Queen :
@interface Queen : Object
{ /* */
int row;
int column;
id neighbor;
}
/* */
- (void) initialize: (int) c neighbor: ngh;
- (int) advance;
- (void) print;
- (int) canAttack: (int) testRow column:
(int) testColumn;
- (int) findSolution;
@end

: , -.
id. , ,
.

Objective-C . , , ,
(, C++ Object
Pascal). , . C++
, ,
.
(sentinel) .
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

;
, , .
-
, ,
.
? ,
. -, ,
;
, .
print.
, .
,
:
@implementation SentinelQueen : Object
- (int) advance
{
/* */
return 1;
}
- (int) findSolution
{
/* */
return 1;
}
- (void) print
{
/* */
}
- (int) canAttack: (int) testRow column:
(int) testColumn;
{
/* */
return 0;
}
@end

, SentinelQueen, .
, ,
.
Queen
,
. , canAttack:
- (int) canAttack: (int) testRow column:
(int) testColumn
{ int columnDifference;
/* */
if (row == testRow)
return 1;
columnDifference = testColumn column;
if ((row + columnDifference == testRow) ||
(row columnDifference == testRow))
return 1;
return [ neighbor canAttack:testRow column:

PDF created with pdfFactory Pro trial version www.pdffactory.com

testColumn ];}


self:
- (void) initialize: (int) c neighbor: ngh
{
/* */
column = c;
neighbor = ngh;
row = 1;
}
- (int) findSolution
{
/* , */
while ([neighbor canAttack: row and: column ])
if (! [self advance])
return 0; /* false */
return 1;
/* true */
}

5.3.5. : Smalltalk
Smalltalk
Objective-C. , Smalltalk ,
, (sentinel) .
, , .
SentinelQueen
( Queen), :
advance
"
false
canAttack: row
"
false
result
"
List new

"
column: column
"
"

Objective-C Smalltalk ,
Smalltalk , .
Smalltalk
. ,
.
Queen Object. Queen
: row (), column () neighbor ().
setColumn : neighbor:.
setColumn: aNumber neighbor: aQueen
" "
column := aNumber.
neighbor := aQueen.

PDF created with pdfFactory Pro trial version www.pdffactory.com

row := 1.

canAttack Objective-C
:
canAttack: testRow column: testColumn |
columnDifference |
columnDifference := testColumn column.
((row = testRow) or:
[ row + columnDifference = testRow]) or:
[ row columnDifference = testRow])
ifTrue: [ true ].
neighbor canAttack: testRow column: testColumn

, Smalltalk
ifFalse, advance:
advance
" "
(row < 8)
ifTrue: [ row := row + 1. self findSolution].
" , "
(neighbor advance)
ifFalse: [ false ].
" 1 "
row := 1.
self findSolution

while Smalltalk ,
:
findSolution
[ neighbor canAttack: row column: column ]
whileTrue: [ self advance ifFalse: [ false ] ].
true

.
, result:
result
neighbor result; addLast: row

,
Queen , , Object:
solvePuzzle | lastQueen |
lastQueen := SentinelQueen new.
1 to: 8 do: [:i | lastQueen := (Queen new)
setColumn: i neighbor: lastQueen.
lastQueen findSolution ].
lastQueen result

, ,
Smalltalk [Budd 1987].

PDF created with pdfFactory Pro trial version www.pdffactory.com

1. , ,
. ?
? ?
2. , ObjectiveC Smalltalk
findSolution,
findSolution advance?
3. , N .
: N N N?
? , N, (, N=2
N=3). ?
?
4. ,
,
.
?

6: :
.
Object Pascal Macintosh 1 . ,
,
.

6.1.
,
, 15 1 .
, . ,
. , ,
, , , , .
1

PowerPC Macintosh
CodeWarrior Pascal 1.1. PowerPC ,
Ball.update , draw. ,
, . , ,
.

,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

6.2.
, ,
. ,
1 .
,
. , ,
. ,
, , .
, ,
,
. ,
.

6.2.1. Wall ()
Wall.
:
1

, . ,
; ,
. , ,
. ,
, 7. ,
, ;
. .
15.

Wall = object
(* *)
link : Wall;
region : Rect;

PDF created with pdfFactory Pro trial version www.pdffactory.com

(* *)
convertFactor : real;
(* *)
procedure initialize
(left, top, right, bottom : integer; cf : real);
(* *)
procedure draw;
(* , *)
procedure hitBy (aBall : Ball);
end;

link () Wall.
(region) (convert factor):
procedure Wall.initialize
(left, top, right, bottom : integer; cf : real);
begin
(* convertFactor *)
convertFactor := cf;
(* *)
SetRect (region, left, top, right, bottom);
end;

.
Macintosh:
procedure Wall.draw;
begin
PaintRect (region);
end;

, .
, convertFactor .
( convertFactor , pi, ,
.)
.
procedure Wall.hitBy (aBall : Ball);
begin
(* *)
aBall.setDirection(convertFactor aBall.direction);
end;

6.2.2. Hole ()
Hole () :
Hole = object
(* *)
link : Hole;
region : Rect;
(* *)
procedure initialize (x, y : integer);
(* *)
procedure draw;
(* , *)
procedure hitBy (aBall : Ball);
end;

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
:
procedure Hole.initialize (x, y : integer);
begin
(* x, y *)
SetRect(region, x-5, y-5, x+5, y+5);
end;
procedure Hole.draw;
begin
PaintOval (region);
end;

. .
( CueBall),
.
.
procedure Hole.hitBy (aBall : Ball);
begin
(* ; *)
aBall.energy := 0.0;
aBall.erase;
(* *)
if aBall = CueBall then
aBall.setCenter(50.100);
else begin
saveRack := saveRack + 1;
aBall.setCenter (10 + saveRack * 15, 250);
end;
(* *)
aBall.draw;
end;

6.2.3. Ball ()
,
:
Ball = object
(* *)
link : Ball;
region : Rect;
direction : real; (* *)
energy : real;
(* *)
procedure initialize (x, y : integer);
(* *)
procedure draw;
procedure erase;
procedure update;
procedure hitBy (aBall : Ball);
procedure setDirection (newDirection : real);
(* x, y *)
function x : integer;
function y : integer;
end;

(link) (region),
, : direction (),

PDF created with pdfFactory Pro trial version www.pdffactory.com

, energy (), .
, , .
.
procedure Ball.initialize (x, y : integer);
begin
SetRect (region, x-5, y-5, x+5, y+5);
setDirection (0.0);
energy := 0.0;
end;

, , ,
.
procedure Ball.draw;
begin
if self = CueBall then
(* *)
FrameOval (region);
else
(* *)
PaintOval (region);
end;
procedure Ball.erase;
begin
EraseRect (region);
end;

update .
, , , .
ballMoved true, -
. , .
; , .
, 7,
.
procedure Ball.update;
var
hptr : Hole;
wptr : Wall;
bptr : Ball;
dx, dy : integer;
theIntersection : Rect;
begin
if energy > 0.5 then
begin
ballMoved := true;
(* *)
erase;
(* *)
energy := energy 0.05;
(* *)
dx := trunc(5.0 * cos(direction));
dy := trunc(5.0 * sin(direction));
offsetRect(region, dx, dy);
(* *)
draw;
(* , *)
hptr := listOfHoles;
while (hptr <> nil) do

PDF created with pdfFactory Pro trial version www.pdffactory.com

if SectRect (region, hptr.region, theIntersection)


then
begin
hptr.hitBy(self);
hptr := nil;
end
else
hptr := hptr.link;
(* , *)
wptr := listOfWalls;
while (wptr <> nil) do
if SectRect (region, wptr.region, theIntersection)
then
begin
wptr.hitBy(self);
wptr := nil;
end
else
wptr := wptr.link;
(* , *)
bptr := listOfBalls;
while (bptr <> nil) do
if SectRect (region, bptr.region, theIntersection)
then
begin
bptr.hitBy(self);
bptr := nil;
end
else
bptr := bptr.link;
end;
end;

, .
.
procedure Ball.hitBy (aBall : Ball);
var
da : real;
begin
(* *)
aBall.energy := aBall.energy / 2;
(* *)
energy := energy + aBall.energy;
(* *)
setDirection(hitAngle(self.x aBall.x,
self.y aBall.y);
(* *)
da := aBall.direction direction;
aBall.setDirection (aBall.direction + da);
end;
function hitAngle (dx, dy : real) : real;
const
PI = 3.14159;
var
na : real;
begin
if (abs(dx) < 0.05) then
na := PI / 2;
else
na := arctan (abs(dy / dx));
if (dx < 0) then
na := PI na;

PDF created with pdfFactory Pro trial version www.pdffactory.com

if (dy < 0) then


na := -na;
hitAngle := na;
end;

6.3.
.
.
:
procedure mouseButtonDown (x, y : integer);
var
bptr : Ball;
begin
(* *)
CueBall.energy := 20.0;
(* *)
CueBall.setDirection(hitAngle (CueBall.x x,
CueBall.y y));
(* , *)
ballMoved := true;
while ballMoved do
begin
ballMoved := false;
bptr := listOfBalls;
while bptr <> nil do
begin
bptr.update;
bptr := bptr.link;
end;
end;
end;

.
.
, .
,
. ,
, .
.

6.4.
1 , 7 ,
. ,
. ,

.

. : ,
. :
GraphicalObject = object
(* *)

PDF created with pdfFactory Pro trial version www.pdffactory.com

link : GraphicalObject;
region : Rect;
(* *)
procedure setRegion (left, top, right, bottom :
integer);
(* , *)
procedure draw;
procedure erase;
procedure update;
function intersect (anObj : GraphicalObject) :
boolean;
procedure hitBy (anObj : GraphicalObject);
end;

setRegion ,
. draw update ,
. erase , .
intersect true, -
. , hitBy .
, , ,
, Ball , ,
GraphicalObject:
procedure GraphicalObject.setRegion
(left, top, right, bottom : integer);
begin
SetRect(region, left, top, right, bottom);
end;
procedure GraphicalObject.draw;
begin
(* *)
end;
procedure GraphicalObject.erase;
begin
EraseRect (region);
end;
procedure GraphicalObject.update;
begin (* *)
end;
procedure GraphicalObject.hitBy(anObject :
GraphicalObject);
begin (* *)
end;
function GraphicalObject.intersect
(anObject : GraphicalObject) : boolean;
var
theIntersection : Rect;
begin
intersect := SectRect
(region, anObject.region, theIntersection);
end;

Ball, Wall Hole GraphicalObject,


,
:
Hole = object (GraphicalObject)
(* *)
procedure initialize (x, y : integer);
(* *)

PDF created with pdfFactory Pro trial version www.pdffactory.com

procedure draw; override;


(* , *)
procedure hitBy (anObject : GraphicalObject);
override;
end;

hitBy Ball.
:
procedure Wall.hitBy (anObj : GraphicalObject);
var
aBall : Ball;
begin
if Member (anObj, Ball) then
begin
aBall := Ball(anObj);
aBall.setDirection(convertFactor aBall.direction);
end;
end;

CueBall Ball,
.
CueBall = Object (Ball)
procedure draw; override;
end;
procedure Ball.draw;
begin
(* *)
PaintOval (region);
end;
procedure CueBall.draw;
begin
(* *)
FrameOval (region);
end;

,
. , , :
procedure drawBoard;
var
gptr : GraphicalObject;
begin
SetPort (theWindow);
gptr := listOfObjects;
while gptr <> nil do begin
gptr.draw;
gptr := gptr.link;
end;
end;

draw .
, Ball,
Wall Hole. ,
, .
14.

PDF created with pdfFactory Pro trial version www.pdffactory.com

Ball.update, , , .
.

1. , ,
.
?
2. , .
?
3. , ,
. ?
4. ,
, , .
- ,
, ,
, .
. ,
. ? ?

7:

-
.
5 6.
-
,
.
() ().

7.1.
.
,
, , . ,
, .
; , ,
. . ,
Shopkeeper,
( ) (Florist)
Shopkeeper, .
, ,
, ( ) ,
.
, , . ,
( ) ,
, , .

, ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
.
, ,
. , Dog
Mammal, Mammal
Animal, Dog
Mammal, Animal.

, ,
. , Platypus
, Mammal,
. .
11.

7.2. ,
, ,
, . :


.
,
( ),
. ,
.


.

, ,
.
.
.
, A B , B
A (, ),
B
A , .
10,
, , ,
.
6. 6.4
:
procedure drawBoard;
var
gptr : GraphicalObject;
begin
SetPort (theWindow);
(* *)

PDF created with pdfFactory Pro trial version www.pdffactory.com

gptr := listOfObjects;
while gptr <> nil do
begin
gprt.draw;
gptr := gptr.link;
end;
end;

listOfObjects
. gptr ,
,
. gptr , ,
. draw
gptr,
GraphicalObject. , ,

, ;
.

7.2.1.
(, C++ Object
Pascal) ,
(Smalltalk Objective-C). ,

,
. , (,
)
, ,
.
,
,
.
10, 14.

7.3.
-.
. ,
, . ,
, .
1 ,
1

[Halbert 1987], .
[Meyer 1988a]. , .

7.3.1. ( )

.
,
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

. (
) ,
.
. Window
(, , . .).
TextEditWindow
,
, .
TextEditWindow , (,
, Window ,
),
.

7.3.2.
,
,
.
, .

,
.

,
, , , , .

.
, , 6
GraphicalObject ,
.
Ball, Wall Hole ,
.
,

.

7.3.3.
,
.
,
(is-a relationship).
, Smalltalk
, Dictionary ().
; . ,
, ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
. , SymbolTable (
) Dictionary ,
.
,
.
,
,
.

, , ,
. , ,
. ,
.
, 1 .

,
( , ). ,
,
. Smalltalk
.
8.
, C++ :
,
.

7.3.4.

.
1

.
, , , .
, , ,
.
class Storable
{
void writeByte(unsigned char);
}
class StoreMyStruct : public Storable
{
void writeStruct (MyStruct &aStruct);
}

.
,
, .
,
Window - .

PDF created with pdfFactory Pro trial version www.pdffactory.com

ColoredWindow. ,
.
, .
,

. , ,
- .
, ,
.
.

7.3.5.

,
. ,
,
.
,
.

StringSet, Set
.
, ,
, .
, .
,
,
, .

7.3.6.
,
, . , ,
,
, .
, Deque
(double-ended-queue, ).
Deque,
Stack, .
, ,
Stack Deque
, .
,
. (,

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
.)

, ,
, .

7.3.7.
,
,
, . ,
,
. , , , Mouse,
, Tablet,
, .
,
, .
, ,
, PointingDevice, .
,
.

7.3.8.
,
. , ,
, , , .

; ,
.

7.3.9.

:
. ,
. ,
.
. ,
, .
. ,
, (
).
.
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
, .
.
.
. ,
.
.
. ;
.

7.4.
,
.
, ,
( Object,
Smalltalk Objective-C), ,
.
,
( Object) . ,
.
, .

,
. , ,
, .

( 12).

, .
,
, .
,
.

7.4.1. Object Pascal


Object Pascal Apple

object. , ,
Ball, Wall Hole GraphicalObject. ,
7.1.
7.1. Object Pascal Apple
type

PDF created with pdfFactory Pro trial version www.pdffactory.com

GraphicalObject = object
(* *)
region : Rect;
link : GraphicalObject;
(* *)
procedure draw;
procedure update;
procedure hitBy(aBall : Ball);
end;
Ball = object(GraphicalObject)
(* *)
direction : real;
energy : real;
(* *)
procedure initialize (x, y : integer);
(* *)
procedure draw; override;
procedure update; override;
procedure hitBy(aBall : Ball); override;
(* , *)
procedure erase;
procedure setCenter(newx, newy : integer);
function x : integer;
function y : integer;
end;

, ,
. , ,
override, draw.

.
Object Pascal Borland ( Delphi)
. -, ,
object class, .
TObject . -,
override virtual
, C++. ,
, 7.2.
override ,
, .
10.
7.2. Delphi Pascal
type
GraphicalObject = class(TObject)
(* *)
region : Rect;
link : GraphicalObject;
(* *)
procedure draw; virtual;
procedure update; virtual;
procedure hitBy(aBall : Ball); virtual;
end;
Ball = class(GraphicalObject)
(* *)
direction : real;
energy : real;

PDF created with pdfFactory Pro trial version www.pdffactory.com

(* *)
procedure initialize (x, y : integer);
(* *)
procedure draw; override;
procedure update; override;
procedure hitBy(aBall : Ball); override;
(* , *)
procedure erase;
procedure setCenter(newx, newy : integer);
function x : integer;
function y : integer;
end;

Delphi Pascal Apple


Object Pascal .
( Objective-C,
C++; . 21). ,
, . dynamic virtual
, . ,
,
. ,
.

7.4.2. Smalltalk
3,
Smalltalk.
, .
.
List subclass: #Set
instanceVariables: #()
classVariables: #()

, Object,
. Object
.
, . .
Smalltalk ,
.
, .

7.4.3. Objective-C
Smalltalk, Objective-C
.
, .
, Card Object:
@interface Card : Object
{
. . .
}
. . .
@end

PDF created with pdfFactory Pro trial version www.pdffactory.com

Smalltalk, Object,
. Object
. .
,
. Smalltalk, , ,
, .

7.4.4. C++
Smalltalk Objective-C C++
.
public,
. TablePile CardPile,
:
class TablePile : public CardPile
{
. . .
};

public private,
, ,
. 11.
, -
,
. ,
.

.
:
TablePile::TablePile (int x, int y, int c)
: CardPile(x,y) //
{
//
. . .
}

C++ ,
.
.
public () private (),
, , .
, ,
protected (). ,
, .
virtual ,
, , ,
. (
, .)

PDF created with pdfFactory Pro trial version www.pdffactory.com

C++ ,
, .
11.

7.4.5. Java
Java
. extends,
:
class window
{
// . . .
}
class textEditWindow extends window
{
// . . .
}

, (, C++,
). ,
, .
, , ,
. C++, protected
, ,
.
Object. ,
Object. , window,
, :
class window extends Object
{
// . . .
}

.
, .
. ,
, / .
public interface Storing
{ void writeOut(Stream s);
void readFrom(Stream s);
}

. ,
. , ,
. ,
, ,
, :
public class BitImage implements Storing
{ void writeOut (Stream s)
{
// . . .

PDF created with pdfFactory Pro trial version www.pdffactory.com

};
void readFrom (Stream s)
{
// . . .
};
};

Java (
), ,
( c).
, C++
, Java
. ,
, extend.
Java
abstract. abstract,
. ,
. abstract,
. , abstract
, ,
:
abstract class storable
{
public abstract writeOut();
}

, final ,
, , .
, , ,
:
final class newClass extends oldClass
{
. . .
}

7.5.

.

7.5.1.

. , .
,
, ,
. - ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com


( ,
) , .

7.5.2.
-
. -,
( [Cox 1986] software-IC,
, ).
, ,
,
. , Set Array
Collection.
.
.

7.5.3.
, ,
. ,
, , , .
,
.

7.5.4.
1 ,
() . :
.
,
.

7.5.5.

, , ,
.
, ,
. (), ,
, . .
,
.

7.5.6.
,
. ,
, .
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
, ,
, ,
.
,
. (-
),
.

,
.
.

7.5.7.
, ,
.
, .

.
.

7.6.
-
, .
- ,
.

7.6.1.
,
, . ,
, ,
.
1 . -,
. -,
. ,
,
. , ,
, , , , ,
, .

7.6.2.

. .
,
.

, 2 .

PDF created with pdfFactory Pro trial version www.pdffactory.com

7.6.3.
,
, . ,
,
. ,

10 .
.
(, Smalltalk)
(C++, ). , ,
- .
, C++,
,
. (
) (inline) .
Delphi Pascal ,
dynamic,
virtual,
.
, .

7.6.4.
-
,

1


: ( , )
, - , [Wulf 1972].
2

, , .
, , :
. 350
, , .
CP/M - 1,2 ...
,
; ,
,
. . .

. , ,
.
-, -. .

1. ,
, - (,
Pascal C). ?

PDF created with pdfFactory Pro trial version www.pdffactory.com

? ?
.
2. , , ,
, . ?
, , (C++
Object Pascal),
.
3. : (
), ( ,
), ( ).
, ,
? ,
( )? ,
. .
4.
?
, -
. ,
- ! , ,
,
.
, (+, , /, *, sqrt, sin, cos . .)
Real , . , ,
.

8: :


. 3 4 ,
, Card.
Java.
CardPile,
.
, CardPile
. ,

.

8.1. Card
Card.
.
Card ( 8.1) .
, ( )
, - .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ,
.
( Java final
static) , .
.
,
. .
, . ( ,
,
(mutator).)
8.1. card
class Card
{
//
Card (int sv, int rv)
{
s = sv; r = rv; faceup = false;
}
//
public int rank ()
{ return r; }
public int suit ()
{ return s; }
public boolean faceUp()
{ return faceup; }
public void flip()
{ faceup = ! faceup; }
public int color()
{
if (suit() == heart || suit == diamond)
return red;
return black;
}
public void draw (Graphics g, int x, int y)
{
...
}
//
final static int width
= 50;
final static int heigth
= 70;
final static int red
= 0;
final static int black
= 1;
final static int heart
= 0;
final static int spade
= 1;
final static int diamond
= 2;
final static int club
= 3;
//
private
private
private

boolean faceup;
int r;
int s;

, , (
), . flip()
, ,
faceup, . draw() :

PDF created with pdfFactory Pro trial version www.pdffactory.com

, Java.
, Graphics,
, .
Graphics,
, .
, .
, .
.
8.2.
,
,
. , , . ,
,
. , , ,
, draw
.
8.2.
class Card
{ ...
public void draw (Graphics g, int x, int y)
{ String names[] = {"A", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "J", "Q", "K"};
// ,
g.clearRect(x, y, width, height);
g.setColor(Color.black);
g.drawRect(x, y, width, height);
//
if (faceUp) //
{ if (color() == red) g.setColor(Color.red);
else g.setColor(Color.blue);
g.drawString(names[rank()], x+3, y+15);
if (suit() == heart)
{ g.drawLine(x+25, y+30, x+35, y+20);
g.drawLine(x+35, y+20, x+45, y+30);
g.drawLine(x+45, y+30, x+25, y+60);
g.drawLine(x+25, y+60, x+5, y+30);
g.drawLine(x+5, y+30, x+15, y+20);
g.drawLine(x+15, y+20, x+25, y+30);
}
else if (suit() == spade )
{ ... }
else if (suit() == diamond )
{ ... }
else if (suit() == club )
{ g.drawOval(x+20, y+25, 10, 10);
g.drawOval(x+25, y+35, 10, 10);
g.drawOval(x+15, y+35, 10, 10);
g.drawOval(x+23, y+45, x+20, y+55);
g.drawOval(x+20, y+55, x+30, y+55);
g.drawOval(x+30, y+55, x+27, y+45);
}
}
else //
{
g.setColor(Color.yellow);
g.drawLine(x+15, y+5, x+15, y+65);

PDF created with pdfFactory Pro trial version www.pdffactory.com

g.drawLine(x+35, y+5,
g.drawLine(x+5, y+20,
g.drawLine(x+5, y+35,
g.drawLine(x+5, y+50,

x+35,
x+45,
x+45,
x+45,

y+65);
y+20);
y+35);
y+50);

}
}
}

8.2.
.
( ),
.
6 (, ,
). ,
6, , -
, , ,
( ) .
,
.
. LinkedList
, , .
List.
List. . 8.3.

, , ,
-, Object. ,
Object ( , value Link),
.
LinkedList : ,
, ,
.
8.3. Link LinkedList
class Link
{
public Link (Object newValue, Link next)
{
valueField = newValue; nextLink = next;
}
public Object value ()
{ return valueField; }
public Link next ()
{ return nextLink; }
private Object valueField;
private Link nextLink;
}
class LinkedList
{
public LinkedList ()

PDF created with pdfFactory Pro trial version www.pdffactory.com

{ firstLink = null; }
public void add (Object newValue)
{ firstLink = new Link(newValue, firstLink);
public boolean empty ()
{ return firstLink == null; }
public Object front ()
{
if (firstLink == null)
return null;
return firstLink.value();
}
public void pop ()
{
if (firstLink != null)
firstLink = firstLink.next();
}
public ListIterator iterator()
{ return new ListIterator (firstLink); }
private Link firstLink;


, ,
(
Link). 16,

, .

. 8.4.
:
ListIterator itr = aList.iterator();
while (! Itr.atEnd() )
{
... do something list itr.current() ...
itr.next();
}

,

.
8.4. ListIterator
class ListIterator
{
public ListIterator (Link firstLink)
{
currentLink = firstLink;
}
public boolean atEnd ()
{
return currentLink == null;
}
public void next ()
{
if (currentLink != null)
currentLink = currentLink.next();
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

public Object current ()


{
if (currentLink == null)
return null;
return currentLink.value();
}
private Link currentLink:
}

8.3.
, ,
( Klondike). , ,
, ,
. , ,
[Morehead 1949].
.
. 8.1. 52
. (tableau) 28 7 . 1
, 2 . . 7.
; .

. 8.1.
( (foundations))
. ,
. 52 .
, , (deck).
,
(discard pile).
. , . ,
.

. ,
(
). , ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

.

( (build)) .
,
.
,
. .
, ,
.
,
. ,
, .
,
.
:
.

8.4.
, ,
. , ;
.
, CardPile,
. CardPile 8.5.
,
. .
protected
( ).
top(), pop() empty(), ,
, LinkedList.
addCard(Card). . :
front() Object.
Card top() pop().

8.5. CardPile
class CardPile
{ CardPile (int x1,int y1)
{
x = x1; y = y1; cardFile = new LinkedList();
}
public Card top()
{
return (Card) cardList.front();
}
public boolean empty()

PDF created with pdfFactory Pro trial version www.pdffactory.com

{
return cardList.empty();
}
public Card pop()
{
Card result = (Card) cardList.front();
cardList.pop();
return result;
}
//
public boolean includes (int tx, int ty)
{
return x <= tx && tx <= x + Card.width &&
y <= ty && ty <= y + Card.height;
}
public void select (int tx, ty) { }
public void display (Graphics G)
{
g.setColor(Color.black);
if (cardList.empty())
g.drawRect(x, y, Card.width, Card.height);
else
top().draw(g, x, y);
}
public boolean canTake (Card aCard)
{ return false; }
//
protected int x;
protected int y;
protected LinkedList cardList;
}


. .
, canTake(Card) ,
. ,
, ( ,
). , ,
1) 2)
, 3)
, .
, CardPile,
:
includes
, , ,
.
. DeckPile ,
.
canTake
, . DeckPile
SuitPile ,
.
.
addCard

PDF created with pdfFactory Pro trial version www.pdffactory.com

( ).
DiscardPile , ,
.
display
.
, TablePile
.
.
.
, .
select
. ,
.
, TablePile, DeckPile
DiscardPile
, .
.
, 25 ,
. , 13 .
, ,
.

includes
canTake
addCard
display
select

CardPile
*
*
*
*
*

SuitPile

DeckPile

DiscardPile

TablePile
*
*

*
*

*
*

8.4.1. SuitPile
CardPile,
-
. SuitPile.
8.6. ,
.
8.6. SuitPile
class SuitPile extends CardPile
{
SuitPile (int x, int y)
{
super(x, y);
}
public boolean canTake (Card aCard)
{
if (empty())
return aCard.rank() == 0;
Card topCard = top();
return (aCard.suit() == topCard.suit()) &&

PDF created with pdfFactory Pro trial version www.pdffactory.com

(aCard.rank() == 1 + topCard.rank());
}
}

SuitPile .
, CardPile.
super, . canTake
, . ,
,
, (,
).
SuitPile , .
. ,
.
.

8.4.2. DeckPile
DeskPile ( 8.7) .
.
52 ,
. random Java
0 1.
.
select , DeskPile.
, .
.
Java .
(, ),
static. 20,
, .
.
Solitare, .
,
. select ( 8.8),
Solitare.discardPile
.
8.7. DeckPile
class DeckPile extends CardPile
{
DeckPile (int x, int y)
{
//
super(x, y);
//
//
CardPile pileOne = new CardPile(0, 0);
CardPile pileTwo = new CardPile(0, 0);
int count = 0;

PDF created with pdfFactory Pro trial version www.pdffactory.com

for (int i = 0; i < 4; i++)


{
pileOne.addCard(new CArd(i, j));
count++;
}
//
for (; count > 0; count--)
{ int limit = ((int)(Math.random() * 1000))
% count;
//
for (int i = 0; i < limit; i++)
pileTwo.addCard(pileOne.pop());
//
addCard(pileOne.pop());
//
while (! pileTwo.empty())
pileOne.addCard(pileTwo.pop());
}
}
public void select(int tx, int ty)
{
if (empty())
return;
Solitaire.discardPile.addCard(pop());
}
}

8.4.3. DiscardPile
DiscardPile (. 8.8) ,
. select
, CardPile. (
) ,
- .
, .
addCard .
. ,
, . ,
, .
,
super , .
.
,
, ,
. - super;
. 11
.
8.8. DiscardPile
class discardPile extends CardPile
{
DiscardPile (int x, int y)
{
super (x, y);

PDF created with pdfFactory Pro trial version www.pdffactory.com

}
public void addCard (Card aCard)
{
if (! aCard.faceUp())
aCard.flip();
super.addCard(aCard);
}
public void select (int tx, int ty)
{
if (empty())
return;
Card topCard = pop();
for (int i = 0; i < 4; i++)
{
if (Solitaire.suitPile[i].canTake(topCard))
{
Solitaire.suitPale[i].addCard(topCard);
return;
}
}
for (int i = 0; i < 7; i++)
{
if (Solitaire.tableau[i].canTake(topCard))
{
Solitaire.tableau[i].addCard(topCard);
return;
}
}
// ,
//
addCard(topCard);
}
}

8.4.4. TablePile
CardPile ,
TablePile. 8.9 8.10.
:

( )
, . ,
, ,
. .
( canTake),


, .
( includes)
, .
, TablePile .
,
. ,
- , - .
, .
,
.
,

PDF created with pdfFactory Pro trial version www.pdffactory.com

( private) ,
, .
.

8.5.
( 5),
Java Applet.
Applet, . Solitare,
, 8.11.
, ,
, static.
init 1.
Java ,
. Java : ,
. ,
8.9. TablePile, I
class TablePile extends CardPile
{
TablePile (int x, int y, int c)
{
//
super(x, y);
//
for (int i = 0; i < c; i++)
{
addCard(Solitaire.deckPile.pop());
}
//
top.flip();
}
public boolean cantake (Card aCard)
{
if (empty())
return aCard.rank() == 12;
Card topCard = top();
return (aCard.color() != topCard.color()) &&
(aCard.rank() == topCard.rank() 1);
}
public boolean includes (int tx, int ty)
{
//
return x < = tx && tx<= x + Card.width &&
y <= ty;
}
private int stackDisplay
(Graphics g, ListIterator itr)
{
int localy;
if (itr.atEnd())
return y;
Card aCard = (Card) itr.current;
itr.next;
localy = stackDisplay(g, itr);

PDF created with pdfFactory Pro trial version www.pdffactory.com

aCard.draw(g, x, localy);
return localy + 35;
}
...

8.10. TablePile, II
class TablePile extends CardPile
{
...
public void select (int tx, int ty)
{
if (empty())
return;
// ,
Card topCard = top();
if (! topCard.faceUp())
{
topCard.flip();
return;
}
// ,
topCard = pop();
for (int i = 0; i < 4; i++)
{
if (Solitaire.suitPile[i].canTake(topCard))
{
Solitaire.suitPile[i].addCard(topCard);
return;
}
}
//
for (int i = 0; i < 7; i++)
{
if (Solitaire.tableau[i].canTake(topCard))
{
Solitaire.tableau[i].addCard(topCard);
return;
}
}
//
addCard(topCard);
}
public void display (Graphics g)
{
stackDisplay(g, cardList.iterator());
}
}

. 8.11. Solitaire
public class Solitaire extends Applet
{
static DeckPile deckPile;
static DisacrdPile discardPile;
static TablePile tableau [ ];
static SuitPile suitPile [ ];
static CardPile allPiles [ ];
public void init()
{

PDF created with pdfFactory Pro trial version www.pdffactory.com

//
allPiles = new CardPile[13];
suitPile = new SuitPile[4];
tableau = new TablePile[7];
//
allPiles[0] = deckPile = new DeckPile(335, 5);
allPiles[1] = discardPile =
new DiscardPile(268, 5);
for (int i = 0; i < 4; i++)
{ allPiles[2+i] = suitPile[i] =
new SuitPile(15 + 60 * i, 5);
}
for (int i = 0; i < 7; i++)
{ allPiles[6+i] = tableau[i] =
new TablePile(5 + 55 * i, 80, i+1);
}
}
public void paint(Graphics g)
{
for (int i = 0; i < 13; i++)
{ allPiles[i].display(g); }
}
public boolean mouseDown(Event evt, int x, int y)
{
for (int i = 0; i < 13; i++)
{
if (allPiles[i].includes(x, y))
{ allPiles[i].select(x, y);
repaint();
return true;
}
}
return true;
}
}

, ;
.
(, allPiles, ).
new ,
.
DeskPile. ,
52 .
DiscardPile .
SuitPile,
TablePile. ,
.
allPiles 13 . ,
, ,
.
. , allPiles
CardPile,
.
,
. ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, . ,
. , ,
. , ,
select includes, .
, ,
, ,
(, ).

8.6.
, ,
.
:

select TablePile
(: ,
,
).
, .
,
.
. :
( ,
), ,
.

1. .
, ?
. :
a.
, ;
b. ,
.
, ,
.

2. . ,
.
a. ,
(, )
.
.
b. .

PDF created with pdfFactory Pro trial version www.pdffactory.com

c.
. ,
.
,
. (
, .)
d. , ,
. (
,
.)
e.
(, , ), .
3. thumb and pouch ,

. ,
, .
. ( [Morehead 1949],
Klondike 1 30, thumb and pouch 1 4.) ,
.

9:

- ,

. , , ,
-
[Cox 1986].
(,
). ,
,
.
,
.
.
,
, , , ,
.
, , 18.

9.1.
,
, . 8,
,
, . ,
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

6.
, ,
GraphicalObject,
,
GraphicalObject.
, ,
.
?

? ,
- ,
( is-a).

9.1.1.
,
. ,
(is-a has-a).
,
. ,
, ,
. , ,
,
( Florist
Shopkeeper, Dog Mammal,
Ball GraphicalObject,
. .).
.
, X Y,
X Y. ,
, , X Y
.
, ,
,
. , Car Engine,
, , Car Engine Engine
Car. Car Vehicle,

MeansOtTransportation 1.
1. X Y
X Y, -
, X Y
. X Y ,
X , , ,
, . . Y, . ,
:
(

PDF created with pdfFactory Pro trial version www.pdffactory.com

),
( ),
, X Y.
. .
, ,
X Y .
.
. ,
,
.

9.2. :
, set
List.
List . , List
:
class List
{
public:
//
List ();
//
void addToFront
int firstElement
int length
int includes
int remove
...
};

(int);
();
();
(int);
(int);

,
, , ,
, .
, ,
, ,
.

9.2.1.
,
. , .

,
. , Set
, theData, List.
Class Set
{
public:
Set (); //

PDF created with pdfFactory Pro trial version www.pdffactory.com

//
void add
(int);
int
size
();
int
includes
(int)
private: //
List theData;
};

List ,
.
( 4),
.
, List, :
//
Set::Set() : theData()
{
//
}


, . ,
includes ,
:
int Set::size ()
{
return theData.length();
}
int Set::includes (int newValue)
{
return theData.includes(newValue);
}

.
, ,
( ):
void Set::add (int newValue)
{
//
if (! Includes (newValue))
{
//
theData.addToFront(newValue);
};
//
}

,
. List

.
.
List Set
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com


-
, . ,
-.
. Smalltalk
-, Objective-C -,
Java Object Pascal .

9.2.2.

. ,
, . ,
, .
.
,
, .
,
Set. List , ,
Set List.
, , , :
Class Set : public List
{ public:
//
Set();
//
void add (int);
int size ();
};

, .
List .
- .
:
Set::Set() : List()
{
//
}

, ,
- , , ,
includes, List
. ,
List:
void Set::add
(int newValue)
{
// ,
if (! Includes(newValue))
addToFront (newValue);
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
,
, .
, ,
.


7 ,
. ,
, .

9.2.3. C++
C++
.
private public .
,
,
:
Class Set : private List
{
public:
//
Set () : List () { }
//
void add
(int);
int includes
(int x);
{
return List::includes(x);
}
int size
()
{
return List::length();
}
};

, 10, ,
, .
,
. , ,
,
. ,
(, includes ),
, , , -.
( ,
, .)
, (
)

, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

9.3.

, ,
:

. ,
,
. Set ,
,
.
, .

. , ,
,
. , Set
, ( includes)
. List
, .
: , ,
- (
) . - (-)
[Taenzer 1989].
, ,
, . ,
.
( )
, .
. ,

include, remove.

,
. ,
Set List,
, List
addToFront.
, List
, .
,
(, , -)
Set.
, Set List,
.

.
14.
, .
.
, . ,
, , ,
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, , ,
,
, .
, ,
()
. ,
( inline ++
).

, ,
? . ,
, List,
Set?
( Set List), ,
, . .
: , -
.

9.4. : ?
- ,

.
( ,
-
, . .),
. :


, ,
. ,

.
,
.
.
,
.
,
.

,
.
,

. ,
,
.
,
.
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, , , ,
, -.

(, ),
.

,
,
.
, , ,
, , ,
- .
, , ,
, ,
.

.
,
[Carroll 1995, McGregor 1992, Meyer 1994, Goldberg 1995].

,
. ,
List, Array. ,
, , .
.
?
, List
, ,
, .
?
.

10:

-
,
, . 6,
, , GraphicalObject
Ball, Wall Hole.
, 14.
, Pascal C, integer,
, , , ,
. , Window,
GraphicWindow, TextEditWindow -
.
,
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
. ,
, . ,
(, ) , .
.
7.
. .
, . ,
,
- .
,
, 9. , ,
, -
. ,
Smalltalk .
, ,
( Object).
,
.
, Array.
, ,
.
-
(, Object Pascal C++)
. -,
,
. -, ,
. .

, ,
.

10.1.
,
. , ,
. .
,
, ?
,
, ? ,
,
, .

10.1.1.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 10.1.

(. 10.1).
()
(). ,
, .
. 10.1, , mouseDown,
(Window), (TextEditWindow).
,
?
. ,
.
, .
, .

10.1.2.
, ,
, .
? , Y Window X
TextEditWindow, Z
TextEditWindow Y?
.
, , Ball
BlackBall WhiteBall.
,
Ball,

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 10.2. ,
( ) (. 10.2). BlackBall
WhiteBall , .
Ball
, .
BlackBall? : 1) ,
BlackBall, ? 2)
, BlackBall?
BlackBall WhiteBall ,
.
: , , ,
. ., , .
-
,
.
.
, ,
?
, . ,
,
( ).
.

10.2.

. ,
(Object Pascal, C++ Java)
(Smalltalk Objective-C).

10.2.1. Object Pascal

PDF created with pdfFactory Pro trial version www.pdffactory.com

ObjectPascal .
. ,
. ,
, , ,
.
,
. Object Pascal Apple
Member, ,
. Member
( )
( Object Pascal ).
true, - , false
. , Animal. ,
fido, Animal,
Mammal :
if Member (fido, Mammal) then
writeln ('fido is a mammal')
else
writeln ('fido is not a mammal');

, Member true, fido


(, Dog).
Member
. ,
Member , ,
BlackBall WhiteBall, .
Delphi Pascal
is. true,
, . as
.
, .
.
.
if aBall is BlackBall then
bBall := aBall as BlackBall
else
writeln('cannot convert ball to black ball');

( ,
), ClassInfo (
Delphi Pascal ClassInfo).
if aBall.ClassInfo = BlackBall then
...

Object Pascal
, ,
. ,
hasLiveYoung ( ?) Mammal

PDF created with pdfFactory Pro trial version www.pdffactory.com

Platypus,
Platypus, () Mammal.
Object Pascal ,
.
,
. , . phyl
Animal, Mammal.
hasLiveYoung, ,
, Mammal,
Animal.
var
phyl : Animal;
newPlat : Platypus;
begin
new (newPlat);
phyl := newPlat;
(*
if phyl.hasLiveYoung then
...

*)

10.2.2. Smalltalk
Smalltalk . -,
, , .
,
,
.
. class,
, . , fido
, , Dog,
( fido class == Dog ) ifTrue: [ ... ]
, . ,
Mammal Dog. , Smalltalk (
Objective-C) . ,
isMemberOf:,
. , isKindOf:, Member
Object Pascal , ,
, , .
, fido Dog,
, isMemberOf: :
( fido isKindOf: Mammal ) ifTrue: [ ... ]
isKindOf ,
.
, ,
. , ,
. , Seal, Dog
bark (). , fido,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, bark,
:
( fido respondsTo: #bark ) ifTrue: [ ... ]
, ( #).
Smalltalk .
,
.
, .
Smalltalk ( standard image)
,
.

10.2.3. Objective-C
Objective-C .
, ,
. , Smalltalk ,
, Objective-C.
, class, isKindOf:
isMemberOf:. ,
.
, ,
@selector
,
:
if ( [ fido respondsTo: @selector(bark) ] ) { ... }
Objective-C
,
.
3, ,
. (, Mammal)

Mammal anAnimal;
anAnimal
. C++,
(Mammal),
.
(,
Dog), - Mammal.
, .
,
. new,

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
, ,
- , .
Mammal *fido;
fido = [ Dog new ];

Objective-C
.
Objective-C.

10.2.4. C++
++
[Ellis 1990,
Stroustrup 1994]. , ,
- . ++:

( , ),
. , ++
, ,
. ++
( ), (
).
7, ++
-.
12, C++.
.
( ) .
,
.
,
virtual. ,
, .
, ,

.
, , :
class Mammal
{
public:
void speak()
{
printf("cant speak");
}
};
class Dog : public Mammal
{
public:
void speak()
{

PDF created with pdfFactory Pro trial version www.pdffactory.com

printf("wouf wouf");
}
void bark()
{
printf("wouf wouf, as well");
}
};
Mammal fred;
Dog lassie;
Mammal * fido = new Dog;

fred.speak() cant speak. lassie.speak()


. fido->speak() cant speak,
Mammal .
fido->bark() , ,
fido Dog.
Mammal, .
virtual:
class Mammal
{
public:
virtual void speak()
{
printf("cant speak");
}
};

fido->speak()
( , fido ).
++
. RTTI (RunTime Type Identification ).
RTTI typeinfo,
. name
.
typeid .
, Dog
fido.
- fido, ,
, :
cout << "fido is a " << typeid(*fido).name() << endl;

, - before,
,
. , true false:
if (typeid(*fido).before(typeid(fred))) ...
if (typeid(fred).before(typeid(lassie))) ...

RTTI ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

. , Animal
Cat Dog
:
class Mammal
{
public:
virtual int isaDog()
{
return 0;
}
virtual int isaCat()
{
return 0;
}
};
class Dog : public Mammal
{
public:
virtual int isaDog()
{
return 1;
}
};
class Cat : public Mammal
{
public:
virtual int isaCat()
{
return 1;
}
};
Mammal *fido;

, fido
Dog, fido->isaDog().
,
.
, ,
.
RTTI, dynamic_cast, .
Mammal Dog, Dog
.
, Dog. ,
, .
:
class Dog;
//
class Cat;
class Mammal
{
public:
virtual Dog* isaDog()
{
return 0;
}
virtual Cat* isaCat()
{
return 0;

PDF created with pdfFactory Pro trial version www.pdffactory.com

}
};
class Dog : public Mammal
{
public:
virtual Dog* isaDog()
{
return this;
}
};
class Cat : public Mammal
{
public:
virtual Cat* isaCat()
{
return this;
}
};
Mammal *fido;
Dog *lassie;

lassie = fido->isaDog(); .
lassie , fido
Dog. fido Dog, lassie
.
lassie = fido->isaDog();
if (lassie)
{
... // fido Dog
};
else
{
... //
... // fido Dog
};

,
,
, .
, .
,
.
,
. dynamic_cast
, , ,
( ), (
). ,
, :
// , fido
lassie = dynamic_cast (fido);
// ,
if (lassie) . . .

C++ (static_cast, const_cast


reinterpret_cast),

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
.

10.2.5. Java
Java ++,
, ++. Java
. ,
,
. ( )
.
, ,
, .
Ball aBallValue;
// ...
//
BlackBall bball = (BlackBall) aBallValue;

,
instanceOf.
if (aBallValue instanceOf BlackBall)
...
else
...


. C++ virtual .
Java ,
,
, .
:
class A
{
String name = "class A";
public void print()
{
println("class A");
}
}
class B extends A
{
String name = "class B";
public void print()
{
println("class B");
}
}
class test
{
public void test()
{
Class B b = new B();
Class A a = b;
println(a);
// " "
println(b);
// " "

PDF created with pdfFactory Pro trial version www.pdffactory.com

a.print();
b.print();
}
}

// " ", " "


// " "

Java , ,
,
Java ,
.
, ,
. 7,
, .
extends, ,
. ,
.
,
, , .
,
. .
, Java
(. 16, )
: , ,
. :
interface forwardIterator
{
void advance();
int currentValue();
}
interface bidirectionalIterator extends forwardIterator
{
void moveBack();
}
interface randomAccessIterator extends
bidirectionalIterator
{
int at(int);
}

.
.
class listIterator implements forwardIterator
{
// ...
}

10.3. : ?

,
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
.
, ,
.
(
),
- .
, , .

. ,
. ,
,
.
, ( Java,
Object Pascal C++)
. ,
,
(, ) .
, . ,
( ,
Object Pascal). ,
.
,
.
,
( )
:
. , -, ( inline C++ ).
, run-time-
( ) .
, (,
, ), , ,
.
, ,
.
,
.
( )
.
, : ,
. [Cox 1986] ,
, ,
. ,
- (
) .

PDF created with pdfFactory Pro trial version www.pdffactory.com

,

,
.
, (
(gate-level abstractions)
). (
)
.
.
, ,
.
, ,
. .

Object Pascal ,
. , (
) .
, -
(, C+
Object Pascal) ,
,
.
, ,
, .
?

?
?

11:


, .
, . ,
. , 1?
.
,
, , .
Platypus -
Mammal.
. ,
,
.

11.1. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, , ,
. ,
, ,
( ) . ,
.
,
, .
. ,
.
1, ,
,
. , ,
. - ,
. .
( ),
.
, , , ,
(override) . ,
, , , ,
. (
. Java
( ),
.)

11.1.1.

.
.
.
. ,
.
,

(Smalltatk C++). ,
Simula [Dahl 1966, Birtwistle 1979, Kirkerud 1989],
- , Beta
[Madsen 1993]. .
7 .

.
.

11.2.

PDF created with pdfFactory Pro trial version www.pdffactory.com

Smalltalk
Integer Float .
Number. ,
aNumber, ,
, sqrt,
. , , Integer ,
Number :
sqrt
" "
" "
self asFloat sqrt

asFloat self,
sqrt. asFloat ,
. sqrt.
Float. , Float
sqrt,
Number. Float ( )
.
sqrt ,
,
Number.
Number (
, ,
). Float,
,
.

11.2.1.

, . ,
,
.
, ,
- ?
Float Number

. ,
, sqrt ,
.

:
1. ,
.
:
Object Pascal, C++, Objective-C, Java Smalltalk.

PDF created with pdfFactory Pro trial version www.pdffactory.com

2. Eiffel [Meyer 1988a, Rist 1995], , .


,
, .
,
. ,
, ,
.
3. , Java.

, ,
.
4. , .
.

11.2.2.
,
( : ).
Smalltalk, Java Objective-C . C++
()
. Object Pascal Apple
, . Delphi Pascal
.
-
, ,
(
). ,
,
, .
, (, List) .

(, OrderedList), . , C++,
,
(virtual). , Java Objective-C
.

11.3.
.

11.3.1. C++
C++
, , ( )
.
. ( 11.1).
11.1. C++

PDF created with pdfFactory Pro trial version www.pdffactory.com

class CardPile
{
public:
CardPile (int, int);
card & top();
void pop();
bool empty();
virtual bool includes(int, int);
virtual void select(int, int);
virtual void addCard(card &);
virtual void display(window &);
virtual void canTake(card &);
protected:
Card * firstCard;
int x;
int y;
};
class SuitPile : public CardPile
{
public:
SuitPile(int, int);
virtual bool canTake(card &);
};

, 1)
, 2)
virtual 1 . :
CardPile, 8. C++,
, 11.1,
CardPile ( SuitPile).
canTake , .

, :
bool CardPile::canTake (card & c) {
//
return false;
}

, SuitPile ,

:
bool SuitPile::canTake (card & c)
{
//
if (empty ())
return c.rank() == 0;
card & topcard = top()
//
//
if ((c.suit() == topcard.suit()) &&
1

: , ,
. . 12.3.1
.
(c.rank() == 1 + topcard.rank()))
return true;
return false;

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
.
C++ ,
canTake CardPile .
. , , .
virtual .
, .
.
virtual , -
.
-. ,
10.
, ()
, ,
. virtual canTake,
, SuitPile, SuitPile,
, CardPile,
.
C++
.
17. C++
, ,

, . ,

.
. ,
, ,
,
C++.
C++ , .
, .

11.3.2. Object Pascal


Object Pascal Apple ,
:

, ;
, , ;
override.

11.2 . Employee
, SalaryEmployee HourlyEmployee .

PDF created with pdfFactory Pro trial version www.pdffactory.com

computePay Employee .
, ,
, .
emp, Employee.
, SalaryEmployee,
HourlyEmployee ( ).
computePay
.
, Delphi Pascal Borland, C++.
Borland , ,
virtual , 11.3.
11.2. Object Pascal Apple
type
Employee = objet
name : alpha;
function computePay : integer;
function hourlyWorker : boolean;
procedure create;
end;
SalaryEmployee = object (Employee)
salary : integer;
function computePay : integer; override;
function hourlyWorker : boolean; override;
procedure create; override;
end;
HourlyEmployee = object (Employee)
wage : integer;
hourworked : integer;
function computePay : integer; override;
function hourlyWorker : boolean; override;
procedure create; override;
end;
function Employee.computePay : integer;
begin
return 0; (* *)
end;
function HourlyEmployee.computePay : integer;
begin
return hoursworked * wage;
(* , *)
end;
function SalaryEmployee.computePay : integer;
begin
return salary div 12;
(* *)
end;

11.3. Delphi Pascal


type
Employee = class (TObject)
name : string;
function computePay : integer; virtual;
function hourlyWorker : boolean; virtual;
constructor create; virtual;
end;

PDF created with pdfFactory Pro trial version www.pdffactory.com

SalaryEmployee = class (Employee)


salary : integer;
function computePay : integer; override;
function hourlyWorker : boolean; override;
constructor create; override;
end;

11.3.3. Smalltalk Objective-C


Smalltalk Objective-C , ,
, . , -
.
.

11.3.4. Java
Java, Smalltalk Objective-C,
. ,
, ,
.
10, Java (
).
,
() , .
.
10.
Java final. ,
, ()
.
, ,
. Java
, final, inline-
( Beta, . 11.4.1).

11.4.

.
.
, .
,
( ).
.
,
, ,
.
-
, ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
. ,
,
,
, , ,
.
,
-
.
, ,
.

11.4.1. Simula Beta


-
Simula, 1960 [Dahl 1966]. Simula
,
, :
class Employee
begin
integer identificationNumber;
comment ;
comment , ,
comment
comment ;
identificationNumber :=
prompt_for_integer("Enter idNumber: ");
inner;
end;


Employee. inner ,
. , ,
Employee,
. .
- HourlyEmployee,
Employee. inner
HourlyEmployee.
inner,
. . , inner
:
Employee class HourlyEmployee
begin
integer hourlyWage;
... comment ;
hourlyWage :=
prompt_for_integer("Enter wage: ");
inner;
end;

Simula inner
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. Beta [Madsen 1993]



, ,
(pattern) ( (design patterns),
18).
Beta ,
, , .
, HTML- WWW-.
- , URL (
), .
HTML- :
<A HREF="http://www.cs.orst.edu/~budd/oop.html">

Beta , ,
, WWW-:
printAnchor:
(#
do
'<A HREF="http:'->puttext; INNER
'">'->puttext
#);

puttext .
, ( ),
.
Web , , Web-
. ,
. ,
INNER :
printOSUAnchor : printAnchor
(#
do
'//www.cs.orst.edu/'->puttext;
INNER
#);

, (printAnchor)
, , .
INNER
. Web-,
.
. ,
Web- :
printBuddAnchor : printOSUAnchor
(#
do
'~budd/'->puttext;

PDF created with pdfFactory Pro trial version www.pdffactory.com

INNER
#);

printBuddAnchor :
<A HREF="http://www.cs.orst.edu/~budd/">

Beta .

printBuddAnchor(# do 'oop.html/'->puttext #)
:
&ltA HREF="http://www.cs.orst.edu/~budd/oop.html/">
, ,
(
Beta virtual pattern declarations). ,
Employee,

:
Employee :
(#
identificationNumber : @integer;
display:<
(#
do 'Employee Number: '->puttext;
identificationNumber->printInteger;
INNER
#);
#);

, HourlyEmployee,
.

. display
, , INNER,
:
HourlyEmployee : Employee
(#
wage : @integer;
display::<
(#
do
' wage: '->puttext;
wage->printInteger;
INNER
#)
#);

, HourlyEmployee
Employee.

PDF created with pdfFactory Pro trial version www.pdffactory.com

display, .
INNER display .

,
. , ,
( sqrt Smalltalk),
, Beta. ,
(, ,
, )
, .

11.4.2. - CLOS

CLOS [Keen 1989] Lisp. CLOS
-
(wrapping method). - -, -
-. , ,
, .
call-next-method
. ,
, C++ Object Pascal.

11.5.
C++
, . (C++
Simula,
.)
. ,
.

11.5.1. Object Pascal


Object Pascal
,
.
Simula Beta,
.
.
inherited ,
,
. , ,
initialize, ,
.
Employee :
procedure Employee.initialize;
begin
writeln("enter employee name: ");
readln(name);

PDF created with pdfFactory Pro trial version www.pdffactory.com

end;

SalaryEmployee
:
procedure SalaryEmployee.initialize;
begin
inherited initialize;
writeln("enter salary: ");
readln(salary);
end;

Delphi Pascal inherited .


, ,
, .

11.5.2. C++
C++ ,

, .
,
.
,
.
C++
.
, , .
, C++.
addCard CardPile
. DiscardPile ,
.
:
void DiscardPile::addCard (card & newCard)
{
// ,
if (! newCard.faceUp())
newCard.flip();
//
CardPile::addCard (newCard);
}

, ,
C++, ,
, .
.
,
,
( ), .
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com


,
. : TablePile,
:
TablePile::TablePile (int c, int x, int y)
: CardPile(x, y)
{
column = c;
}

C++ .

.

11.5.3. Smalltalk, Java Objective-C


6 super. ()
Smalltalk, Java Objective-C
. super
,
:
class A
{
private int a;
public initialize()
{
a = 3;
}
}
class B extends A
{
private int b;
public initialize()
{
b = 7;
//
super.initialize();
}
}

Java
super:
class newClass extends oldClass
{
newClass (int a, int b, int c)
{
//
super (a, b);
// . . .
}
}


Smalltalk Objective-C.

PDF created with pdfFactory Pro trial version www.pdffactory.com


1. : , ,
.
2. , ,
, .
3.
, ,
. , ,
.
4.
: ,
, . ,
,
(,
),
.
- ?
: ,
.
5. .
, ,
,
WWW-, 11.4.1. 4,
, , .

12:

. ,
, , (
), , .

.
, ( )
( ,
). win ,
Window, Window.
Window, TextWindow, , TextWindow
Window, win
TextWindow. ,
.
,
-
, .
, .
, , ,
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

12.1.
, ,
: ,
? win,
Window?
,
, (., , [Appel 1987]).
,
.
,
.
, ,
.
, ,
. , TextWindow, ,
, . .
:
class Window
{
int height;
int width;
...
public
virtual void oops();
};
class TextWindow : public Window
{
char *contents;
int cursorLocation;
...
public:
virtual void oops();
};
Window win; // Window

( contents
cursorLocation) win?
:
1. , .
win , Window,
.
2. , ,
,
.
3. . ,
, (
).

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
.
.

12.1.1.
C
. ,
,
, , , C++
(
) .
C++ ,
. , ,
win .
, .
. tWinPtr, , .
, tWinPtr,
new.
TextWindow , ,
TextWindow, .
Window win;
Window *tWinPtr;
...
tWinPtr = new TextWindow;

, , tWinPtr,
win? ,
win = *tWinPtr;

, win, Window,
, tWinPtr, .
, , tWinPtr, .
, (.
12.1). ( C++
.
,
.)
, ( tWinPtr)
. (slicing) ,

.
? , -
. : ?
, win ,
Window, TextWindow.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, Window,
. , Window
?
, , oops, .
win.oops() TextWindow,
win.cursorLocation,
win. , (
) .
void Window::oops()
{
printf("Window oops");
}
void TextWindow::oops()
{
printf("TextWindow oops %d", cursorLocation);
}

, C++,
.
:

, -,
, -
.

. 12.1.


( ), (
).

,
. ,
.
, , ,
.
, . ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

win.oops(), Window, ,
.
.
,
. ,
, . :
Window win;
TextWindow *tWinPtr, *tWin;
...
tWinPtr = new TextWindow;
win = * tWinPtr;
tWin = tWinPtr;
...
win.oops();
(*tWin).oops();

, , , win ,
tWin, , ,
win . - oops()
Window, TextWindow.

12.1.2.
:
, ,
( , ).
,
( ,
(union) C). ,
,
. . 12.1 , ,
.
, -, , :
, .
(unit Object Pascal, C++),
,
. ,
- .

12.1.3.
.
. (),
FIFO (
), .
,
, .
Object Pascal, Smalltalk, Java Objective-C,
Object Pascal.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, new
.
free , .
,
.
,
, .
, ,
:
type
intBuffer = object
value : integer;
end;
var
x, y : intBuffer;
begin
new(x); { }
x.value:=5; writeln(x.value);
y:=x;
{y , x}
y.value:=7; writeln(x.value);
end;

, .
, , ,
7, 5. : x y ,
. . 12.2.
Object Pascal ,

. x y , y:=x
x y.
, y
x.

12.2.
C++, Object Pascal
.
. ,
.
.
, . ,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ,
, . (
.) ,
. ,
.

, ,
.
,
.

-. .
, ,
. ,
, , (
), ().
, ,
.
-
(, ).

12.2.1. C++
, C++
- ,
. ,
. ,
C++ ,
.
.
, ,
, . void,
, , ,
. (
, ):
String & String::operator = (String& right)
{
len = right.len;
//
buffer = right.buffer;
//
return (*this);
}

C++
(=)
. C
.
int limit = 300;

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
int limit;
limit = 300;

C++
.
Complex x = 4;

Complex x(4);

;
. , s String,
t s (
).
... // s
String &t = s;
... // t s

-
.
, . ,
C++ -.
(
). , ,
. ,
class Base
{
public:
virtual void see();
};
class Derived
{
public:
virtual void see();
};
void f(Base);
void g(Base &);
Derived z;
f(z); g(z);

f() g() Base,


g() -. f()
, Derived,
( ), ,
Base. f() see,
Base. ,
g(). see
g(),
Derived. ,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

C++
( ). ,
. , ,
( =),
.
, C++,
[Koenig 1989a, Koenig 1989b].
C++
( ,
. .) .
, .
, .

.
class Complex
{
...
Complex (const Complex &source)
{
//
rl = source.rl;
im = source.im;
}
...
private:
double rl;
double im;
}

12.2.2. Object Pascal Java


Object Pascal, Java
. Object Pascal
, copy,
, . Java
Object clone,
clone. .
Object,
:
newBall = (Ball) aBall.clone();
, Object Pascal
. (, )
.
.

12.2.3. Smalltalk
Smalltalk . Object,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com


.
x := y copy
, y,
( ) , c
y. ,
x := y deepCopy
, y, ( )
y.
, copy ( shallowCopy)
, deepCopy

, deepCopy .
copy.
copy,
shallowCopy deepCopy,
.

12.2.4. Objective-C
Objective-C .
: copy, shallowCopy deepCopy,
, Smalltalk.
id x, y, z;
// ...
x = [ y copy ];
z = [ y deepCopy ];

12.3.
, ,
, , .
, (),
. - :
? (Is the morning star the evening star?),
, .
, (
), , , . ,
, ,
, .
, ,
.

, [Rosenberg 1971] [Whorf 1956]. ,
, -
.

PDF created with pdfFactory Pro trial version www.pdffactory.com


. ,
,
. ,
.
.
,
, , .
, , (. 12.3).
?
.
, .
Pascal C
.
, , .
,
, .
,
, , .
. ,
. - ,
.
.
-
. ,
,
. ?
, ?
,
. , , ,
. x y ,
, x=y , y=x !
, , ,
.
,
(,
).
; .

. 12.3.
-
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
. ?
, ?
,
. , , ,
. x y ,
, x=y , y=x !
, , ,
.
,
(,
).
; .

12.3.1.
.
Shape () Triangle () Square
(). ,
, .
, , :
class Shape
{
public:
boolean equals (Shape)
{
return false;
}
...
}
class Triangle : public Shape
{
public:
boolean equals (Triangle);
...
}
class Square : public Shape
{
public:
boolean equals (Square);
...
}

, , ,
. ,
.
:

,
, , .
, ,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com


, . ,
.
.
.
, ,
. ,
,
. ,
(
, ),
(
, ). , ,
. (
,
.)
,
. :

(),
, .
, - .
,
.
Triangle, Shape.

,
( ). , ,
. Shape,
Triangle, Square.
,
:
Triangle aTriangle;
Shape aShape;
aShape := aTriangle;
if aTriangle.equals(aTriangle) ... // true
if aTriangle.equals(aShape) ... // false!

equals Triangle ,
, true. Shape,
.
false, ( ,
) .
,
. ,
, , false:
if aShape.equals(aTriangle) ... // false

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
, , -
.
(novariance). ,
,
:
boolean Triangle.equals(Shape & aShape)
{
Triangle & right = dynamic_cast(aShape);
if (right)
{
... //
}
else
return false;
}

C++ . ,

. - Shape
Shape, Triangle
Triangle.
, .
, , Shape,
Shape,
Triangle ( Triangle).
, ,
Eiffel [Rist 1995] Sather.

12.3.2. Objective-C, Java Object Pascal


Objective-C, Java Object Pascal ( Objective-C
) . ,
(= Object Pascal, == Objective-C Java)
, .
, .
,
,
.
Card. ,
, :
function Card.equal(aCard : Card) : boolean;
begin
if (suitValue = aCard.suit) and
(rankValue = aCard.rank)
then equal := true
else equal := false;
end;


, . ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

(, ),
. 10
.
class Triangle extends Shape
{
boolean equals (Shape aShape)
{
if (aShape instanceOf Triangle)
{
... //
}
else return false;
}
}

12.3.3. Smalltalk
Smalltalk .
(==).
(=)
, .
, .
. , Array ,
, ,
.
, , :
, . x=y y=x
.
Smalltalk ,

. , ,
.

12.3.4. C++
C++ .
==. ,
,
. .
. , :
class A
{
public:
int i;
A(int x) {i = x;}
int operator== (A& x)
{
return i == x.i;
}
};
class B : public A
{

PDF created with pdfFactory Pro trial version www.pdffactory.com

public:
int j;
B (int x, int y) : A(x) { j = y;}
int operator== (B& x)
{
return (i == x.i;) && (j == x.j);
}
};

a b A B, a==a a==b
A, b==b B. b==a
, (a)
B. (
,
.)
, ( C++
, ) A B,
A. ,
, , ,
. ,
virtual.

12.4.
( C++ Object
Pascal) ,
. , , Window,
, TextWindow.
. , 1
2 .
.
, , ,
,
. ( )
.
C++ Objective-C C,
.
.
, ,
.
,
C++.
Ball , ,
Link :
class Link
{
protected:
Link *link;
public:
Link * next();

PDF created with pdfFactory Pro trial version www.pdffactory.com

void setLink(Link * elem);


};
void Link::setLink(Link *elem)
{
link = elem;
}
Link * Link::next()
{
return link;
}

Ball Link. setLink


, .
next. , Link, Ball.
, Ball,
, .
Ball , next, ,
:
class Ball : public Link
{
...
public:
...
Ball * next();
...
};
Ball * Ball::next()
{
return dynamic_cast(Link::next());
}

, next :
. , C++
, (. 2
). dynamic_cast RTTI (Run-Time Typing
Information ),
10. RTTI
,
.
Object Pascal .
,
, . Object Pascal
,
.
var
x : TextWindow;
y : Window;
begin
...
if Member(y, TextWindow)
then x := TextWindow(y)
else writeln('illegal window assignment');
...
end;

PDF created with pdfFactory Pro trial version www.pdffactory.com

Java , ,
, .
,
, . ,
instanceOf,
:
Ball aBall;
WhiteBall wBall;
if (aBall instanceOf WhiteBall)
wBall = (WhiteBall) aBall;
else
...

1. , ( C++ Object
Pascal) ,
.
:
TextWindow X;
Window Y;
...
X := Y;

2. , C++ ,
12.1. , ,
, 1,
,
x = (TextWindow) Y;

3. Object Pascal C++, ,


12.1
, .
4. ,
,
.
5. , , Object Pascal,
. ,
,
, 12.1, ,

. , Object Pascal
?

13:

,
. , , ,
, ( )
. ,
1, ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
. , , , .
.
. . Potter.
, PortraitPainter. ,
, : (HousePainter).
(
Potter Artist).
( PortraitPainter
Artist Painter, . 13.1).

. 13.1.

13.1.
, ,
. Smalltalk Magnitude
: 1 .
, ( Char)
(, ASCII).
, Number Smalltalk.
, Number
(, . .).
Char. Smalltalk : (Integer),
(Fraction), (Float).
. 13.2.

. 13.2. Smalltalk
, Complex,
. , ,
. Complex Number,
. ,
.
.
1
, ,
(,
, ). . .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, :

Char Magnitude,
Number.
Integer Magnitude, Number.
Complex Number,
Magnitude.


. :
1. Complex Number,
Magnitude. ,
Complex, ,
. ,
7. ,
,
.
2. .
Char, Integer, Complex . .
. ,
, 7, ,
.
(, C++ Object
Pascal) : ,
,
.
3. .
, Number,
( ) .
4. Magnitude Number
, Integer
(. 13.3). Float Number,
Magnitude.

. 13.3.
2 3 ,

(Objective-C, Smalltalk). C++ Object Pascal ,
, .
, ,
Magnitude. , Smalltalk Objective-C
, , ,
, . ,
,

PDF created with pdfFactory Pro trial version www.pdffactory.com

-,
(. [Ingalls 1986] 18.2.3).
, , ,
.
,
. C++
,
Smalltalk .
, .

13.2.
, ,
.
, Eiffel [Meyer 1988a, Meyer 1988b].
Menu. Menu ,
, . . , ,
( )
(. 13.4).

. 13.4. CRC- Menu


() MenuItem.
, ,
(. 13.5).

. 13.5. CRC- MenuItem

PDF created with pdfFactory Pro trial version www.pdffactory.com


( ),
, .
,
. () , ,
(/ . .).
Menu.
, Menu, .
, , , MenuItem,
( ),
.
, WalkingMenu
. ,
( MenuItem),
( , Menu).
,
.
, :
.
,
. , :
.
. , ,
.
,
, .

( ).

, ,
.
(, ) .
,
Persistence Storable. ,
Storable .
,
,
.

13.3.
,
.
, . ,
CardDeck,
: ( shuffle),
( draw 1 )
1
, draw
( - . .
), , . . .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ., . ,
.
( ) , , draw
.
GraphicalDeck,
CardDeck, GraphicalObject. ,
GraphicalDeck CardDeck .
GraphicalDeck GraphicalObject.
draw.
[Meyer 1988a], ,
. draw
, .
. ,
.
, .
.
,
.

. GraphicalDeck
draw ,
drawCard.

13.3.1.
, ,
. ,
/. .
. ,
. InStream
.
, . . OutStream
.
Stream. , ,
, .

. 13.6.

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
/ InOutStream (. 13.6). ,
. (. )
, InStream OutStream.
, Stream?
, , (.
. 13.6). ,
, .
(,
), .
? ,
,
. ,
C++.

13.4. C++
C++,
. ,
( 13.1).
: Link ,
LinkedList .
.
.
. Link.
13.1.
class LinkedList
{
public:
Link *elements;
LinkedList()
{
elements = (Link *) 0;
}
void add(Link *n)
{
if (elements) elements->add(n);
else elements = n;
}
void onEachDo(void f(Link *))
{
if (elements) elements->onEachDo(f);
}
};
class Link
{
public:
Link *next;
Link()
{
next = (Link *) 0;
}
void setLink(Link *n)
{
next = n;

PDF created with pdfFactory Pro trial version www.pdffactory.com

}
void add(Link *n)
{
if (next) next->add(n);
else setLink(n);
}
void onEachDo(void f(Link *))
{
f(this);
if (next) next->onEachDo(f);
}
};

Link. ,
IntegerLink 13.2 .
13.2 , ,
.
,
Tree ( ).
, .
(,
). ,
. 13.7 .
,
.
LinkedList (
), Link (
). C++ ,
, (
). ,
(public private),
. 13.3 Tree,
13.2. Link
class IntegerLink : public Link
{
int value;
public:
IntegerLink(int i) : Link()
{
value = i;
}
print()
{
printf("%d\n", value);
}
};
void display(IntegerLink *x)
{
x->print();
}
main()
{
LinkedList list;
list.add(new IntegerLink(3));

PDF created with pdfFactory Pro trial version www.pdffactory.com

list.add(new IntegerLink(17));
list.add(new IntegerLink(32));
list.onEachDo(display);
}

. 13.7.
13.3.
class Tree : public Link; public LinkedList
{
int value;
public:
Tree(int i)
{
value = i;
}
print()
{
printf("%d\n",value);
}
void add(Tree *n)
{
LinkedList::add(n);
}
void addChild(Tree *n)
{
Linkedlist::add(n);
}
void addSubling(Tree *n)
{
Link::add(n);
}
void onEachDo(void f(Link *))
{
/* */
if (elements) elements->onEachDo(f);
/* */
f(this);
/* */
if (next) next->onEachDo(f); }
};
main()
{
Tree *t = new Tree(17);
t->add(new Tree(12));
t->addSubling(new Tree(25));
t->addChild(new Tree(15));
t->addEachDo(display);
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

Link LinkedList public. Tree


, .
.
add (),
. :
- -.
add LinkedList, add Link.
add , ,
( ).
, .
,
. - (, Eiffel)
.
onEachDo .
.
-, ,
(, ,
). Link
LinkedList, 13.4.
-
. C++
, ,
.
. , A B,
display, ( 13.4).
, ,
.
, . display
, ,
A ( ) B (
C++ ;
integer double). ,
.
, .
, C,
13.4.
C, ,
.
13.4.
class A
{
public:
void virtual display(int i)
{
printf("in A %d\n", i);

PDF created with pdfFactory Pro trial version www.pdffactory.com

}
};
class B
{
public:
void virtual display(double d)
{
printf("in B %g\n", d);
}
};
class C: public B, public A
{
public:
void virtual display(int i)
{
A::display(i);
}
void virtual display(double d)
{
B::display(d);
}
};
main()
{
C c;
c.display(13);
c.display(3.14);
}

, ,
, .
InStream OutStream,
Stream. ,
, Stream, InStream OutStream
,
. virtual ,
, ,
. 13.5
.
13.5.
class Stream
{
File *fid;
...
};
class InStream : public virtual Stream
{
...
int open(File *);
};
class OutStream : public virtual Stream
{
...
int open(File *);
};
class InOutStream: public InStream, public OutStream
{
...
};

PDF created with pdfFactory Pro trial version www.pdffactory.com

, C++, (
), ,
( virtual ) .
,
.
. ,
,
, Link.
, GraphicalDeck.
virtual
. ,
.
, ,
. ,
, public protected.
( protected)
.
,
, , .
,
. , 13.6
, C B
, A.
.
.
(
,
). .
13.6 C
: D ,
B A. D
A B , ,
.
, , C
, D
13.6.
class D
{
public:
D()
{ ... }
D(int i)
{ ... }
D(double d)
{ ... }
};
class A : virtual D

PDF created with pdfFactory Pro trial version www.pdffactory.com

{
public:
A() : D(7)
{ ... }
};
class B : virtual D
{
public:
B() : D(3.14)
{ ... }
};
class C : public A, public B
{
public:
C() : B(), A()
{ ... }
};

C. ,
,
. C
:
C() : D(12), B(), A() { ... }

,
.
, ,
. , 13.5
initialize(). Stream
. initialize()
InStream OutStream Stream::initialize() , ,
.
initialize() InOutStream.
InStream::initialize() OutStream::initialize() ,
Stream::initialize().
Stream::initialize(), , . -
: Stream::initialize() , ,
. : InStream
OutStream, Stream.
InOutStream
.

13.5. Java
Java ,
. ,
. ,
,
. :
class graphicalObject implements Storable, Graphical
{
// ...

PDF created with pdfFactory Pro trial version www.pdffactory.com

( ),
.
:
interface GraphicalObject extends Storable, Graphical
{
// ...
}


[Sakkinen 1988a].
Ph. D.
[Sakkinen 1992]. C++
[Ellis 1990].

1. ,
.
2. [Wiener 1989]
C++. IntegerArray,
Array Integer. ,
? .
3. Tree ,
.
. ?
4. 3 ,
. :
, ,
.
5. C++
.

14:

(poly , morphos ). morphos
(Morphus), ,
, , , .
( Homo Sapiens),
.
(,
).

14.1.
(,
), .
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

14.1.1.

(Lisp, Scheme , Smalltalk
- ).
Smalltalk silly, x : (x+1),
x , , x , ,
x , nil :
silly: x " "
(x isKindOf: Integer) ifTrue: [ x + 1 ].
(x isKindOf: Fraction) ifTrue: [ x reciprocal ].
(x isKindOf: String) ifTrue: [ x reversed ].
nil

.
. ,
+ .
.
(, ML [Milner 1990])
,
.
, T, T .
, .
. .
-
. -
,
.

14.1.2.

.
(, ),
.
(, ),
, .
.
, , ,
. . ,
, :
function length (list) -> integer
begin
if list.link is nil
then
return 1
else
return 1 + length(list.link)

PDF created with pdfFactory Pro trial version www.pdffactory.com

end

(,
Pascal) ,
( link), , ,
. Pascal
:
type
intlist = record
value : integer;
link : ^ intlist;
end;
function length (x : ^ intlist) : integer;
begin
if x^.link = nil then
length := 1
else
lehgth := 1 + length(x^.link);
end;

,
,
.
.
, .
,
, ,
.
, . ,

.
. .
, (
)
.
,

.
.
, .

14.2.
-
:
;
;
;
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

-
.
.
,
. ( )
. ,
( ) .
ad hoc.
1 .

14.3.
-
.
: ,
. .
, ,
.
(Smalltalk, Objective-C)
(
). .
, , . ,
(,
), ,
.
(, C++, Java, Object Pascal ObjectiveC ) . ,
.

() () ,
. 10,
.
.
Object Pascal Java ,
object. C++ Objective-C
. ,
10, ,
.
1

- ,
. , [Horovitz 1984], [Marcotty 1987], [MacLennan 1987]
[Pinson 1988] , ,
. [Sethi 1989] [Meyer 1988a], ,
[Wikstrom 1987], [Milner 1990],
, .
- ,
. ,
[Cardelli 1985] [Danforth 1988].

PDF created with pdfFactory Pro trial version www.pdffactory.com

allPiles
8. CardPile,
, .
(, display),
, , ,
.
public class Solitare extends Applet
{
...
static CardPile allPiles[];
...
public void paint(Graphics g)
{
for (int i = 0; i < 13; i++)
{
allPiles[i].display(g);
};
}
...
}

14.4.
, , ( ) ,
. ,
, 11 (
), ,
.
.
: ,
, ,
. ,
(
) , ,
.

14.4.1.
1 , .
, ?
, sendFlowersTo
.
. ,
, - ,
. sendFlowersTo
, , .
.
, ( , ).

Human (). sendFlowersTo

PDF created with pdfFactory Pro trial version www.pdffactory.com

. , , ,
.

14.4.2.
,
, .
(, , , )
. add,
.
,
.
, -.
+. ,
, .
, ,
.
, .

. ,
.
,
:

, +
, + , + , +
. , .
: + + .
+ +
.
.
: + .
.
, .

14.4.3.
, ,
, - . ,
, 8. draw ,
. draw ,
. draw
draw,
, .
,

. , .
, ( add, draw . .)
- . ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

add, - addNewElement
Set_Module_Addition_Method.
- , ,
.
,
. ,
, .
C++ Object Pascal, , .

14.4.4.
, (, )
,
, .
C++ Java, (, Ada)
, .
-. C++ , ,
,
, . (
, character
integer integer float ,
,
.
[Ellis 1990] [Stroustrup 1986].)
,
: , , .
,
, .
, ,
.

14.5.
11 ( )
- .
. , ,
. ( )
,
.
.
( ).
, .
, ,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

14.5.1. Magnitude
Magnitude
Little Smalltalk. Magnitude ,
, ( )
. ,
, , ,
, . .
Magnitude :
<= arg
self < arg or: [ self = arg ]
>= arg
arg <= self
< arg
self <= arg and: [ self ~= arg ]
> arg
arg < self
= arg
self == arg
~= arg
(self = arg) not

, : .
- ?
, Magnitude
.
, = &lt:, >=,
.
,
, ,
. , ,
.
,
.
C++
.
14.9.

14.6.
( , C++
) .
.
, , .
.
:
.
, draw,
: Triangle, Circle Square.

PDF created with pdfFactory Pro trial version www.pdffactory.com

Shape.
, Shape
. draw
() Shape,
Square, Triangle Circle.
, .
-
(C++, Object Pascal) ,
, ,
. ,
Shape,
. .
draw ,
, .
draw Shape ,
draw Shape .

14.7.
( )
, ,
, 1.
- . ,
Lisp ML ,
. ,
.
- .

.
, .

. ,
.
.
14.5, , Magnitude
Smalltalk ,
. between:and:, :
between: low and: high
" , "
" "
( low <= self ) and: ( self <= high )

Magnitude ( ),
.

(
Smalltalk ).
true, , .

PDF created with pdfFactory Pro trial version www.pdffactory.com

between:and: ,
.
, Magnitude,
.
. between:and:
.
:
anInteger between: 7 and: 11
aFloat between: 2.7 and: 3.5

ASCII .
between:and: ,
. , , aChar ,
( $a Smalltalk
a):
aChar between: $a and: $z

Points true,
(

-). Points Smalltalk.
@. -
, - . ,
< () .
.
aPoint between: 2@4 and: 12@14

true, aPoint (2,4)


(12,14) .
,
between:and:. .
,
( ), .
18 ,
.

14.8.

, C++ .
() , ()
. :
.
, .
, ,
Linked List of X ( X),
X .
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
, ,

. -
. , C++
:
template class List
{
public:
void add(T);
T firstElement();
//
T value;
List * nextElement;
};

T .
List T .
- add .
firstElement.
,
T.
:
List aList;
List bList;

( - ) -.
,
.
template int length (List & aList)
{
if (aList == 0) return 0;
return 1 + length(aList.nextElement);
}

++ - ,
16.

14.9.

.

14.9.1. C++
C++.
.

PDF created with pdfFactory Pro trial version www.pdffactory.com


10, C++
. (
) ,
,
.

. , :
class One
{
public:
virtual int value()
{
return 1;
}
};
class Two : public One
{
public:
virtual int value()
{
return 2;
}
};

One , 1.
Two , 2.
:
void directAssign (One x)
{
printf("by assignment value is %d\n",
x.value());
}
void byPointer (One * x)
{
printf("by pointer value is %d\n",
x->value());
}
void byReference (One & x)
{
printf("by reference value is %d\n",
x.value());
}

One,
, .
Two
One, 1.
.
, 2.

PDF created with pdfFactory Pro trial version www.pdffactory.com


C++
.
11, virtual ,
.
, . virtual
One (
Two), 1 .
virtual ( )
,
.
,
, (, )
. ,
virtual void display (char *, int);

:
virtual void display (char *, short);

,
. ,
() . , ,
, .
,
.
:
? .
,
, ,
.
.
, . ,
C++, , -
,
( ) . ,
,
. ,
, ,
.


C++
, ,
, .
, ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
, .

, .
, :
,
, ,
. ,
.

C++
C++ ( )
virtual.
, 0:
class Shape
{
public:
...
virtual void draw() = 0;
...
};

,
. .

, .


C++
template (). - . -
-
C++, 16.

14.9.2. Java
Java ( extends),
( interfaces).
, . .
, , ,
. , ,
, .
Java abstract. ,
abstract, ; .
. ,
, .
.
abstract class shape

PDF created with pdfFactory Pro trial version www.pdffactory.com

{
//
public abstract draw();
// ...
}
class triangle extends shape
{
public draw()
{
//
}
// ...
}

Java final,
abstract. , final,
.

14.9.3. Object Pascal


Object Pascal
, .
, .

Object Pascal
7, Apple Borland Object Pascal ,
. Object Pascal Apple
override . Delphi
override virtual .
Object Pascal Apple .
, :
type
Shape = object
corner : Point;
procedure draw();
...
end;
Circle = object (Shape)
radius : integer;
procedure draw(); override;
...
end;
procedure Shape.draw();
begin
writeln('descendant should define draw');
halt();
end;

Delphi Pascal c
abstract, virtual ( dynamic)
. . C++

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
Delphi , .
type
class TShape
procedure draw; virtual; abstract;
...
end;
class TTriangle (TShape)
procedure draw; override;
...
end;

14.9.4. Objective-C


id Objective-C
.
( ) C++.

Objective-C
ObjectiveC. , Object
subclassResponsibility ( ).
, ,
.
draw Shape , ,
:
@implementation Shape : Object
...
- draw { return [ self subclassResponsibility ]; }
...
@end

14.9.5. Smalltalk


Smalltalk ,
. .

Smalltalk
Smalltalk , .
Object subclassResponsibility (
). , ,
, .
draw Shape :

PDF created with pdfFactory Pro trial version www.pdffactory.com

draw
" "
self subclassResponsibility

14.10.
. ,
,
.
: , .
.
( between:and:, ),
, , ,
.
,
. -,
. ,

, .

1. , nil Pascal,
NULL C ? .
2. ( )
(Pascal C)?
3. :
anInteger between: 7 and: 11

4. , Smalltalk : Apple
Orange, Fruit.
?

15: :

, , , , , , .
,
.
, .

. ,
, .
:


,
?

PDF created with pdfFactory Pro trial version www.pdffactory.com

(
) ,
( )?

,
?

15.1.
, ,
(, C
Pascal). .
Pascal .
, :
type
List = record
firstLink :
end;

Link;

() ,
( ) .
, . Link
:
type
Link = record
value : integer;
nextElement : Link;
end;

,
. ,
, ,
. .
procedure addToList(var aList : List,
newVal : integer);
(* *)
var
newLink : Link;
begin
(* *)
new(newLink);
newLink.value := newVal;
(* *)
newLink.nextElement := aList.firstLink;
aList.firstLink := newLink;
end;
function firstElement (var aList : List) : integer;
(* *)
var
firstNode : Link;
begin
firstNode := aList.firstLink;
firstElement := firstNode^.value;
aList.firstLink := firstNode^.nextElement;
dispose(firstNode);
end;

PDF created with pdfFactory Pro trial version www.pdffactory.com

(
), . ,
, .

.
,
. integer, ,
.
RealLink,
RealList .
( union C)
,
.
, , .
. ,
, -
. ,
.
(,
)?

. ,
, :
var
aList : List;
(* *)
p
: Link; (* , *)
begin
...
p := aList.firstLink;
while (p <> nil) do
begin
writeln(p.value);
p := p.nextElement;
end;
...

,
, p. Link,
.
Link, .
, ,
, ,
.

15.2.

(Smalltalk, Objective-C).

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
,

, , .
,
Objective-C:
@ interface List : Object
{
id firstLink;
}
- (void) addToList : value
- id
firstElement
@ end
@ interface Link : Object
{
id Value
id NextElement
}
+
- id value
- id nextElement
@ end

, , , id (
). , , id,
,
.
, new
List:
id aList
...
aList = [ List new ];

,
-:
[ aList addToList: aValue ];

,
, , :
@ implementation List
- (void) addList: newElement
/* */
{
id newLink;
newLink = [ Link new ];
[ newLink setValue: newElement link: firstLink ];
firstLink = newLink;
}
- id firstElement
/* */
{
id result;
result = [ firstLink value ];

PDF created with pdfFactory Pro trial version www.pdffactory.com

firstLink = [firstLink nextElement ];


return result;
}
@ end


,
. : Smalltalk,
.
, Smalltalk
, block. . ,
.
Smalltalk ,
. , ,
:
aList do: [ :ele ele print ]

.
,
.
linkDo: aBlock
" , "
aBlock value: value.
nextLink notNil
ifTrue: [ nextLink linkDo: aBlock ]

,
.
Objective-C -
- .
15.1. Objective-C
@ implementation ListIterator
{
currentLink : id;
}
+ newIterator : aList
{
self = [ ListIterator new ];
currentLink = [ aList firstLink ];
return self;
}
- id value
{
return [ currentLink value ]
}
- int atEnd
{
return currentLink == nil;
}
- void advance
{
if (! [ self atEnd ] )

PDF created with pdfFactory Pro trial version www.pdffactory.com

currentLink = [ currentLink nextElement ];


}
@end

,
(iterator).
(, ).
( )
. ,
. 15.1 ,
.
.
:
id aList; /* */
id itr;
/* */
for (itr = [ aList iterator ]; ! [ itr atEnd ];
[itr advance ])
print( [ itr value ] );

, -,
.
, ,
.

. , . ,
.
,
.

15.3.
,
(Object Pascal, C++). ,

. 6 , ,
, .

, .
, ,
, .
ListElement. .
15.2 , Object Pascal.
, , ListElement.
,
ListElement.
.
, ListElement.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, - ,
. ,
ListElement, .
. ,
, ListElement. WhiteBall
, BlackBall .
, WhiteBall.
15.2. Object Pascal
type
List = object
firstLink : ListElement;
procedure addToList(var newValue : ListElement);
function firstValue : ListElement;
end;
ListElement = object
next : ListElement;
end;
procedure List.addToList(var newValue : ListElement);
(* *)
begin
(* *)
newValue.next := firstLink;
(* *)
firstLink := newValue;
end;
function firstValue : ListElement;
(* *)
var
first : ListElement;
begin
first := firstLink;
firstValue := firstLink;
firstLink := first.next;
end;

, ,
, , - :

, ?
,
?

, Object Pascal
Member, , ,
. Member ,
,
:
var
aBall : WhiteBall;
aList : List;
aValue : ListElement;
...
(* *)
aValue := aList.firstElement;
(* ? *)

PDF created with pdfFactory Pro trial version www.pdffactory.com

if Member(aValue, WhiteBall) then


(* *)
aBall := WhiteBall(aValue);

,
,
.
.
, (.
Objective-C). , firstElement,
ListElement.
:
var
aList : List;
aValue : ListElement;
itr : ListIterator;
...
itr := aList.iterator;
while (not itr.atEnd) do
begin
aValue := itr.current;
if Member(aValue, WhiteBall) then
...
itr.advance; (* *)
end;

RTTI (Run-Time Typing Information


), C++
. ,
,
. dynamic_cast
.

15.4.
, ,
:

,
ListElement.
.
.
,
.

,
. C++
. , C++
, (
). C++
, , .

PDF created with pdfFactory Pro trial version www.pdffactory.com


, .
, ,
:
class GenericList // void *
{
public:
void addToList (void * newElement);
void * firstElement();
private:
GenericLink * firstLink;
};
class GenericLink
{
public:
void
* value;
GenericLink * nextLink;
};

void.
.
. ,
( Window). ,
,
, .
.
class WindowList : public GenericList
{
public:
void addToList (Window * newElement)
{
GenericList::addToList (newElement);
}
Window * firstElement ()
{
return (Window *) GenericList::firstElement;
}
};

, ,
( ).
List, Link, , ,
-.
,
, ,
.
, , .

15.5.
, (
)
. .
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. C++
.
,
.
. , ,
. ,
,
.
C++
:
template
class List
{ public:
void addElement (T newValue);
T firstElement ();
ListIterator iterator();
private:
Link * firstLink;
};
template
class Link
{
public:
T value;
Link * nextLink;
Link(T, Link *);
};

( T)
. ,
T, , T, . .
-, ,
:
template
void List::addElement(T newValue)
{
firstLink = Link new (newValue, firstLink);
}
template
T List::firstElement()
{
Link first = firstLink;
T result = first.value;
firstLink = first->nextLink;
delete first;
return result;
};
template
Link::Link(T v, Link * n) : value(v); nextLink(n)
{ }

PDF created with pdfFactory Pro trial version www.pdffactory.com

, . ,
:
List listOne;
List listTwo;

.
.
,
,
,
.
.
, . (
.)
:
, .
,
, -
. ,
.
,
(
), .
.

15.5.1. C++
,
, C++.
C++,
16.
.
, , . ,
:
template
class List
{
public:
typedef ListIterator iterator;
...
iterator begin()
{
//
return ListIterator (firstLink);
}
iterator end()
{
//
return ListIterator (0);

PDF created with pdfFactory Pro trial version www.pdffactory.com

}
}
template
class ListIterator
{
public:
ListIterator (Link * sl) : currentLink(sl)
{ }
void operator ++ ()
{
//
currentLink = currentLink->nextLink;
}
T operator * ()
{
//
return currentLink->value;
}
bool operator == (ListIterator & right)
{
return currentLink == right.currentLink;
}
private:
Link * currentLink;
};

.
:
List::iterator start = aList.begin();
List::iterator end = aList.end();
for (; itr != end; itr++)
{
cout << (*itr) << endl;
};

, ,
Smalltalk.
, .
for_each,
:
void printOut(int n)
{
cout << "the collection contains a " << n << "\n";
}
...
for_each (aList.begin(), aList.end(), printOut);

,
, .
, .
.

1. - :
?

PDF created with pdfFactory Pro trial version www.pdffactory.com

2. ,
( , ), ,
(, ). ,
.

.
3. ,
.
4. Smalltalk ,
, , .
, ,
, .
Smalltalk (Object Pascal,
C++)? ?
5. , .

16: : STL
, ,
C++.
, , , (), , , .
, C++

.
STL (STL Standard Template Library,
) [Musser 1996, Glass 1996].
STL
Hewlett-Packard
Rensselaer Polytechnic Institute. STL
- ,
(Scheme
Ada).
STL .
, -
, ,
STL. STL . .
,
.
-
.
, ,
.
, .
STL . ,
, , .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
.
, STL
, .

, -
. , ,
( C),
.

. find
. ,
. find
. :
template
InputIterator find (InputIterator first,
InputIterator last,
const T& value)
{
while (first != last && *first != value)
{
++first;
};
return first;
}

,
C. 7 ,
:
int data[100];
...
int * where;
where = find(data, data+100, 7);

:
list aList;
...
list::iterator where;
where = find(aList.begin(), aList.end(), 7);

STL.
, ,
-.
STL.

16.1.
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
.
-
, .
( ).
, (
).
, .
, , ,
, .
null
. -
. ,
null, ,
.
C++ , ,
. ,
x 10 x x+10,
x+10 . x+10
, ,
. .
,
, ,
.
, , ,
( ++). ,
,
. (*)
, .
,
, , .
, (
).
begin(), ,
, end().

16.2. -
STL .
for_each(),
, , .
,
:
void printElement(int value)
{
cout << "The list contains " << value << endl;
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

main ()
{
list aList;
...
for_each(aList.begin(), aList.end(), printElement);
}

-. -
, ().
-. -
, .
, :
class biggerThanThree
{
public:
bool operator () (int v)
{
return V > 3 ;
}
};

biggerThanThree, ,
, ,
. ,
,
:
class biggerThan
{ public:
biggerThan (int x) : testValue(x) { }
const int testValue;
bool operator () (int val)
{ return val > testValue; }
};

,
X, X .
, ,
. , 12,
list::iterator firstBig = find_if(aList.begin(),
aList.end(),
biggerThan(12));

16.3. :
STL.
, WorldWideWidgetWorks .
, :
class Widget
{
public:
Widget(int a) : id(a) { }
Widget() : id(0) { }

PDF created with pdfFactory Pro trial version www.pdffactory.com

int id;
};
ostream & operator << (osteam & out, Widget & w)
{
return out << "Widget " << w.id;
}
bool operator == (const Widget & lhs, const Widget & rhs)
{
return lhs.id == rhs.id;
}
bool operator < (const Widget & lhs, const Widget & rhs)
{
return lhs.id < rhs.id;
}

. ,
. ,
. ,
. ,
:

order() ;
receive() .

class inventory
{
public:
void order (int wid);
// wid
void receive (int wid);
// wid
private:
list on_hand;
list on_order;
};

,
. find()
, () .
.
void inventory::receive(int wid)
{
cout << " " << wid << endl;
list::iterator weNeed = find(on_order.begin(),
on_order_end(),
wid);
if ( weNeed != on_order.end() )
{
cout << " " << Widget(wid)
<< " " << endl;
on_order.erase(weNeed);
}
else
{
on_hand.push_front(Widget(wid));
};
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
find_if() , ,
. ,
, .
:
class WidgetTester
{ public:
WidgetTester(int t) : testid(t) { }
const int testid;
bool operator () (const Widget & wid)
{ return wid.id == testid;
}
};

, , :
void inventory::order(int wid)
{
cout << " " << wid << endl;
list::iterator weHave = find_if(on_hand.begin(),
on_hand.end(),
WidgetTester(wid));
if ( weHave != on_hand.end() )
{
cout << " " << *weHave << endl;
on_hand.erase(weHave);
}
else
{
cout << " " << wid << endl;
on_order.push_front(wid);
};
}

16.4. :
. map
, -.
, ,
. , ,
,
.
. 16.1.

. 16.1.

PDF created with pdfFactory Pro trial version www.pdffactory.com

16.1.
typedef map stringVector;
typedef map graph;
string pendleton("Pendleton");
string pensacola("Pensacola");
string peoria("Peoria");
string phoenix("Phoenix");
string pierre("Pierre");
string pittsburg("Pittsburg");
string princeton("Princeton");
string pueblo("Pueblo");
graph cityMap;
cityMap[pendleton][phoenix] = 4;
cityMap[pendleton][pueblo] = 8;
cityMap[pensacola][phoenix] = 5;
cityMap[peoria][pittsburgh] = 5;
cityMap[peoria][pueblo] = 3;
cityMap[phoenix][peoria] = 4;
cityMap[phoenix][pittsburg] = 10;
cityMap[phoenix][pueblo] = 3;
cityMap[pierre][pendleton] = 2;
cityMap[pittsburg][pensacola] = 4;
cityMap[princeton][pittsburg] = 2;
cityMap[pueblo][pierre] = 3;

stringVector , .
graph ,
.
. , . 16.1, 16.1.
.
.
, .
/. .
.
DistancePair :
struct DistancePair
{ unsigned int first;
string second;
DistancePair() : first(0) { }
DistancePair(unsigned int f, string & s)
: first(f), second(s) { }
};
bool operator < (DistancePair & lhs, DistancePair & rhs)
{ return lhs.first < rhs.first; }

.
,
. ,
.
void shortestDistance(const graph & cityMap,
const string & start,
stringVector & distances)
{
//
priority_queue,

PDF created with pdfFactory Pro trial version www.pdffactory.com

greater > que;


que.push(DistancePair(0, start));
while (! que.empty() ) {
//
int distance = que.top().first;
string city = que.top().second;
que.pop();
// ,
if (0 == distances.count(city))
{
//
//
distances[city] = distance;
//
stringVector::iterator start, stop;
start = cityMap[city].begin();
stop = cityMap[city].end();
for (; start != stop; ++start)
que.push(DistancePair(distance + (*start).second,
(*start).first));
}
}
}

16.5. :
.
multimap. multimap ,
.
,
, .
multimap,
( string). ( ).
, .
.
:
class concordance //
{
typedef multimap wordDictType;
public:
concordance() : wordMap() { }
void addWord (string, int);
void readText(istream &);
void printConcordance(ostream &);
private:
wordDictType wordmap;
};

.
( ),
. - readText()
printConcordance(). :
void concordance::readText(istream & in)
// ,
//

PDF created with pdfFactory Pro trial version www.pdffactory.com

{
string line;
//
for (int i=1; getline(in, line); i++)
{
allLower(line); //
list words;
split(line, " ,.;:", words); //
list::iterator wptr; //
for (wptr = words.begin(); wptr != words.end(); ++wptr)
addWord(*wprt, i);
}
}

.
.
split(). - .
,
STL:
void split(string & text, string & separators,
list & words)
{
int n = text.length();
int start, stop;
// ,
start = text.find_first_not_of(separator);
while ( (start >= 0) && (start < n) )
{
//
stop = text_find_first_of(separators, start);
if ( (stop < 0) || (stop > n) ) stop = n;
//
words.push_back(text.substr(start, stop-start));
//
start = text.find_first_not_of(separators,stop+1);
}
}

split()
:
void concordance::addWord(string word, int line)
{
// ,
//
wordDictType::iterator low =
wordMap.lower_bound(word);
wordDictType::iterator high =
wordMap.upper_bound(word);
// ,
for (; low != high; ++low)
if ( (*low).second == line ) return;
// ,
wordMap.insert(make_pair(word, line));
}

addWord() , , -
. ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
,
/ .
. :
void concordance::printConcordance(ostream & out)
{
string lastword("");
wordDictType::iterator pairPtr;
wordDictType::iterator stop = wordMap.end();
for(pairPtr = wordMap.begin(); pairPtr != stop;
++pairPtr)
// , ,
//
if ( lastword == (*pairPtr).first)
out << " " << (*pairPtr).second;
else
//
{
lastword = (*pairPtr).first;
out << endl << lastword << ": " <<
(*pairPtr).second;
}
cout << endl; //
}

, .
;
. , ,
It was the best of times,
it was the worst of times.

best worst:
best: 1
it: 1 2
of: 1 2
the: 1 2
times: 1 2
was: 1 2
worst: 1

16.6.
, STL . ,
. STL C++,
- ?
, . -
.
.
STL ( ) ,
- ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

- ,
- .

1. ,
(, 15).
- .
?
2. (, ).
,
, ?

17:
1 ,
.
. ,
, ,
(., , [Gillet 1982] [Fairley 1885]).
-
.
.
.
, .
, ,
.
:
,
. Smalltalk, ,

. , .
, , .
, Object Pascal Apple ,
. ,
, .
.
.
(, . .) ,
, . ,
. ,
.

17.1.
,
[Stevens 1981] . ,
, , .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
( ) ,
, . ,
,
. ,
.

17.1.1.
.
, .
:

;
;
;
- ;
1.

,
.
, , .
,
- .
,
. -,
, .
.
,
(file scope), , .
(program scope), , ,
.
, , ,
, .
-
,
.
-. (
.)
,
. Java ,
.
,
, . ,
, ,
, , .
,
. ,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, (
).
, ,
, (
).
,
,
.
,
(, ).
.
-
. (
).
.
,
( ),
( )
.

17.1.2.

. ,
( ) ( ) :

;
;
;
;
;
;
.

,

. . -
, ,
, .
,
( ), ,
. (, . .)
, ,
.
, ,
.
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
.
, ( )
, /.
.
,
.
. -, ,
. ( ,
.
.)
.
.
, ,
(, , ),
. ,
.
,
, ( CRC- 2).
:
1. , , ,
, ( )
. ,
.
2. , (,
, , , , , ), ,
, . :
, ,
, .
3. , ,
, , , .
,
.
.
4. ,
.

17.1.3.
1 ,
.
.
, ,
.
, ,
. .

PDF created with pdfFactory Pro trial version www.pdffactory.com

17.1.4.
(
) (
60 ). ,
( )
.
, 60 ,
.
,
.
,
goto . goto
, . , goto
, ,
, , goto .
, goto.
, ,
.
: -
?

- Demeter.
[Lieberherr 1989a, Lieberherr 1989b].
: .
.
. M C :

C;
, M (
C). , M,
.

, ,
.
( ).
:
1. ( self).
2. .
3. ( ,
).
4. , .

, .
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

( ).
:
1.
2.
3.
4.

( self).
, .
.
, .

, ,
, . ,
, ()
.
(
). ,
(
, ). -
, ,
[Wirfs-Brock 1989a].
,
.

17.1.5. :
, ,
, . -
.
.
:
?
, (, C++),
. , C++
,
( private)
.
.
,
.
(, Smalltalk)
,
.

17.1.6.
[Stefik 1986] ,
, .
, ,
, - . ,
Reactor,
.
(, ).

PDF created with pdfFactory Pro trial version www.pdffactory.com

, : setHeat,
getHeat. :
@interface Reactor : Object
{ ...
double heat; ...
}
- (void) setHeat: (double) newValue;
- (double) getHeat;

, ,
,
.
. , Reactor.
(, , ,
, .)
, Reactor (
GraphicalReactor), setHeat.

(. ). ,
Reactor, GraphicalReactor. , ,
. Reactor
setHeat,
.
@implementation GraphicalReactor : Reactor
- (void) setHeat: (double) newValue
{
/* , */
[ super setHeat: newValue ];
}
@end

Smalltalk Objective-C ,
. 17.4.

17.2. - -
, , , :
(public) (private). (,
), .
, ,
. , ( ),
.
, ,
.
[Snyder 1986] ,
- ,
, ,
. , ,
,

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
.
,
, .
, -
-.
, , .
, 8 , Card
r s ( ) .
Card .
, CardPile : private,
protected public. firstCard
CardPile, x y
, .
.
,
. (
.
, private protected.)

- -.
, :
.
, (, ,
). , ,
.
:
.
(
),
. , ,
.
- ,
, .
, .
.
,

, .

17.3.

- .
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

17.3.1. Smalltalk
Smalltalk
.
- .

.
, ,
. , ,
, .
private. ,
-.
.

17.3.2. Object Pascal


Object Pascal Apple
. ,
-, -.
,
-.
( )
, - .
, ,
.
Borland . Delphi
public, protected private ,
C++. implementation unit
.
.

17.3.3. C++
C++
. ,
: public, protected private.
,
17.2. ,
public:, , -. ,
protected:,
-, -. ,
private: ,
: , .
- private.
C++
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ,
. :
class Sneaky
{
private:
int safe;
public:
// safe 10
Sneaky() { safe = 10; }
int &sorry() { return safe; }
}

safe private, sorry.


,
Sneaky x;
x.sorry() = 17;

safe 10 17 , sorry
.
, C++
, . , ,
:
int i; //
class A
{
private:
int i;
};
class B : public A
{
void f();
};
B::f()
{
i++; // : A::i private
}

, f i,
A, ( private:).
, , i A
i.


, . ,
private, C++ ,
.
, C++
. C++
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, . rp ip,
,
private:
class Complex
{
private:
double rp;
double ip;
public:
Complex (double a, double b)
{ rp = a; ip = b;
}
Complex operator + (Complex & x)
{
return Complex(rp+x.rp, ip+x.ip);
}
};

+
. rp ip, -
x, .
, Complex ,
public. protected ,
(. )
, private

.

(protected).
(private). C++
[Sakkinen 1988b].
C++ ,
,
.
, , C++ Delphi
Pascal, , ,
. (
), .
,
, .

public private
. ,
. ,
(public), ,
: . (private),
.
, ,

PDF created with pdfFactory Pro trial version www.pdffactory.com


.
,
( ).

. public ,
, ,
. Dog
Mammal, , , Dog ,
Mammal.
,
. ,
SymbolTable (
Dictionary private) ,
Dictionary. Dictionary,
SymbolTable ( ,
).


C++ .
( ), friend
.
, private, protected.
,
:
class Complex
{
private:
double rp;
double ip;
public:
Complex(double, double);
friend double abs(Complex&);
};
Complex::Complex(double a, double b)
{
rp = a; ip = b;
}
double abs(Complex& x)
{
return sqrt(x.rp*x.rp + x.ip*x.ip);
}

rp ip , ,
private, . abs,
,
, ( ).
friend
, , .

PDF created with pdfFactory Pro trial version www.pdffactory.com


.
,

;
,
, , abs(x), x.abs().

,
. ,
,
. , ,
- (, )
. ,
,
( ) .
[Koenig 1989c].


C++ (namespace).
namespace . static
.
,
, .
namespace:
namespace myLibrary
{
int x;
class A
{
...
};
class B : public A
{
...
};
...
}

, namespace, .
- , .
, ,
, :
using namespace myLibrary;


, :
myLibrary::A anA; //
using myLibrary::B; // B
B aNewB;
// B

PDF created with pdfFactory Pro trial version www.pdffactory.com


C++ const ,
. ,
, . ,
const ,
.
,
, . ,
,
Complex (. ), ,
. 3 .
const.
, C++
,
(. 7,
). :
class Complex
{
public:
const double rp;
const double ip;
Complex(double, double);
};
Complex::Complex(double a, double b) : rp(a), ip(b)
{
/* */
}

rp ip const,
public,
. , , -,
rp ip, .
.
, .
, , .
const -.
.

C++
. , ,
,
.
, test
:
class A
{
public:

PDF created with pdfFactory Pro trial version www.pdffactory.com

void test(int a)
{
cout << "This is the integer version\n";
}
void test(double b)
{
cout << "This is the floating point version\n";
}
};

, C++
, ,
, .
, ,
. :
class A
{
public:
void test(double b)
{
cout << "This is the floating point version\n";
}
};
class B : public A
{
public:
void test(int a)
{
cout << "This is the integer version\n";
}
};

test B
, ,
test ( , B),
. , ,
A.
, test virtual . ,
B,
:
class B : public A
{
public:
void test(double b)
{
A::test(b);
}
void test(int a)
{
cout << "This is the integer version\n";
}
};

17.3.4. Java
Java, private public
-.

PDF created with pdfFactory Pro trial version www.pdffactory.com

Java final.
.
. , final, .
final .
C++, private Java , .
.
, Java,
. .
.
package, :
package packageName;

,
, , .
:
// foo bar
bar.foo newObj = new bar.foo();


, :
//
// bar
import bar.*;


*.
// foo
// bar
import bar.foo;

17.3.5. Objective-C
Objective-C
. (
, ),
( C++ ).
@public,
, , .
, Ball,
-. ,
x y. ,
.
@interface Ball : Object
{
double direction;
double energy;
@public
double x;

PDF created with pdfFactory Pro trial version www.pdffactory.com

double y;
}

implementation
, .
,
.
, -,
-.
.

17.4.
, ,
, . , ,
. ( ),
.
,
, .
, , (,
: ).
?

.
run-time Smalltalk Objective-C,
(, C++). ,
, ,
. .
- ,
, -. -
.
,
.
, ,
.

, .
, 17.1.6, ,
, - . , ,
Reactor,
.

1. , ,
, - .

PDF created with pdfFactory Pro trial version www.pdffactory.com

2. .
.
, .
3. ,
? .
, [Wulf 1973].
4. , ,
, :
o
;
o , , .
.
,
?
5. ( -)
.

:
. : (
) . C++,
,
, private protected.
(, Smalltalk) .
.
6. -
, ,
. (.
[Snyder 1986]).
7. C++ Delphi Pascal
, -, ,
-.
.
( )
, .
, .
,
.

18:
.
, .
, .
, ,
.
.
, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

: .
.

18.1.
, .
,
.
GUI. 19.
.
, ,
, , [Gamma 1995, Deutsch 1989,
Weinand 1988].
,
, . , ,
,
. Model-View-Controller
Smalltalk OWL PC.
. ,

. ,

.
,
.
, LAF, 19,
. application ,
: .
, . ,
, application.
(,
). ,
, 8.
LAF.
class cardApp : public application
{ public:
//
cardApp();
// ,
virtual void mouseButtonDown(int x, int y);
virtual void paint();
private:
CardPile * piles[13];
};

,
. ,
, , , .

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
. , LAF ,
, ,
.
, .
, ,
,
(, /)
.


. ,
, . ,
.

, .
[Wilson 1990].

18.1.1. Java API


Java API (Application Programming Interface,
). ,
Java, .
LAF, , API,
.
Java- Applet.
main,
. , ,
, .
:
init ()
start ()
paint (Graphics)
mouseDown (Event, int, int)
keyDown (Event, int, int)
stop ()

PDF created with pdfFactory Pro trial version www.pdffactory.com

, API
, ,
, .

18.1.2.
,
, ,
, , (. 6).
,
:
GraphicalObject = object
(* *)
link : GraphicalObject;
region : Rect;
(* *)
procedure setRegion(left, top, right, bottom :
integer);
(* , *)
procedure draw;
procedure erase;
procedure update;
function intersect(anObj : GraphicalObject) :
boolean;
procedure hitBy(anObj : GraphicalObject);
end;

, , ,
. ,
,
:
GraphicalUniverse = object
(* *)
moveableObjects : GraphicalObject;
fixedObjects : GraphicalObject;
continueUpdate : boolean;
(* *)
procedure initialize;
procedure installFixedObject (newObj :
GraphicalObject);
procedure installMovableObject (newObj :
GraphicalObject);
procedure drawObjects;
procedure updateMovableObjects;
procedure continueSimulation;
end;


. ,
. -
( continueSimulation),
. :
procedure GraphicalUniverse.updateMovableObjects;
var
currentObject : GraphicalObject;

PDF created with pdfFactory Pro trial version www.pdffactory.com

begin
repeat
continueUpdate := false;
currentObject := movableObjects;
while currentObject <> nil do
begin
currentObject.update;
currentObject := currentObject.link;
end
until not continueUpdate
end;

,
, , ,
, ,
, .
.
, .
.
, .
[Budd 1994].

18.2.
,
.
.
, ,
.
.

.
, .

18.2.1.
.
: . -,
, ().
,
. .
.
. , ,
.
. .
,
.

.
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .

, .

( ). ,
, , .
, , .

( , ).
, .
. ,
(, ), .
, ,
.
. ,
,
.

( ). - ,
,
, .
.
,
().

,
, ,
. , ,
,
,
(),
. .
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

18.2.2.
,
(, ).
, . ,
, , .

. ,
, .
, :
template class TreeNode
{
public:
TreeNode (T & initial);
void visit (Visitor & v);
private:
T value;
TreeNode * left;
TreeNode * right;
};


; .
Visitor.
action. visit TreeNode
,
action.
, ( , )
:
TreeNode::visit (Visitor & v)
{
v.action(value);
if (left != NULL) left->visit(v);
if (right != NULL) right->visit(v);
}

- action. visit
, . ,
, Visitor.
. , , ,
, .
.
, , .
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, , ,
, .
, .
: ?
, .
- .
, ,
, . . , ,
, .
:
,
. , , ,
, .
. ,
.
, ,
. ,
( , ).

.

. ,
. ,
, ,
. :
initialize
current
atEnd

advance



true,

, , :
itr.initialize();
while not itr.atEnd() do
begin
... (* itr.current() *)
itr.advance();
end;

.
, ,
.

18.2.3.
, ( )
. , ,
, , ,
. .

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
Shape, (Triangle, Square . .). ,
: .
Device. , ,
, .
,
:
class Triangle : public Shape
{
public:
Triangle (Point, Point, Point);
// ...
virtual void displayOnPrinter (Printer);
virtual void displayOnTerminal (Terminal);
// ...
private:
Point p1, p2, p3;
};

: Shape ( )
Device? ,
.
. (

.)
, Shape .
Device
.
:
function Printer.display (Shape aShape)
begin
aShape.displayOnPrinter(self);
end;
function Terminal.display (Shape aShape)
begin
aShape.displayOnTerminal(self);
end;

: display ,
. displayOnPrinter displayOnTerminal
( Shape ).
, Triangle.
, Triangle, (
) :
procedure Triangle.displayOnPrinter (Printer p)
begin
// ,
//
// ...
end;
procedure Triangle.displayOnTerminal(Terminal t)
{
// ,
//
// ...
}

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
. ,
. ,
( ) [Ingalls 1986,
Budd 1991, LaLonde 1990a, Hebel 1990] 1 .
.

18.2.4.
, ,
,
.

.
,
,
.
[Gamma 1995, Coplien 1995, Pree 1995].
,
.
. ,
. ,
.
, ,
, .
.
.

1. .
, ?
2. ,
18.1.2.
3. ?
4. , ,
(), ?
5. ,
(, Smalltalk)

. , , ,
, ?

[Ingalls 1986],
.
,

PDF created with pdfFactory Pro trial version www.pdffactory.com

19: :
18, ,
, . ,

. , GUI (Graphics User
Interface ).

LAF (LAF Little Application Framework). ,
,
. LAF ,
. , LAF
,
.
LAF .
, LAF
. LAF Macintosh, PC
UNIX-.

19.1. GUI
LAF GUI: , ,
, , .
,
(, , ), . ,
button, , .
, .
LAF,
, ,
.

19.2. ,
,
,
.
.
, .
, ,
, . ,
/,
.
, , .
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

, . ,
.
, , , ,
.
, .
program main;
begin
initialize application;
while not finished do
begin
if user has done something interesting
then respond to it
end;
clean up application;
end

, , .
, ,
.
. , , ,
, .
, , .
-
, , .
,
, .


. , GUI.
ple ,
, [Wilson, 1990].
,
. GUI ,
,
,
, .

19.3.
,
, .
, .
, , ,
:
1. . ,
,
.
, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

2. . ,
. ,
.
-
. ,
.
3. . .
, ,
, .
.

, ,
,
.
( )
( ),
.

19.4. LAF
LAF : application, button, menu, menuItem,
staticText editText. , ,
application, button menuItem. .
application,
.
run, application. ,
. ,
quit, , quit(),
application.
. ,
:

;
;
run :

class simpleApp : public application


{
public:
simpleApp() : application("simple application")
{ }
};
void main()
{
simpleApp theApp;
theApp.run();
};

,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

19.5. application
, application, . 19.1.
run() , main. run()
. quit()
.
update() clearAndUpdate() ,
. -
,

19.1. application

. clearAndUpdate()
. , paint().
paint() .
paint() .
, .

update() clearAndUpdate().

PDF created with pdfFactory Pro trial version www.pdffactory.com

, mouseButtonDown().

. , ,
. - ,
application.
, , - ,
.
update(),
clearAndUpdate() .
, keyPressed().
, .
top(), bottom(), left() right()
, . ,
, (0,0)
. , ,
, (0,0)
.
application
. .
clearAndUpdate(), ,
, , . ,
( )
paint(). ,
. , (0,0) ,
OX , OY .
.

19.5.1. button
button
pressed. ,
:
class quitButtonClass : public button
{
public:
quitButtonClass (window * win)
: button (win, "Quit", 5, 5, 20, 50)
{ }
protected:
pressed (window * win)
{
...
}
};

.
,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com


, :
class newApplication : public application
{
public :
newApplication : application ("new program"),
quitButton(this)
{ }
...
private:
quitButtonClass quitButton;
};

-
. ,
,
. C++
,
1. :
template
class tbutton : public button
{
public:
tbutton (window * win, char * t,
int x, int y, int h, int w,
void (T::*f)() )
: button(win, t, x, y, h, w), fun(f)
{ }
protected:
void (T::* fun) ();
virtual void pressed (window * win)
{
(((T *) win)->*fun) ();
}
};

, f, -
- . , ,
-, .
fun .

:
class helloApp : public application
{
public:
helloApp() :
application("hello world"),
quitButton (this, "quit", 5, 5, 50, 20, quit),
clearButton (this, "clear", 5, 30, 50, 20,
clearAndUpdate)
{ };
virtual void mousButtonDown(int, int);
private:
tbutton quitButton;
tbutton clearButton;
};

PDF created with pdfFactory Pro trial version www.pdffactory.com

19.5.2. menu menuItem


. LAF
: menu menuItem.
, .
, .
, selected .

menuItem selected. ,
.

, :
lass clearMenuItem : public menuItem
{
public:
clearMenuItem (menu & m, char * t) : menuItem(m, t)
{ }
protected:
virtual void selected (window *);
};
class helloApp : public application
{
public:
helloApp ()
: application("hello world")
clearMenu (this, "Clear"),
clearItem (clearItem, "clear/C")
{ };
virtual void mouseButtonDown(int, int);
private:
menu clearMenu;
clearMenuItem clearItem;
};
void clearMenuItem::selected (window * w)
{
w->clear () ;
}

tmenuItem tbutton. , tbutton


, ,
-. tmenuItem
menuItem , -. ,
,
.
template
class tmenuItem : public menuItem
{
public:
tmenuItem (menu & m, char * t, void (T::* f) () )
: menuitem(m, t), fun(f)
{ }
protected:
void (T::* fun) ();
virtual void selected (window * win)
{
(((T*) win)->*fun)();

PDF created with pdfFactory Pro trial version www.pdffactory.com

}
};

LAF (
). LAF :
LAF,
ftp://ftp.cs.orst.edu/pub/budd/laf.

19.6.
LAF ,
(
),
. , :

.
LAF .
,
. LAF , ,
, (,
).
. ,
LAF ,
. .
,
. LAF
,
, , . .

1. LAF .
2. ?
application LAF.
3. ,
,
. ,
update , ,
.

20:
, .
, .
, , ,
- . , ,
. .
.
? , ,
, .
. , C++ Object Pascal,

PDF created with pdfFactory Pro trial version www.pdffactory.com

, . , Smalltalk
Objective-C, , .
.

20.1.
,
. ,
. ,
[Wegner 1986], ?:
.
, .
(- ).
,
.
, .
. ,
. ,
, ,
.
.
:
. ,
.
. ,
.
.
, ( )
.
.
.
.
.
, C++ Object Pascal,
. ,
.
, (
, ,
).

.
,
. , , ,

.
,
.
, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

20.1.1.
, , ,
, .
1 .
,
, , .
,
.
, .
,
. ,
10.
,
,
, -
. ( ,
,
.) , , , ,
.
,
, , , .
(. 7) (,
) ,
.

:
,
8, draw TablePile,
. ,
,
. ,
, , .
,
. ,
, .
, , -
, ,
, . ,
1

, [Gries 1981].
, .
. (
)
. (
)
. . ,
,
. . .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 20.1.
7, . ,
(, ParallelCard),
Card draw. .
Card
, ParallelCard.
class ParallelCard : public Card
{
// ,
public:
void draw()
{ if (!fork()) //
{ Card::draw(); exit(0); }
}
};

, ,
, , .
, ( ),
, ,
. 20.1.
,
( , -
). draw.
, , ,
.
, .
, , ,
, ,
, .
, ,
[Perry 1990].


- Eiffel [Meyer 1988a, Rist 1995]
.
. (

PDF created with pdfFactory Pro trial version www.pdffactory.com

). ,
.
. ,
,
, .
, Java :
final. , final, ,
.

20.1.2.
.
,
(, ).

, , , . ,
12,
. ,
, :
,
;
, .
, ,
, .
,
.

20.2.
, -
.

. ,
,

. 20.2. CRC- Class

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 20.3.
. ,
. , ( ) ?

20.2.1.
, .
.
.
, :
( ) .
,
, , ,
, .
, ,
.
, ,
.
. ,
. .

20.2.2. Class
,
. ?
Smalltalk, Objective-C, Java ,
, Class. . 20.2 CRC- ( ) Class
Little SmallTalk. ,
, , . Set
, . 20.3.
.
new,
Class.
, .
, ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
. Class Object Class
Object . , Object
Class Object Class.
Class .
, Set, Set
Class, , , () Object.
set Set.
. 20.3,
, .

20.2.3. -
,
. ,
, , ,
.
, , .
,
:
.
,
, . 20.2. , ,
, .
.
, ,
, .
, ,
( ).
, ,
.
. Smalltalk-80
, .
, .
,
. Class,
Object.
Smalltalk-80:
Object
Collection
Bag

:
Object
Class ,
Metaclass-Object
Metaclass-Collection collections

PDF created with pdfFactory Pro trial version www.pdffactory.com

Metaclass-Bag bags

, , .
, Bag ,
. Bag
20.1. newDay:year: Date
newDay: dayount year: referenceYear
" , dayount "
" referenceYear "
day year daysInYear
day <- dayCount.
year <- referenceYear.
[ day > (daysInYear <- self daysInYear: year) ]
whileTrue:
[ year <- year + 1
day <- day daysYear ].
[day <= 0]
whileTrue:
[ year <- year 1
day <- day + (self daysInYear: year) ]
self new day: day year: year

new, .
new, Metaclass-Bag
Class. :
new
" "
super new setdictionary

Bag Metaclass-Bag,
20.2. daysInYear: Date
daysInYear: yearInteger
" , yearInteger "
365 + (self leapYear: yearInteger)
leapYear: yearInteger
" 1, yearInteger , "
" 0 "
( yearInteger \\ 4 ~= 0 or:
[ yearInteger \\ 100 = 0 and:
[ yearInteger \\ 400 ~= 0 ]])
ifTrue: [ 0 ]
ifFalse: [ 1 ]

new. ,
, ,
, CRC-, .
. ,
setDictionary. , ,
:
setDictionary
" "

PDF created with pdfFactory Pro trial version www.pdffactory.com

contents <- Dictionary new

.
Smalltalk-80. ,
,
, -
. , ,
, . ,
Smalltalk-80 - ,
. ,
. Objective-C ,
( ), , ,
, -.

[Kiczales 1991].

20.2.4.
, , ,
,
. ,
, ,
. ( ),
.
Date Smalltalk-80.
. Date
: 1 366, .
Date . , Date today
Date, .
.
, 20.1. ,
, .
, , new
.
daysInYear:, , 20.1,
: ,
, - . Date

. ,
,
, . daysInYear leapYear
(-), , 20.2.

20.2.5. Objective-C

Objective-C. ,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com


. GraphicalReactor 12
ParallelCard, .
,
.
Objective-C ,
.
.
. ,
:
[ GraphicalReactor poseAs: [ Reactor class ]];

Reactor,
, GraphicalReactor. ,
, .

20.3.
//
// --------- Card
//
class Card
{
public:
//
Card(int s, int c);
//
static const int CardWidth;
static const int CardHeight;
...
};
const int Card::CardWidth = 68;
const int Card::CardHeight = 75;

( ).

20.3.
, ,
, . ,
Windows Cards
.
.
,
. ,

. , -
. , ,
, .
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

.
: ,
. :

;
.

Smalltalk, Objective-C , initialize


. initialize
. initialize
,
initialize. Java ,
.
,
, , , .
,
.

20.3.1. Smalltalk
,
. , Date. 20.2.4,
Date .
,
, ,
. .
, .
initialize.
Magnitude subclass: #Date
instanceVariableNames: 'day year'
classVariableNames:
'DaysInMonth FirstDayOfMonth MonthNames SecondsInDay
WeekDayNames'
poolDictionaries: ''
category: 'Numeric-Magnitudes'

20.3.2. C++
C++ static,
. , ,
. (
) C++.
( private, protected public)
,
.
,
,
( 20.3).
,
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

,
(public) . ,
CardPile :
void CardPile::display()
{
if (top == nilLink)
game->clearArea(x, y, x+Card::CardWidth,
y+Card::CardHeight);
else
top->draw();
}

CardWidth CardHeight Card ,


.
,
, . C++
, static.
Smalltalk Objective-C.

20.3.3. Java
Java, C++, static .
, , static,
, .
,
. , static,
. .
, 1
12:
class statTest
{
static final size = 12;
static int arr[] = new int[size]; //
static { //
for (int i = 0; i < arr.length; i++)
{
arr[i] = i + 1;
}
}
}

static final ,
.
,
. .

PDF created with pdfFactory Pro trial version www.pdffactory.com

20.3.4. Objective-C
Objective-C .
, (static) C
implementation . ,
, - . , Date
dayNames. ( C++ ,
,

. 20.4.
, .)
# import "Date.h"
static char *dayNames[ ] = {"",
"", "", "", "",
"", ""};
@implementation Date
...
@end

20.4. ?
, , , ,
, -
? , , ,
-
, . ,
- .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 20.5.

20.4.1. ?
- ,
. , , ,
- ,
.
; .
,
, ,
. ,
, .
, .
, , , ,
.
.
, .

20.4.2.

[Lieberman 1986].
,
. ,
,
-. , ,
, .
. .
, (
) (
).

.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ,
,
drawFromTo(a, b, c, d).
(a,b) (c,d).
, ,
. - , x y,
: getX(), getY(), setX(a), setY(b).
- , moveTo(a,b),
, drawTo(a,b), .
:
method moveTo(a, b)
self setX(a)
self setY(b)
end
method drawTo(a, b)
self drawFromTo(self getX(), self getY(), a, b)
self moveTo(a,b)
end

- drawFromTo - (. 20.4).
, .
, , (
) .
: ,
. ,
setX . .
, ,
; .
, ( )
.
self ( ),
. setX getX, , ,
drawTo, , .
,
. , ,
.
. ,
,
x y, (. 20.5).
, drawTo;
. x y
,
. drawTo :
method
self
self
self
self
self
end

drawTo(a, b)
drawFromTo(self getX(), self getY(), a, b)
drawFromTo(- self getX(), self getY(), a, b)
drawFromTo(self getX(), self getY(), a, b)
drawFromTo(- self getX(), self getY(), a, b)
moveTo(a,b)

PDF created with pdfFactory Pro trial version www.pdffactory.com

, ,
, [Abelson 1981].
,
.
,
, turn(amount), forward(amount) backward(amount).
, ,
.
,
dasedPen,
, . (, ,
, .)
/
/, ,
.
- -, ,
. ,
, ,
.
[Lieberman 1986].
,
.
[Stein 1987].
Self, , [Ungar 1987].
[Tomlinson 1990]
, ,
, , .

1.
[Wegner 1986]? , .
2.
( [Gries 1981,
Dijkstra 1976]).

21: -

. ,
- ,
. , , .
,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

21.1.

: .
, .
,
.
, 1 .
, ,
, , .
, ,
, . ,
,

.
, .
.
, , , ,
,
, , , . C++
, C++. , Smalltalk
, Smalltalk.

21.2.
,
.
. ,

, ,
1.
, .
, x d,
y. , x
20, d 28. y
d.
x:= d.y

,
, ,
:
1

,
. ,
, (
). ,
,
.
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

move AR+36, AR+20

:
, .
,
. , (
) .
, ,
, -, , -.

(. 21.1).

. 21.1.
GraphicalObject Ball , 6.
GraphicalObject link region, Ball
direction energy, -
. ,
, : , ,
,
, (
) . , moveTo GraphicalObject
-,
region .

21.2.1.
, ,
, ,
.
, , .
.
, ,
,
, . -
, ,
.
, ,
, , ,
. , :

PDF created with pdfFactory Pro trial version www.pdffactory.com

? 12,
:

, ;
.
,
, ().

,
, .
, , .

21.2.2.
-
,
() .
.
Wall draw , Ball.
- ,
, . ,
, , ,
. ,
,
.

21.2.3.

, ,
.
, . 21.2.
,
, , ,
. , ,
.
( ) . ,
,
. ,
,
. ,
,
(. 21.3).
.

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 21.2. ,

. 21.3.
.
,
, .
,
.
, -
-,
. , ,
.
, .
. 21.4 Wall Ball,
GraphicalObject. ,
, ,
, .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 21.4. Wall Ball


, ,
.
, self
(this C++). , , vtab ,
x,
hitBy 12.
x.hitBy(y)

:
(*(*(x.vtab))[12]) (x,y)

, ,
, x Ball, Wall,
- GraphicalObject.

.

21.2.4.

, ,
. .
(linkers) (loaders)
( ), ,
, ,
. .
, draw Ball , , Ball::draw

PDF created with pdfFactory Pro trial version www.pdffactory.com

. ,
, .
, C++,
,
1 ,
. , Complex,
, Complex::Complex,
Complex::Complex_float Complex::Complex_float_float.
- (mangled). . ,
;

.

. , , .
[Ellis 1990].

21.2.5.
, C++ Object Pascal,
,
.
, ,
. (
Objective-C) ,
, . ,
20 10 ,
20 , 200 .
,
.

. 21.5.
, , ,
, /.
. ,
.
,
(. 21.5).
,
( )

PDF created with pdfFactory Pro trial version www.pdffactory.com

, .
isa (isa link) (
(is-a relation) ). Objective-C

[neighbor checkrow: row column: column]

Objective-C1
objc_msgSend(neighbor, "checkrow:column:", row, column)

objc_msgSend, ,
isa ,
.
, .
, . ,
.
Object, ,
.



,
, . ,
.
, ,
.
,
.
- , .
- 2
. -
, (
), .
,
, - (. 21.6).
-
, .
, .
-. ,
- (
2

(hashing) .
- (hash-code),
- . -
, , , (
-).
.
. -. . .

PDF created with pdfFactory Pro trial version www.pdffactory.com

), . ,
, , ,
.

. 21.6. ,
-
9095 ,
, ,
[Cox 1986].
.

21.3.
,
,
. . ,
,
( ObjectiveC ,
, ). ,
.
, ,
, -,
. . 21.7
- Little Smalltalk.
, .
, ,
. (
) .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 21.7. - Little Smalltalk


,
switch (case, select . .). -,
,
.
( [Budd 1987])
.
while (timeslice-- > 0)
{
high = nextByte();
// -
low = high & 0x0F;
// 4
high >>= 4;
//
if (high == 0)
// ?
{
// ,
high = low;
// low
low = nextByte();
//
}
switch (high)
{
case PushInstance: ...
...
case PushArgument: ...
...
}
}


, Smalltalk
. , ,
20, . , , , ,
(. 21.8). .

PDF created with pdfFactory Pro trial version www.pdffactory.com

. 21.8.
,
. ,
.
, , .
, ,
, ,
.
. ,
.
,
.
Java -,
.


,
- ,
[Cox 1986], /
. C++
[Ellis 1990], Simula
[Krogdahl 1985]. C++
[Lippman 1996].
Smalltalk-80 [Goldberg 1983]. [Krasner
1983] ,
Smalltalk-80. Smalltalk
[Budd 1987]. [Kamin 1990]
.

1.
?
2. Objective-C
. ,
, . ,

.

PDF created with pdfFactory Pro trial version www.pdffactory.com

3. , , C++ ,
, .
, , ?
4. , 21.2. ,
, -, , ,
, . ,
-, .
, ?
.
5. Smalltalk, , .

PDF created with pdfFactory Pro trial version www.pdffactory.com