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

Baepeuue

H nauan nnca1L a1y knnry a 1988 ropy. B konue 1990 ropa yanpeno cae1 ee nepaoe
nspanne. 3a aoceHL ne1, npouepune c nauana pa6o1m nap knnro, Hm c1ann canpe1ennHn
nsHenenn a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn, no1pe6oaaaunx
snaun1enLnmx ncnpaanenn n po6aanenn a 1ekc1e. K nnH Hoxno o1nec1n cnepykuee:
Bonee rny6okoe nonnHanne pasnnunn Hexpy nopknaccaHn n nop1nnaHn n npnsnanne
1oro qak1a, u1o sauac1yk a1o paneko ne opno n 1o xe.
Bmc1pm poc1, aaonkunn n c1anpap1nsaunn nsmka nporpaHHnpoaannn C++,
aknkuan aaepenne ua6nonoa, ncknkun1enLnmx cn1yaun, nornuecknx nepeHennmx,
npoc1panc1aa nHen, c1pok, npen1nqnkaunn 1nnoa pannmx ao apeHn amnonnennn
(RTTI run-time type identification system) n c1anpap1no 6n6nno1ekn.
Honanenne nsmka nporpaHHnpoaannn Java aocxn1n1enLnoro noaoro cpepc1aa pnn
paspa6o1kn npnnoxenn World Wide Web.
Mepnennoe ncuesnoaenne nsmka Object Pascal nocne 1oro, kak Apple nepec1ana
ncnonLsoaa1L ero a kauec1ae ocnoanoro cpepc1aa cospannn npnnoxenn pnn
koHnLk1epoa Macintosh. Opnako na PC a1o1 nsmk aosapauae1cn k xnsnn a o6pase
Delphi.
3aka1 n napenne Objective-C. [nn 1ex, k1o npoqeccnonanLno sannHae1cn nsmkaHn
nporpaHHnpoaannn, a1o 6onesnennan y1pa1a, 1ak kak pnnaHnueckn
1nnnsnpoaannm Objective-C oc1aaancn npak1nueckn epnnc1aenno anL1epna1nao
c1poro 1nnnsaunn a pyxe C++. Hoa1oHy a pannoH nspannn n npoponxnn
o6cyxpenne Objective-C.
Pasan1ne noamx nn1epecnmx o6Lek1no-opnen1npoaannmx nsmkoa (1aknx, kak Beta,
CLOS n Java), ko1opme coepnnnk1 coapeHennme n knaccnueckne npen.
C1anoanenne npepc1aanenn o coaokynnoc1nx knaccoa, a uac1noc1n nonanenne
1aknx nonn1n, kak ua6nonm konc1pynpoaannn knaccoa n cpepa paspa6o1kn
npnnoxenn.
Ho a1nH n HnornH ppyrnH npnunnaH nou1n kaxpan rnaaa knnrn 6mna nepecHo1pena. TeH
ne Henee n nonm1ancn coxpann1L o6uyk c1pyk1ypy knnrn, ko1opan Hoxe1 6m1L
npepc1aanena a anpe cnepykuero cnncka 1eH:
I. Baepeuue u o6u saHmcen. Inaaa 1 pae1 neqopHanLnoe onpepenenne 6asoamx
konuenun o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn. Inaaa 2 aaopn1
npnnunn paspao1kn na ocnoae onsannoc1e. D1n pae rnaam nannk1cn
qynpaHen1anLnmHn, n nx cnepye1 nsyun1L noppo6no. B uac1noc1n, n nac1on1enLno
pekoHenpyk amnonnn1L no kpane Hepe opno ynpaxnenne c CRC-kap1oukaHn ns
rnaam 2. Texnnka CRC-kap1ouek, no HoeHy Hnennk, nanne1cn opno ns nyuunx pnn
onpepenennn qynkunonanLnoc1n, o1ae1c1aennoc1n n nnkancynnunn npn 6asoao
paspa6o1ke npoek1a.
II. Knaccm, He1opm u coo6euun Inaam 3 n 4 onpepennk1 cnn1akcnc, ncnonLsyeHm
a nsmkax Smalltalk, C++, Java, Objective-C n Object Pascal pnn sapannn knaccoa,
He1opoa n nocmnkn coo6uenn. Inaaa 3 saoc1pne1 annHanne na c1a1nuecknx
caoc1aax (knaccax n He1opax), a 1o apeHn kak rnaaa 4 onncmaae1 pnnaHnueckne
acnek1m (cospanne o6Lek1oa n nepecmnky coo6uenn). Inaam 5 n 6 pasanaak1 a1n
npen. 3pecL xe naunnak1cn o6yuakune npnHepm o6pasum nporpaHH,
converted to PDF by BoJIoc
paspa6o1annmx a o6Lek1no-opnen1npoaanno Hanepe n nnnkc1pnpykunx
pasnnunme uep1m o6Lek1no 1exnnkn.
III. Hacnepoaauue u noa1opuoe ucnonusoaauue kopa Inaam 7, 8 n 9 aaopn1
konuenunk nacnepoaannn n o6Lncnnk1 ee npnHenenne pnn o6ecneuennn noa1opnoro
ncnonLsoaannn kopa. HpnHep ns rnaam 8, nanncannm na nsmke Java, nnnkc1pnpye1
1akxe npnHenenne c1anpap1noro npnknapnoro nporpaHHnoro nn1epqeca (API
application program interface). B rnaae 9 npo1naonoc1aannk1cn nacnepoaanne n
koHnosnunn a kauec1ae anL1epna1nanmx 1exnnk o6ecneuennn noa1opnoro
ncnonLsoaannn kopa.
IV. Bonee noppo6uo o uacnepoaauuu. B rnaaax c 10 no 13 konuenunn nacnepoaannn
anannsnpye1cn 6onee pe1anLno. Baepenne nacnepoaannn okasmaae1 annnnne na
nou1n ace acnek1m nsmka nporpaHHnpoaannn, ko1opoe sauac1yk ne cpasy oueanpno
pnn naunnakuero. B rnaae 10 o6cyxpae1cn nonck He1opoa n nx cansmaanne c
coo6uennnHn. TaH xe nnnkc1pnpye1cn 1o1 qak1, u1o nopknaccm n nop1nnm a1o
ne opno n 1o xe. B rnaae 11 o6cyxpae1cn ceHan1nka nepeonpepenennn He1opoa n
o1Heuak1cn pae coaepuenno pasnnunme nn1epnpe1aunn a1oro nonn1nn. B rnaae 12
npoponxae1cn 1eHa nepeonpepenennn n nccnepyk1cn neko1opme cnepc1ann
nacnepoaannn npnHenn1enLno k HexannsHaH ynpaanennn naHn1Lk, npncaanaannn n
cpaanennn. Hakoneu, a rnaae 13 nsyuae1cn Hnoxec1aennoe nacnepoaanne.
V. HonuHopqusH. B snaun1enLno c1enenn HouL o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn nponc1ekae1 ns npnHenennn pasnnunmx qopH nonnHopqnsHa. B
rnaae 14 un1a1enL snakoHn1cn c ocnoanmHn HexannsHaHn nonnHopqnsHa a
o6Lek1no-opnen1npoaannmx nsmkax n payHn nokasa1enLnmHn o6yuakunHn
npnHepaHn. Hepam npnHep a rnaae 15 paccHa1pnaae1 cospanne 6n6nno1ek o6uero
nasnauennn. Konkpe1nan 6n6nno1eka, a nHenno nepaano paspa6o1annan
c1anpap1nan 6n6nno1eka ua6nonoa (STL Standard Template Library) pnn nsmka
C++, o6cyxpae1cn a rnaae 16.
VI. Paspa6o1ka nporpaHHuoro o6ecneveuun. B rnaae 17 o6cyxpae1cn pnp
c1anpap1nmx 1eH koHnLk1epno nnxenepnn a kon1ekc1e o6Lek1no-
opnen1npoaannoro nporpaHHnpoaannn. Inaaa 18 snakoHn1 c neckonLknHn
o1nocn1enLno noamHn konuenunnHn cpeo paspao1kn npnnoxenn n
manonaMn paspao1kn. O6a nopxopa ocnoaanm na ncnonLsoaannn na6opoa
knaccoa. Hakoneu, a rnaae 19 npnaopn1cn konkpe1nm npnHep cpepm paspa6o1kn.
VII. Hpopauuy1oe usyveuue.Konuenunn knaccoa npn annHa1enLnoH paccHo1pennn ne
c1onL npoc1a, kak nac nm1anncL y6epn1L a rnaae 3. B rnaae 20 paccHo1penm 6onee
rny6okne acnek1m o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn. TaH xe
o6cyxpak1cn penernpoaanne (nannkueecn npnHepoH o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn 6es knaccoa) n nonn1ne He1aknacca (na ypoane co6c1aenno
nsmka nporpaHHnpoaannn). B rnaae 21 a o6unx uep1ax onncanm pasnoo6pasnme
1exnnkn peannsaunn, npnHennkunecn npn cospannn o6Lek1no-opnen1npoaannmx
nsmkoa.
B pecn1nnepenLnoH kypce, ko1opm n un1ak a ynnaepcn1e1e u1a1a Operon,
npn6nnsn1enLno opny nepenk n nocanuak kaxpoHy ns ocnoanmx nanpaanenn,
onncannmx amue. B 1o xe caHoe apeHn c1ypen1m pa6o1ak1 nap ne cnnukoH
6onLunH npoek1oH. Konkpe1nm o6Lek1no-opnen1npoaannm nsmk paspa6o1kn onn
am6npak1 caHn. CeHec1p sakanunaae1cn npepc1aanenneH pnsana npoek1a n ero
peannsaune.
Hepaoe nspanne knnrn n sakonunn rnaao [ononnn1enLnan nnqopHaunn. K
coxanennk, o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne pasanaae1cn 1ak
6mc1po, u1o nk6an pononnn1enLnan nnqopHaunn nou1n cpasy yc1apeaae1. Hoa1oHy
n ne aknkunn ao a1opoe nspanne rnaay c 1aknH nasaanneH. BHec1o a1oro n
nonm1akcL noppepxnaa1L c1pannuky Web c nocnepnnHn caepennnHn.
converted to PDF by BoJIoc
Kak nonyun1u ncxoqnme 1ekc1m
Hcxopnme 1ekc1m o6yuakunx npnHepoa, npepc1aanennmx a knnre, Hoxno nonyun1L
anonnHno, o6pa1nauncL uepes ftp no appecy ftp.cs.orst.edu, ka1anor /pub/budd/oopintro. B
1oH xe ka1anore Hoxno 6ype1 na1n pononnn1enLnyk nnqopHaunk, nanpnHep cnncok
oun6ok, o6napyxennmx a knnre, ynpaxnennn, konnn npospauek, ko1opme n ncnonLsyk a
caoeH kypce. Bce a1o Hoxno 1akxe yanpe1L uepes World Wide Web na Honx nnunmx
poHaunnx c1pannuax no appecy http://www.cs.orst.edu/~budd/oopintro. Bonpocm am
Hoxe1e nocmna1L anek1ponno nou1o no appecy budd@cs.orst.edu nnn o6muno nou1o:
Professor Timothy A. Budd, Department of Computer Science, Oregon State University,
Corvallis, Oregon, 97331.
H1o 1pe6ye1cn sna1u qnn u1ennn knnrn
H npepnonarak, u1o un1a1enL snakoH xo1n 6m c opnnH 1papnunonnmH nsmkoH
nporpaHHnpoaannn, nanpnHep Pascal nnn C. Mon kypcm 6mnn anonne ycneuno aocnpnnn1m
c1ypen1aHn nocnepnero ropa undegraduate level n nepaoro graduate level. B neko1opmx
cnyuanx (oco6enno a nocnepne ue1aep1n knnrn) 6onee rny6okne snannn okaxy1cn
nonesnm, no onn ne nannk1cn o6nsa1enLnmHn. HanpnHep, c1ypen1, ko1opm
cneunannsnpye1cn na paspa6o1ke nporpaHHnoro o6ecneuennn, nerue aocnpnHe1 Ha1epnan
rnaam 17, a o6yuakuncn noc1poennk koHnnnn1opoa cou1e1 rnaay 21 anonne nonn1no.
TeHa1nky o6enx rnaa Hoxno ynpoc1n1L npn neo6xopnHoc1n.
Hpeqncnoane k nepaoHy nsqannm
Korpa-1o n nauan aec1n kypc nekun no nsmky Smalltalk n ckopo o6napyxnn, u1o
yue6no nn1epa1ypm no panno 1eHe ne cyuec1aye1. HpnunocL nannca1L knnry no Smalltalk
[Budd 1987], na ocnoae ko1opo n neckonLko ne1 aen ceHnnap no nsmky Smalltalk n
o6Lek1no-opnen1npoaannoHy nporpaHHnpoaannk. HecoHnenno, am yxe porapanncL, u1o
knnry, ko1opyk am pepxn1e a pykax, nopopnna 1a xe no1pe6noc1L. Hauaa npenopaaanne a
konue 80-x, n nonyuan ace aospac1akuee uncno sanpocoa na kypc, noc1poennm na ocnoae
C++. B 1o xe caHoe apeHn nonynnpnoc1L koHnLk1epoa Macintosh cpenana nsaec1nmH nsmk
Object Pascal. Hakoneu, nonanenne NeXT amsaano nn1epec k o6yuennk Objective-C.
HockonLky n ne co6npancn paaa1L ue1mpe pasnnunmx kypca, n peunn aec1n opnn kypc, a
ko1opoH nsnaran npnnunnm o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn, nnnkc1pnpyn
nx npnHepaHn na acex ue1mpex nsmkax. Cnyua1enn ysnann koe-u1o o kaxpoH nsmke n
cHornn amnonnn1L npoek1 na 1oH nsmke, ko1opm nH nonpaanncn.
Bckope n o1npaanncn na nonckn yue6nnka no kypcy 1akoro 1nna. K HoeHy ypnanennk,
poaonLno 6mc1po o6napyxnnocL, u1o ace nHekunecn knnrn, coaepuenno aocxn1n1enLnme
ao Hnornx o1nouennnx, opnen1npoaanm na opnn o1penLno asn1m nsmk. H nsyunn
cnepykune 1pypm: Kokc [Cox 1986], Ionp6epr n Po6nncon [Goldberg 1983], Kaxnep n
Ha11epcon [Kaechler 1986], Knn [Keen 1989], Meep [Meyer 1988a], Hnncon n Bnnep
[Pinson 1988], a 1akxe, pasyHee1cn, Bnnep n Hnncon [Wiener 1988], C1payc1pyn
[Stroustrup 1986], HonL [Pohl 1989]. Xo1n a pesynL1a1e n o1o6pan neko1opme ns nnx kak
acnoHora1enLnme, ace a1n knnrn 6mnn o1aeprny1m a kauec1ae ocnoanoro yue6nnka no 1o
npoc1o npnunne, u1o ace onn caopn1cn k y1aepxpennk: o6Lek1no-opnen1npoaannoe
nporpaHHnpoaanne ec1L o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne na nsmke X, rpe X
a1o nk6nHm nsmk nporpaHHnpoaannn 1oro nnn nnoro aa1opa. H1ak, Hne npnunocL
nnca1L caon co6c1aennme nekunn. Ha cnepykun rop n nepecHo1pen n pononnnn caon
sanncn. B pesynL1a1e popnnacL a1a knnra.
Heko1opme cnyua1enn Hoero kypca (ko1opm okasancn naHnoro nonynnpnee n,
cnepoaa1enLno, snaun1enLno Hnorouncnennee, ueH n npepnonaran) pononnn1enLno k
amnonnennk npoek1oa na opnoH ns ue1mpex nsmkoa, ynoHnny1mx amue, ycneuno
saaepunnn pa6o1m na nsmkax Actor [Actor 1987], Turbo Pascal [Turbo 1988] n CLOS
[Keen 1989]. Tak kak Hon sapaua coc1onna a 1oH, u1o6m nepepa1L npnnunnm o6Lek1no-
converted to PDF by BoJIoc
opnen1npoaannoro nporpaHHnpoaannn ane saancnHoc1n o1 konkpe1noro nsmka, n cnpocnn
a1nx ynnkyHoa o 1oH, npnropnnncL nn nH a nanncannn nporpaHH Hon nekunn. Ha ocnoaannn
nx nonoxn1enLnmx o1ae1oa n y6epnncn, u1o no kpane Hepe uac1nuno poc1nr
onpepenennoro ypoann nsmkoao nesaancnHoc1n Ha1epnana.
D1y knnry nenLsn cun1a1L nn yue6nnkoH nsmka nporpaHHnpoaannn, nn cnpaaounnkoH no
nk6oHy ns paccHa1pnaaeHmx ue1mpex nsmkoa. B kaxpoH ns nnx ec1L Hnorouncnennme
nkancm, cneunqnunme pnn nsmka nporpaHHnpoaannn a uenoH nnn ero konkpe1no
peannsaunn, ko1opme n ne cun1ak aosHoxnmH o6cyxpa1L a panno knnre, no ko1opme,
necoHnenno, nHek1 aaxnoe npak1nueckoe snauenne pnn nporpaHHnc1a.
Bnaroqapnoc1n
H, 6esycnoano, 6naropapen aceH 65 c1ypen1aH Hoe rpynnm CS589 ynnaepcn1e1a u1a1a
Operon, ko1opme a 1euenne 1989 ropa am1epnenn na ce6e npouecc c1anoanennn uepnoaoro
aapnan1a a1o knnrn. Onn nonyuann na pykn no opno rnaae, sauac1yk acero sa penL nnn
sa paa po 1oro, kak nH npepc1onno ycnmua1L coo1ae1c1aykun Ha1epnan na nekunn. Hx
1epnennk o1pae1cn ponxnoe. Konkpe1nme npnHeuannn, ncnpaanennn, saHeuannn n kpn1nka
Honx yaaxaeHmx c1ypen1oa 6mnn upesamuano nonesnm. B uac1noc1n, n xouy
no6naropapn1L sa noppo6nme koHHen1apnn ToHaca AHoqa (Thomas Amoth), KnHa
[ponresena (Kim Drongesen), Opanka Ipncaonpa (Frank Griswold), Papxnaa Hanpen (Rajeev
Pandey) n Onna Papepa (Phil Ruder).
HacLnnc ns rnaam 8 6mn apoxnoanen npoek1oH, amnonnennmH KnHoH [ponresenoH, a
nrpa BnnLnpp (rnaaa 6) ocnoaana na npoek1e Iyn1apa MaHLe (Guenter Mamier) n [n1pnxa
Be11uepeka (Dietrich Wettschereck). Opnako a o6onx cnyuanx co6c1aenno 1ekc1 nporpaHHm
6mn Hno nonnoc1Lk nepepa6o1an. Oak1nueckn Hon aapnan1m nporpaHH c uenLk nyuuero
nsnoxennn 6mnn snaun1enLno cokpauenm n nnkonH o6pasoH ne conoc1aanHm c naHnoro
npeaocxopnunHn nx npoek1aHn, amnonnennmHn a1nHn c1ypen1aHn.
H 1akxe npnsna1enen 1eH nkpnH, ko1opme anecnn koHHen1apnn, ncnpaanennn,
saHeuannn. K nnH o1nocn1cn: Makn Apap (Michael Adar), [xepn Anppeac (Jerrie Andreas),
nnn Kokpan (Lynn Cochran), Bpap Kokc (Brad Cox), IpaxaH [aHnne1on (Graham
Dumpleton), Hn1ep Iporono (Peter Grogono), Hona Xar (Nola Hague), Mapcna Xop1on (Marcia
Horton), PanLq [xoncon (Ralph Johnson), [yr n (Doug Lea), Tap Lknc (Ted Lewis),
C1annn nnHan (Stanley Lippman), [apcn MakKannyH (Darcy McCallum), nnpce Mapuan
(Lindsey Marshall), Makky Cakknnen (Makku Sakkinen), Makn Lape (Michael Share), [aa
Tensep (Dave Taenzer), Ha6nnL 3aHenL (Nabil Zamel). Xouy no6naropapn1L peuensen1oa Epa
Iepnnrepa (Ed Gehringer), [xeHca Xenno1nca (James Heliotis), Kapna n6epreppa (Karl
Lieberherr), [xeqqa Hapkepa (Jeff Parker), [xac1nHa CHn1a (Justim Smith) n [annenn
L1epHca (Daniel Sterms).
nc1nnrn nporpaHH 6mnn naneua1anm c noHouLk Hakpocoa LaTeX, ocnoaannmx na
Hakpocax qopHa1npoaannn nporpaHH C, nanncannmx DaHonnoH MakManycoH (Eamonn
McManus) ns konnepxa Can1o Tponum, [y6nnn.
[nn nk6oro aa1opa acerpa nonesno ysna1L 1ouky spennn ppyrnx nkpe na ero knnry.
Hoa1oHy n 6naropapk Apnny Bpnnu (Arina Brintz), ynca nnena (Louise Leenen), ToHHn
Meepa (Tommie Meyer), Eneny Posen6na11 (Helene Rosenblatt) n AnenL BnnLoen (Anel
Viljoen) c qakynL1e1a koHnLk1epnmx nayk n nnqopHaunonnmx cnc1eH kxnoaqpnkanckoro
ynnaepcn1e1a a Hpe1opnn.
OrpoHnoe konnuec1ao nkpe okasano noHouL a yc1panennn oun6ok n nepoue1oa
nepaoro nspannn n anecennn ynyuuenn. H npnsna1enen aceH n coxanek, u1o ne Hory
nepeuncnn1L specL acex. Peuensen1aHn a1oporo nspannn 6mnn ToHac Bonnnk (Thomas
Bonnick), Ceaepo-Boc1ounm ynnaepcn1e1, M. A. Lpnpxap (M. A. Shridhar), ynnaepcn1e1
u1a1a Kxnan Kaponnna, n Yon1ep C. [oxepn1n (Walter C. Daugherity), A&M ynnaepcn1e1,
Texac.
converted to PDF by BoJIoc
nnn [opan Ko1e (Lynne Doran Cote) n [e6opa aqqep1n (Debora Laffertey) ns Addison
Wesley 6mnn koHne1en1nmHn n 1epnennamHn pepak1opaHn a1oporo nspannn. HoHouL a
nspannn knnrn 6mna okasana Ann Ha1 (Ann Knight) ns Superscript.
Fnaaa 1

O61ek1uo- opueu1upoaauuoe Hmmneuue
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne (OOH) c1ano upesamuano nonynnpno a
nocnepnne neckonLko ne1. Hponsaopn1enn nporpaHHnoro o6ecneuennn 6pocak1cn cospaaa1L
o6Lek1no-opnen1npoaannme aepcnn caonx npopyk1oa. HonannocL necue1noe konnuec1ao
knnr n cneunanLnmx amnyckoa akapeHnuecknx (n ne 1onLko) xypnanoa, nocanuennmx a1oHy
npepHe1y. C1ypen1m c1peHn1cn k sanncn koHne1en1en a o6Lek1no-opnen1npoaannoH
nporpaHHnpoaannn a caonx xapak1epnc1nkax. 1o6m ouenn1L a1y 6esyHnyk ak1nanoc1L,
o1He1nH, u1o o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne npnae1c1aye1cn c 6onLunH
an1ysnasHoH, ueH 1o1, ko1opm Hm anpenn panee npn npoaosrnauennn 1aknx
peaonkunonnmx npe, kak c1pyk1ypnoe nporpaHHnpoaanne nnn akcnep1nme cnc1eHm.
Mon uenL a nepao rnaae coc1on1 a 1oH, u1o6m nccnepoaa1L n o6Lncnn1L ocnoanme
npnnunnm o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn, a 1akxe nponnnkc1pnpoaa1L
cnepykune y1aepxpennn.
OOH a1o peaonkunonnan npen, coaepuenno nenoxoxan na u1o-nn6o ampanraaueecn a
nporpaHHnpoaannn.
OOH a1o aaonkunonnm uar, ec1ec1aennmH o6pasoH am1ekakun ns npepuec1aykue
nc1opnn.
1.1. HoveHy OOH 1ak nonynnpuo?
H nepeuncnk neko1opme (na Ho asrnnp caHme rnaanme) npnunnm orpoHno
nonynnpnoc1n o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn a nocnepnee pecn1nne1ne:
napexpa, u1o OOH Hoxe1 npoc1o n 6mc1po npnaec1n k poc1y npopyk1nanoc1n n
ynyuuennk napexnoc1n nporpaHH, noHoran 1eH caHmH paspeun1L kpnsnc a
nporpaHHnoH o6ecneuennn;
xenanne nepe1n o1 cyuec1aykunx nsmkoa nporpaHHnpoaannn k noao
1exnonornn;
apoxnoannkuee cxopc1ao c npenHn, popnaunHncn a ppyrnx o6nac1nx.
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne nanne1cn nnuL nocnepnnH saenoH a
pnnnno uenn peuenn, ko1opme 6mnn npepnoxenm pnn paspeuennn kpnsnca
nporpaHHnoro o6ecneuennn. Honoxa pyky na ceppue: kpnsnc nporpaHHnoro o6ecneuennn
npoc1o osnauae1, u1o naue aoo6paxenne n 1e sapaun, ko1opme Hm xo1nH peun1L c
noHouLk koHnLk1epoa, nou1n acerpa onepexak1 naun aosHoxnoc1n.
HecHo1pn na 1o u1o o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne pec1an1enLno
noHorae1 npn cospannn cnoxnmx nporpaHHnmx cnc1eH, aaxno noHnn1L, u1o OOH ne
nanne1cn cepe6pnno nyne (1epHnn, c1aaun nonynnpnmH 6naropapn Opepy Bpykcy
[Brooks 1987]), ko1opan sanpoc1o cnpaanne1cn c uypoanueH. HporpaHHnpoaanne no-
npexneHy nanne1cn opno ns nan6onee 1pypnmx sapau, asaannaaeHmx na ce6n uenoaekoH.
1o6m c1a1L npoqeccnonanoH a nporpaHHnpoaannn, neo6xopnHm 1anan1, cnoco6noc1L k
1aopuec1ay, nn1ennek1, snannn, nornka, yHenne c1pon1L n ncnonLsoaa1L a6c1pakunn n,
caHoe rnaanoe, onm1 paxe a 1oH cnyuae, korpa ncnonLsyk1cn nyuune cpepc1aa
paspa6o1kn.
converted to PDF by BoJIoc
H nopospeaak, u1o ec1L n ppyran npnunna oco6o nonynnpnoc1n 1aknx nsmkoa
nporpaHHnpoaannn, kak C++ n Object Pascal (no kon1pac1y co Smalltalk n Beta). Ona
coc1on1 a 1oH, u1o n apHnnnc1paunn n paspa6o1unkn napek1cn, u1o nporpaHHnc1 na nsmkax
C nnn Pascal Hoxe1 nepe1n na C++ nnn Object Pascal c 1o xe nerkoc1Lk, c ko1opo
nponcxopn1 po6aanenne neckonLknx 6yka na 1n1ynLnm nnc1 cep1nqnka1a o
cneunanLnoc1n. K coxanennk, 1ak nponcxopn1 ne acerpa. O6Lek1no-opnen1npoaannoe
nporpaHHnpoaanne nanne1cn noamH nonnHanneH 1oro, u1o co6c1aenno nasmaae1cn
amuncnennnHn, a 1akxe 1oro, kak Hm HoxeH c1pyk1ypnpoaa1L nnqopHaunk any1pn
koHnLk1epa. 1o6m c1a1L npoqeccnonanoH a 1exnnke OOH, 1pe6ye1cn nonnan nepeouenka
npnamunmx He1opoa paspa6o1kn nporpaHH.
1.2. Rsmk u Hmmneuue
enoaeueckne cyuec1aa ne o6uak1cn nenocpepc1aenno c o6Lek1nanmH HnpoH n c
o6uec1aoH a 1oH cHmcne, kak a1o o6muno nonnHae1cn. Onn a snaun1enLno Hepe saancn1 o1
1oro konkpe1noro nsmka, ko1opm c1an nx cpepo o6uennn. D1o coaepuennan nnnksnn
nonara1L, u1o k1o-1o Hoxe1 cornacoaa1L ce6n c cyunoc1Lk peanLnoc1n 6es ncnonLsoaannn
nsmka n u1o nsmk acero nnuL cnyuanoe cpepc1ao peuennn konkpe1nmx sapau o6uennn
nnn Hmunennn. Cy1L aonpoca a 1oH, u1o peanLnm Hnp a snaun1enLno c1enenn
neocosnanno c1pon1cn na nsmkoamx npnamukax rpynnm nkpe... Mm anpnH, cnmunH n
ncnm1maaeH oc1anLnme ouyuennn 1ak, kak Hm a1o penaeH, a snaun1enLno c1enenn
no1oHy, u1o nsmkoame o6muan nauero o6uec1aa npeppacnonarak1 k onpepenennoHy
am6opy cnoco6a nn1epnpe1aunn.
Dpaapp Cannp (un1npoaano no [Whorf 1956]).
Un1a1a nopuepknaae1 1o1 qak1, u1o nsmk, na ko1opoH Hm roaopnH, nenocpepc1aenno annne1
na cnoco6 aocnpnn1nn Hnpa. D1o cnpaaepnnao ne 1onLko pnn ec1ec1aennmx nsmkoa,
nopo6nmx 1eH, u1o nsyuanncL a nauane paapua1oro aeka aHepnkancknHn nnnranc1aHn
DpaappoH CannpoH n n BopqoH, no 1akxe n pnn nckycc1aennmx nsmkoa, nanopo6ne 1ex,
u1o Hm ncnonLsyeH a nporpaHHnpoaannn.
1.2.1. 3ckuHocm u cuer
HpnHepoH, nou1n noaceHec1no un1npyeHmH (xo1n sauac1yk oun6ouno cH. [Pillum 1991])
a kauec1ae nnnkc1paunn 1oro, kak nsmk annne1 na Hmunenne, nanne1cn 1o1 qak1, u1o a
acknHoccknx (nnn kn1cknx) nsmkax nHee1cn Hnoxec1ao cnoa pnn onncannn pasnnunmx
1nnoa cnexnoro nokpoaa Hokporo, nno1noro, nopHepsuero n 1. p. D1o-1o kak pas ne
nanne1cn ypnan1enLnmH. k6oe coo6uec1ao c o6unHn nn1epecaHn ec1ec1aennmH o6pasoH
paspa6a1maae1 cneunannsnpoaannm cnoaapL neo6xopnHmx nonn1n.
1o pec1an1enLno aaxno ne cnnukoH a6conk1nsnpoaa1L amaop, ko1opm Hm HoxeH
cpena1L ns a1oro npoc1oro na6nkpennn. Inaanoe ne a 1oH, u1o rnas acknHocoa a kakoH-1o
cyuec1aennoH acnek1e o1nnuae1cn o1 Hoero co6c1aennoro nnn u1o acknHocm Hory1 anpe1L
aeun, ko1opme n ne cnoco6en pasnnua1L. C 1euenneH apeHenn, c noHouLk 1pennpoakn, n
6m c1an nnuy1L ne xyxe pasnnua1L pasnoo6pasnme 1nnm cnexnoro nokpoaa. Opnako nsmk,
na ko1opoH n roaopk (a nHenno anrnnckn), ne amnyxpae1 Henn sannHa1Lcn a1nH, n 1eH
caHmH ykasannme cnoco6noc1n ne nannk1cn pnn Henn ec1ec1aennmHn.
TaknH o6pasoH, pasnnunme nsmkn (nanpnHep, aaenknckn) Hory1 npnaec1n (no ne
o6nsa1enLno 1pe6yk1 a1oro) k 1oHy, u1o6m cHo1pe1L na Hnp c pasnmx c1opon.
1o6m aqqek1nano ncnonLsoaa1L OOH, 1pe6ye1cn rnnpe1L na Hnp nnmH cnoco6oH. CaHo no
ce6e npnHenenne o6Lek1no-opnen1npoaannoro nsmka nporpaHHnpoaannn (1akoro, kak C++)
ne amnyxpae1 c1a1L o6Lek1no-opnen1npoaannmH nporpaHHnc1oH. HcnonLsoaanne o6Lek1no-
opnen1npoaannoro nsmka ynpouae1 paspa6o1ky o6Lek1no-opnen1npoaannmx npnnoxenn,
no, kak 6mno oc1poyHno saHeueno, nporpaHHa qop1panoackoro 1nna Hoxe1 6m1L nanncana
na nk6oH nsmke.
converted to PDF by BoJIoc
1.2.2.HpuHep us o6nac1u nporpaHHupoaauun
CansL Hexpy nsmkoH n HmunenneH pnn ec1ec1aennmx nsmkoa, o ko1opo Hm roaopnnn,
nanne1cn eue 6onee saHe1no pnn nckycc1aennmx koHnLk1epnmx nsmkoa. Hsmk
nporpaHHnpoaannn, a 1epHnnax ko1oporo paspa6o1unk pyHae1 o npo6neHe, anocn1 oco6me
o11enkn n, aoo6ue roaopn, nsHenne1 paxe caH anropn1H.
HpnaepeH npnHep, nnnkc1pnpykun cansL Hexpy koHnLk1epnmH nsmkoH n cnoco6oH
peuennn sapaun. Heko1opoe apeHn nasap opnn c1ypen1, pa6o1akun a o6nac1n
rene1nuecknx nccnepoaann, c1onknyncn c neo6xopnHoc1Lk anannsa nocnepoaa1enLnoc1e
[HK. Hpo6neHa Horna 6m1L caepena k o1nocn1enLno npoc1o sapaue. Monekyna [HK
npepc1aanne1cn a anpe aek1opa ns N uenouncnennmx snauenn, rpe N ouenL aennko
(nopnpka pecn1koa 1mcnu). Hyxno 6mno npoaepn1L, ne nanne1cn nn kako-nn6o yuac1ok
pnnnm M (M qnkcnpoaannan konc1an1a nopnpka 510) noa1opnkunHcn a
nocnepoaa1enLnoc1n [HK.
ACTCGGATCTTGCATTTCGGCAATTGGACCCTGACTTGGCCA...
HporpaHHnc1, ne ponro pyHan, nanncan npoc1yk n npnHonnnenyk nporpaHHy na Fortran
neu1o apope
DO 10 I = 1, N-M
DO 10 J = 1, N-M
FOUND=.TRUE.
DO 20 K = 1, M
20 IF (X(I+K-1).NE.X(J+K-1)) FOUND=.FALSE.
IF (FOUND) ...
10 CONTINUE
On 6mn nenpnn1no pasouapoaan, korpa npo6nme sanyckn nporpaHHm nokasann, u1o ona
no1pe6ye1 Hnornx uacoa pnn saaepuennn pa6o1m. C1ypen1 o6cypnn a1y npo6neHy co
c1ypen1ko, ko1opan okasanacL npoqeccnonanoH a nporpaHHnpoaannn na nsmke APL. Ona
ckasana, u1o Horna 6m nonpo6oaa1L nannca1L nporpaHHy pnn peuennn a1o sapaun.
C1ypen1 6mn a coHnennn: Fortran nsaec1en kak opnn ns nan6onee aqqek1nanmx
koHnnnnpyeHmx nsmkoa, a APL peannsoamaancn c noHouLk nn1epnpe1a1opa. TaknH
o6pasoH, 1o1 qak1, u1o APL-nporpaHHnc1 cnoco6en coc1aan1L anropn1H, ko1opm 1pe6ye1
pnn pa6o1m Hnny1m, a ne uacm, 6mn aocnpnnn1 c onpepenenno poso nepoaepnn.
APL-nporpaHHnc1ka nepeqopHynnpoaana sapauy. BHec1o 1oro u1o6m pa6o1a1L c aek1opoH ns
N aneHen1oa, ona npepc1aanna pannme a anpe Ha1pnum, nHekue npn6nnsn1enLno N c1pok
n M c1on6uoa:
A C T C G G noswuww 1 M
C T C G G A noswuww 2 M+1
T C G G A T noswuww 3 M+2
C G G A T T noswuww 4 M+3
G G A T T C noswuww 5 M+4
G A T T C T noswuww 6 M+5
. . .
T G G A C C
G G A C C C
3a1eH c1ypen1ka o1cop1npoaana Ha1pnuy no c1pokaH. Ecnn kako-1o qparHen1 okasmaae1cn
noa1opnkunHcn, 1o a o1cop1npoaanno Ha1pnue pae cocepnne c1pokn ponxnm okasa1Lcn
npen1nunmHn.
. . .
T G G A C C
T G G A C C
. . .
converted to PDF by BoJIoc
Hpoaepka a1oro ycnoann okasmaae1cn 1pnananLno sapaue. Hpnunna, no ko1opo APL-
nporpaHHa okasanacL 6mc1pee, ne nHena nnuero o6uero co ckopoc1Lk pa6o1m APL no
cpaanennk c Fortran. InaanmH 6mno 1o, u1o nporpaHHa na Fortran ncnonLsoaana anropn1H
co cnoxnoc1Lk O(M r N 2), a 1o apeHn kak anropn1H cop1npoakn APL-nporpaHHm 1pe6oaan
npnHepno O(M r N log N) onepaun.
Knkueao HoHen1 a1o nc1opnn ne a 1oH, u1o APL nanne1cn nyuunH nsmkoH
nporpaHHnpoaannn, ueH Fortran, no a 1oH, u1o APL-nporpaHHnc1 ec1ec1aennmH o6pasoH
npnuen k 6onee ypaunoHy peuennk. B uac1noc1n, ns-sa 1oro, u1o na nsmke APL ouenL
neypo6no oprannsoamaa1L unknm, a cop1npoaka nanne1cn 1pnananLno onepaune e
coo1ae1c1aye1 ac1poennm onepa1op nsmka. TaknH o6pasoH, pas yx cop1npoaky Hoxno
c1onL nerko ncnonLsoaa1L, xopoun APL-nporpaHHnc1 acerpa c1apae1cn na1n pnn nee
noaoe npnHenenne. B a1oH cHmcne nsmk nporpaHHnpoaannn, na ko1opoH sanncmaae1cn
peuenne sapaun, nanpnHyk annne1 na xop Hmcne nporpaHHnc1a, sac1aannn ero
paccHa1pnaa1L sapauy nop onpepenennmH yrnoH.
1.2.3. Hpuuun Hpva u runo1esa Bopqa
erko noaepn1L a y1aepxpenne, u1o nsmk, na ko1opoH amckasmaae1cn npen, nanpaanne1
Hmunenne. Opnako ec1L 6onee cnnLnoe y1aepxpenne, nsaec1noe cpepn nnnranc1oa kak
rnno1esa CannpaBopqa. Ona npe1 eue panLue, xo1n n nanne1cn cnopno [Pullum 1991].
Inno1esa CannpaBopqa y1aepxpae1, u1o nnpnanpyyH, ncnonLsykun neko1opm nsmk, a
coc1onnnn aoo6pasn1L nnn npnpyHa1L neu1o, ne Horyuee 6m1L nepeaepennmH nnn paxe
nonn1mH nnpnanpyyHaHn ns ppyro nsmkoao cpepm. Takoe nponcxopn1, ecnn a nsmke
a1oporo nnpnanpyyHa ne1 akanaanen1nmx cnoa n o1cy1c1ayk1 konuenunn nnn ka1eropnn pnn
npe, aoaneuennmx a paccHa1pnaaeHyk HmcnL. Hn1epecno cpaann1L pannyk npek c npnHo
npo1naononoxno konuenune a nnqopHa1nke a nHenno npnnunnoH pua.
B 30-x ropax y Ha1eHa1nkoa npo6ypnncn 6onLuo nn1epec k pasnnunmH qopHannsHaH,
ko1opme Hory1 6m1L ncnonLsoaanm npn amuncnennnx. D1n npen nonyunnn pasan1ne a 40
50-x ropax, korpa onn npnaneknn annHanne Honoporo coo6uec1aa cneunannc1oa no
nnqopHa1nke. HpnHepaHn 1aknx cnc1eH nannk1cn Hopenn, npepnoxennme pueH
[Church 1936], Hoc1oH [Post 1936], MapkoamH [Markov 1951], TLkpnnroH [Turing 1936],
Knnnn [Kleene 1936] n ppyrnHn. B opno apeHn npnaopnnocL Hnoxec1ao apryHen1oa,
pokasmaakunx, u1o kaxpan ns a1nx cnc1eH Hoxe1 6m1L ncnonLsoaana pnn Hopennpoaannn
oc1anLnmx. ac1o 1akne poaopm 6mnn payxc1oponnnHn, nokasmaan, u1o o6e Hopenn
akanaanen1nm c neko o6ue 1oukn spennn. Bce a1o npnaeno nornky Anonso pua k
rnno1ese, ko1opan 1enepL cansana c ero nHeneH.
Hpuuun Hpva: k6oe amuncnenne, pnn ko1oporo cyuec1aye1 aqqek1nanan
npouepypa, Hoxe1 6m1L peannsoaano na Haunne TLkpnnra.
Ho caHo caoe npnpope a1o y1aepxpenne nepokasyeHo, nockonLky Hm ne nHeeH c1pororo
onpepenennn 1epHnna aqqek1nanan npouepypa. TeH ne Henee po cnx nop ne 6mno
napeno kon1pnpnHepa, n y6epn1enLnoc1L oueanpnoc1n, no-anpnHoHy, 6naronpnn1c1aye1
npnnn1nk a1oro y1aepxpennn
1
.
Hpnsnanne npnnunna pua nHee1 aaxnoe n rny6okoe cnepc1ane pnn nsmkoa
nporpaHHnpoaannn. Maunnm TLkpnnra nannk1cn nsyHn1enLno npoc1mHn HexannsHaHn. O1
nsmka nporpaHHnpoaannn 1pe6ye1cn neHnoro, u1o6m cHopennpoaa1L 1akoe yc1poc1ao.
B 1960-x ropax, k npnHepy, 6mno nokasano, u1o Haunna TLkpnnra Hoxe1 6m1L
cHopennpoaana na nk6oH nsmke nporpaHHnpoaannn, a ko1opoH copepxa1cn ycnoanme
onepa1opm n onepa1opm unkna [Bohm 1966]. D1o1 ne coaceH npaannLno nonnHaeHm
pesynL1a1 6mn opnnH ns ocnoanmx poaopoa a saun1y y1aepxpennn o 1oH, u1o snaHenn1m
onepa1op goto nanne1cn nenyxnmH.
Ecnn Hm npnsnaeH npnnunn pua, 1o nk6o nsmk, na ko1opoH Hoxno cHopennpoaa1L
Haunny TLkpnnra, nanne1cn poc1a1ouno HounmH, u1o6m ocyuec1an1L nk6o peannsyeHm
converted to PDF by BoJIoc
anropn1H. [nn peuennn npo6neHm napo noc1pon1L Haunny TLkpnnra, ko1opan ampac1
xenaeHm pesynL1a1, cornacno npnnunny pua 1akan Haunna ponxna cyuec1aoaa1L pnn
kaxporo anropn1Ha. 3a1eH oc1ae1cn 1onLko cHopennpoaa1L Haunny TLkpnnra na aaueH
nk6nHoH nsmke nporpaHHnpoaannn. TeH caHmH cnopm o6 o1nocn1enLno Hounoc1n
nsmkoa nporpaHHnpoaannn ecnn nop Hounoc1Lk Hm nonnHaeH cnoco6noc1L peua1L
sapaun, okasmaak1cn 6eccHmcnennmHn. Hospnee Anan Hepnnc aaen ypaunm 1epHnn
pnn nopo6nmx apryHen1oa, nasaaa nx 1Lkpnnroackan nponac1L, nockonLky ns nnx 1ak
cnoxno am6pa1Lcn, a 1o apeHn kak caHn onn c1onL qynpaHen1anLno 6ecnonesnm.

1
Cospanne Ha1eHa1nueckoro qopHannsHa amuncnnHoc1n 6mno cansano c
neo6xopnHoc1Lk onpepenn1L nonn1ne anropn1Ha. Hoka nccnepoaannn a a1o
o6nac1n unn ycneuno, kaxpan noaan qopHannsoaannan nocnepoaa1enLnoc1L
amuncnenn nonyuana nHn anropn1H npoc1o no onpepenennk. Korpa xe
Ha1eHa1nkn c1onknynncL c sapauaHn, pnn ko1opmx npnunocL pokasmaa1L
o1cy1c1ane anropn1Ha, no1pe6oaanocL qopHanLnoe onpepenenne. B nac1onun
HoHen1 npnnn1o cun1a1u, u1o anropn1HoH nanne1cn nocnepoaa1enLnoc1L pec1an,
ko1opan Hoxe1 6m1L caepena k nporpaHHe, amnonnneHo c noHouLk Haunnm
TLkpnnra. Hnn, a akanaanen1no qopHe: nocnepoaa1enLnoc1L pec1an, ko1opan
Hoxe1 6m1L caepena k nporpaHHe pnn Haunnm Hoc1a, nnn koneunoro aa1oHa1a
Mapkoaa, nnn xe k nocnepoaa1enLnoc1n pekypcnanmx qynkun Knnnn n pua,
nanne1cn anropn1HoH. [okasano, u1o ace a1n qopHanLnme cnc1eHm amuncnnHoc1n
nannk1cn akanaanen1nmHn. TeH caHmH npnnunn pua nanne1cn akcnoMo, ne
1pe6ykue pokasa1enLc1aa, ko1opan qopHannsye1 nonn1ne anropn1Ha
(aqqek1nano npouepypm) n a cnny c1a1yca akcnoHm onpoaeprakuero
kon1pnpnHepa nHe1L ne Hoxe1. HpnMeu. nepea.
3aHe1nH, u1o npnnunn pua nanne1cn a onpepenennoH cHmcne 1ouno
npo1naononoxnoc1Lk rnno1esm CannpaBopqa. Hpnnunn pua y1aepxpae1, u1o no caoe
cy1n ace nsmkn nporpaHHnpoaannn npen1nunm. k6an npen, ko1opan ampaxae1cn na opnoH
nsmke, Hoxe1 (cornacno 1eopnn) 6m1L peannsoaana na ppyroH. Inno1esa xe CannpaBopqa,
kak am noHnn1e, y1aepxpae1, u1o cyuec1ayk1 npen, ne cornacykunecn c a1nH npnnunnoH.
Mnorne nnnranc1m o1aeprak1 rnno1esy CannpaBopqa n aHec1o a1oro npnnnHak1
1Lkpnnroackn akanaanen1 pnn ec1ec1aennmx nsmkoa: nk6an npen a npnnunne Hoxe1
6m1L ampaxena na nk6oH nsmke. HanpnHep, necHo1pn na 1o u1o nsmk nkpe, xnayunx a
xapkoH knnHa1e, ne copepxn1 ro1oamx nonn1n pnn 1nnoa cnexnoro nokpoaa, a npnnunne
kxane 1oxe Hory1 c1a1L cneunannc1aHn a o6nac1n rnnunonornn. Ananornuno o6Lek1no-
opnen1npoaannan 1exnnka ne cna6xae1 (a 1eopnn) aac noao amuncnn1enLno Hounoc1Lk,
ko1opan nosaonnna 6m peun1L npo6neHm, nepoc1ynnme pnn ppyrnx cpepc1a. Ho o6Lek1no-
opnen1npoaannm nopxop penae1 sapauy npoue n npnaopn1 ee k 6onee ec1ec1aenno
qopHe. D1o nosaonne1 o6paua1Lcn c npo6neHo 1aknH o6pasoH, ko1opm 6naronpnn1c1aye1
ynpaanennk 6onLunHn nporpaHHnmHn cnc1eHaHn.
H1ak, kak pnn koHnLk1epnmx, 1ak n pnn ec1ec1aennmx nsmkoa cnpaaepnnao: nsmk
nanpaanne1 Hmcnn, no ne npepnncmaae1 nx.
1.3. Hoaan napapurHa
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne uac1o nasmaak1 noao napapnrHo
nporpaHHnpoaannn. [pyrne napapnrHm nporpaHHnpoaannn: pnpek1nanoe (nsmkn 1nna
Pascal nnn C), nornueckoe (nsmkn 1nna Prolog) n qynkunonanLnoe (nsmkn 1nna Lisp, FP nnn
Haskell) nporpaHHnpoaanne.
Hn1epecno nccnepoaa1L cnoao napapnrHa. Cnepykun qparHen1 asn1 ns 1onkoaoro
cnoaapn American Heritage Dictionary of the English Language:
par-a-digm (cyu.) 1. Cnncok acex aapnan1oa okonuann cnoaa, paccHa1pnaaeHm kak
nnnkc1pa1nanm npnHep 1oro, k kakoHy cnpnxennk nnn cknonennk ono o1nocn1cn. 2.
converted to PDF by BoJIoc
k6o npnHep nnn HopenL (o1 na1nnckoro paradigma n rpeueckoro paradeigma HopenL,
o1 paradeiknunai cpaannaa1L, amc1aann1L).
Ha nepam asrnnp, cknonenne n cnpnxenne cnoa (nanpnHep, na1nncknx) nHee1 Hano o6uero
c koHnLk1epnmHn nsmkaHn. 1o6m nonn1L cansL, Hm ponxnm saHe1n1L, u1o cnoao
napapnrHa npnuno a nporpaHHnpoaanne ns okasaaue 6onLuoe annnnne knnrn
C1pyk1ypa nayunmx peaonkun, nanncanno nc1opnkoH naykn ToHacoH KynoH
[Kuhn 1970]. Kyn ncnonLsoaan a1o1 1epHnn ao a1opoH snauennn, u1o6m onncmaa1L na6op
1eopn, c1anpap1oa n He1opoa, ko1opme coaHec1no npepc1aannk1 co6o cnoco6
oprannsaunn nayunoro snannn nnmHn cnoaaHn, cnoco6 anpennn Hnpa. Ocnoanoe
nonoxenne Kyna coc1on1 a 1oH, u1o peaonkunn a nayke nponcxopn1, korpa c1apan
napapnrHa nepecHa1pnaae1cn, o1aeprae1cn n saHenne1cn noao.
HHenno a a1oH cHmcne kak HopenL nnn npnHep, a 1akxe kak oprannsykun nopxop a1o
cnoao ncnonLsoaan Po6ep1 Onop, naypea1 npeHnn TLkpnnra 1979 ropa, a nekunn
HapapnrHm nporpaHHnpoaannn [Floyd 1979]. HapapnrHm a nporpaHHnpoaannn a1o
cnoco6 konuen1yannsaunn, ko1opm onpepenne1, kak npoaopn1L amuncnennn n kak pa6o1a,
amnonnneHan koHnLk1epoH, ponxna 6m1L c1pyk1ypnpoaana n oprannsoaana.
Xo1n ceppueanna o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn 1exnnka oprannsaunn
amuncnenn n pannmx nanne1cn noao, ee sapoxpenne Hoxno o1nec1n no kpane Hepe k
apeHenaH nnnen (17071778), ecnn ne Hna1ona. HapapokcanLno, no c1nnL peuennn sapau,
aonnouennm a o6Lek1no-opnen1npoaanno 1exnnke, nepepko ncnonLsye1cn a
noacepneano xnsnn. TeH caHmH noanukn a nnqopHa1nke uac1o cnoco6nm aocnpnnn1L
ocnoanme npen o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn cpaann1enLno nerko, a 1o
apeHn kak nkpn, 6onee ocaepoHnennme a nnqopHa1nke, sauac1yk c1anoan1cn a 1ynnk ns-sa
caonx npepc1aanenn. K npnHepy, Anan Ke o6napyxnn, u1o nerue o6yua1L nsmky Smalltalk
pe1e, ueH npoqeccnonanLnmx nporpaHHnc1oa [Kay 1977].
Hpn nonm1kax nonn1L, u1o xe a 1ounoc1n nHee1cn a anpy nop 1epHnnoH oek1no-
opnen1npoaannoe nporpaMMnpoaanne, nonesno nocHo1pe1L na OOH c pasnmx 1ouek spennn.
B neckonLknx cnepykunx paspenax kpa1ko ouepunaak1cn 1pn pasnmx acnek1a o6Lek1no-
opnen1npoaannoro nporpaHHnpoaannn. Kaxpm ns nnx no-caoeHy o6Lncnne1, ueH
saHeua1enLna a1a npen.
1.4. Cnoco6 aupeuun Hupa
1o6m nponnnkc1pnpoaa1L neko1opme ocnoanme npen o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn, paccHo1pnH cn1yaunk ns o6mpenno xnsnn, a sa1eH nopyHaeH, kak
Hoxno sac1aan1L koHnLk1ep nan6onee 6nnsko cHopennpoaa1L napennoe peuenne.
HpepnonoxnH, n xouy nocna1L uae1m caoe 6a6yuke (ko1opyk soay1 Dncn) a ee penL
poxpennn. Ona xnae1 a ropope, pacnonoxennoH sa Hnoro HnnL o1 Henn, 1ak u1o aapnan1,
korpa n caH cpmaak uae1m n knapy nx k ee nopory, ne nopnexn1 o6cyxpennk. TeH ne Henee
nocna1L e uae1m a1o poc1a1ouno npoc1an sapaua: n npy a 6nnxaun uae1ounm
Harasnn, xosnky ko1oporo (kakoe coanapenne) soay1 Ono (florist uae1ounnua), nasmaak
e 1nn n konnuec1ao uae1oa, ko1opme 6m n xo1en nocna1L Hoe 6a6yuke, n (sa npneHneHyk
ueny) n Hory 6m1L yaepen, u1o uae1m 6ypy1 poc1aanenm a cpok, no nyxnoHy appecy.
1.4.1 Areu1m, o6nsauuoc1u, coo6euun u He1opm
Pnckyn 6m1L o6annennmH a 1aa1onornn, ace-1akn xouy nopuepkny1L, u1o HexannsH, ko1opm
n ncnonLsoaan pnn peuennn a1o npo6neHm, coc1onn a noncke nopxopnuero aren1a (a
nHenno, Ono) n nepepaue e coo6uennn, copepxauero Ho sanpoc. Onsannoc1uk Ono
nanne1cn ypoane1aopenne Hoero sanpoca. HHee1cn neko1opm Me1o 1o ec1L anropn1H,
nnn nocnepoaa1enLnoc1L onepaun, ko1opm ncnonLsye1cn Ono pnn amnonnennn sanpoca.
Mne ne napo sna1L, kako konkpe1nm He1op ona ncnonLsye1 pnn amnonnennn Hoero
sanpoca, n a pec1an1enLnoc1n sauac1yk n n ne xouy a1o sna1L. Bce panLneuee o6muno
ckpm1o o1 Hoero asrnnpa.
converted to PDF by BoJIoc
Opnako ecnn 6m n nccnepoaan a1o1 aonpoc, n, aosHoxno, o6napyxnn 6m, u1o Ono noune1
caoe coo6uenne xosnnny uae1ounoro Harasnna a ropope, rpe xnae1 Hon 6a6yuka. Xosnnn
uae1ounoro Harasnna a caok ouepepL npnHe1 neo6xopnHme Hepm n nopro1oan1
pacnopnxenne (coo6uenne) pnn uenoaeka, o1ae1c1aennoro sa poc1aaky uae1oa, n 1. p. TeH
caHmH Ho sanpoc a koneunoH cue1e 6ype1 ypoane1aopen uepes nocnepoaa1enLnoc1L
sanpocoa, nepecmnaeHmx o1 opnoro aren1a k ppyroHy.
H1ak, nepamH npnnunnoH o6Lek1no-opnen1npoaannoro nopxopa k peuennk sapau nanne1cn
cnoco6 sapannn pec1an.
[ec1ane a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn nnnunnpye1cn nocpepc1aoH
nepepaun coo6uenn aren1y (o6Lek1y), o1ae1c1aennoHy sa pec1ane. Coo6uenne copepxn1
sanpoc na ocyuec1anenne pec1ann n conpoaoxpae1cn pononnn1enLno nnqopHaune
(apryHen1aHn), neo6xopnHo pnn ero amnonnennn. Honyua1enL (receiver) a1o aren1,
ko1opoHy nocmnae1cn coo6uenne. Ecnn on npnnnHae1 coo6uenne, 1o na nero
aa1oHa1nueckn aosnarae1cn o1ae1c1aennoc1L sa amnonnenne ykasannoro pec1ann. B
kauec1ae peakunn na coo6uenne nonyua1enL sanyc1n1 neko1opm He1op, u1o6m
ypoane1aopn1L npnnn1m sanpoc.
Mm saHe1nnn, u1o cyuec1aye1 aaxnm npnnunn Hacknpoakn nnqopHaunn a o1nouennn
nepecmnkn coo6uenn. A nHenno: knnen1y, nocmnakueHy sanpoc, ne 1pe6ye1cn sna1L o
qak1nuecknx cpepc1aax, c noHouLk ko1opmx ero sanpoc 6ype1 ypoane1aopen. Cyuec1aye1 n
ppyro npnnunn, 1akxe anonne uenoaeueckn, ko1opm Hm anpenn a nenano qopHe npn
nepecmnke coo6uenn. Ecnn nHee1cn pa6o1a, ko1opyk nyxno amnonnn1L, 1o nepaan HmcnL
knnen1a na1n koro-nn6o eue, koHy Hoxno 6mno 6m ee nopyun1L. Takan anonne
nopHanLnan peakunn nou1n nonnoc1Lk a1poqnpoaanacL y nporpaHHnc1a, nHekuero
6onLuo onm1 a 1papnunonnoH nporpaHHnpoaannn. EHy 1pypno npepc1aan1L, u1o on (nnn
ona) ne ponxen ace nonnoc1Lk nporpaHHnpoaa1L caH, a Hoxe1 o6pa1n1Lcn k ycnyraH
ppyrnx. Baxnan uac1L o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn paspa6o1ka
noa1opno ncnonLsyeHmx koHnonen1, n nepamH uaroH a a1oH nanpaanennn nanne1cn
xenanne nonpo6oaa1L a1o1 ny1L.
Ckpm1ne nnqopHaunn nanne1cn aaxnmH npnnunnoH n a 1papnunonnmx nsmkax
nporpaHHnpoaannn. Torpa a ueH nepecmnka coo6uenn o1nnuae1cn o1 o6munoro amsoaa
npouepypm? B o6onx cnyuanx nHee1cn nocnepoaa1enLnoc1L 1ouno onpepenennmx pec1an,
ko1opme 6ypy1 nnnunnpoaanm a o1ae1 na sanpoc. Opnako nHek1cn paa cyuec1aennmx
o1nnunn.
Hepaoe ns nnx coc1on1 a 1oH, u1o y coo6uennn nHee1cn anonne konkpe1nm nonyua1enL
aren1, ko1opoHy nocnano coo6uenne. Hpn amsoae npouepypm ne1 c1onL nano ampenennoro
nonyua1enn. (Xo1n, koneuno, Hm HoxeH npnnn1L cornauenne, cornacno ko1opoHy
nonyua1eneH coo6uennn nanne1cn nepam apryHen1 a amsoae npouepypm npnHepno 1ak n
peannsyk1cn nonyua1enn coo6uenn).
B1opoe o1nnune coc1on1 a 1oH, u1o nn1epnpe1aunn coo6uennn (a nHenno He1op,
amsmaaeHm nocne npneHa coo6uennn) saancn1 o1 nonyua1enn n nanne1cn pasnnuno pnn
pasnnunmx nonyua1ene. H Hory nepepa1L Hoe coo6uenne, k npnHepy, Hoe xene Be1, n
ona ero noHe1, n kak pesynL1a1 pec1ane 6ype1 amnonneno (a nHenno uae1m 6ypy1
poc1aanenm 6a6yuke). Opnako He1op, ko1opm ncnonLsye1 Be1 pnn amnonnennn sanpoca
(aecLHa aepon1no, npoc1o nepeappecoaaa ero xosnke uae1ounoro Harasnna Ono), 6ype1
nnmH, ueH 1o1, ko1opm npnHenn1 Ono a o1ae1 na 1o1 xe caHm sanpoc. Ecnn n nonpouy o
1oH xe Kena, Hoero sy6noro apaua, y nero Hoxe1 ne okasa1Lcn nopxopnuero He1opa pnn
peuennn noc1aanenno sapaun. Ecnn npepnonoxn1L, u1o Ken aoo6ue aocnpnHe1 a1o1
sanpoc, 1o on c 6onLuo aepon1noc1Lk ampac1 napnexauee pnarnoc1nueckoe coo6uenne o6
oun6ke.
BepneHcn a naueH o6cyxpennn na ypoaenL koHnLk1epoa n nporpaHH. Pasnnune Hexpy
amsoaoH npouepypm n nepecmnko coo6uennn coc1on1 a 1oH, u1o a nocnepneH cnyuae
cyuec1aye1 onpepenennm nonyua1enL n nn1epnpe1aunn (1o ec1L am6op nopxopnuero
He1opa, ko1opm sanyckae1cn a o1ae1 na coo6uenne) Hoxe1 6m1L pasnnuno pnn pasnmx
converted to PDF by BoJIoc
nonyua1ene. O6muno konkpe1nm nonyua1enL nensaec1en anno1L po amnonnennn
nporpaHHm, 1ak u1o onpepenn1L, kako He1op 6ype1 amsaan, sapanee neaosHoxno. B 1akoH
cnyuae roaopn1, u1o nHee1 Hec1o nospnee cansmaanne Hexpy coo6uenneH (nHeneH
npouepypm nnn qynkunn) n qparHen1oH kopa (He1opoH), ncnonLsyeHmH a o1ae1 na
coo6uenne. D1a cn1yaunn npo1naonoc1aanne1cn panneHy cansmaannk (na a1ane
koHnnnnpoaannn nnn koHnonoakn nporpaHHm) nHenn c qparHen1oH kopa, u1o nponcxopn1
npn 1papnunonnmx amsoaax npouepyp.
1.4.2. O6nsauuoc1u u o1ae1c1aeuuoc1u
OynpaHen1anLno konuenune a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn nanne1cn
nonn1ne o6nsannoc1n nnn o1ae1c1aennoc1n sa amnonnenne pec1ann. Mo sanpoc ampaxae1
1onLko c1peHnenne nonyun1L xenaeHm pesynL1a1 (a nHenno poc1aan1L uae1m 6a6yuke).
Xosnka uae1ounoro Harasnna cao6opna a am6ope cnoco6a, ko1opm npnaepe1 k xenaeHoHy
pesynL1a1y, n ne ncnm1maae1 npenn1c1an c Hoe c1oponm a a1oH acnek1e.
O6cyxpan npo6neHy a 1epHnnax o6nsannoc1e, Hm yaennunaaeH ypoaenL a6c1parnpoaannn.
D1o nosaonne1 nHe1L 6onLuyk nesaancnHoc1L Hexpy aren1aHn kpn1nueckn qak1op npn
peuennn cnoxnmx sapau. B rnaae 2 Hm 6ypeH noppo6no nccnepoaa1L, kak Hoxno
ncnonLsoaa1L o6nsannoc1n a paspa6o1ke nporpaHHnoro o6ecneuennn. Honnm na6op
o6nsannoc1e, cansannmx c onpepenennmH o6Lek1oH, uac1o onpepenne1cn c noHouLk
1epHnna npo1okon.
Pasnnune Hexpy asrnnpoH na nporpaHHnoe o6ecneuenne co c1oponm 1papnunonnoro,
c1pyk1ypnoro nopxopa n o6Lek1no-opnen1npoaanno 1ouko spennn na nero Hoxe1 6m1L
ampaxeno a qopHe napopnn na xopouo nsaec1nyk un1a1y:
3apaaa1ecL aonpocoH ne o 1oH, u1o am Hoxe1e cpena1L pnn caonx c1pyk1yp pannmx, a o
1oH, u1o c1pyk1ypm pannmx Hory1 cpena1L nn aac.
1.4.3. Knaccm u skseHnnnpm
Xo1n n nHen peno c Ono nnuL neckonLko pas, y Henn nHee1cn npnHepnoe npepc1aanenne o
ee peakunn na Ho sanpoc. H Hory cpena1L onpepenennme npepnonoxennn, nockonLky nHek
o6uyk nnqopHaunk o nkpnx, sannHakunxcn pasaepenneH uae1oa, n oxnpak, u1o Ono,
6ypyun npepc1aan1eneH a1o ka1eropnn, a o6unx uep1ax 6ype1 coo1ae1c1aoaa1L ua6nony.
Mm HoxeH ncnonLsoaa1L 1epHnn Florist pnn onncannn ka1eropnn (nnn knacca) acex nkpe,
sannHakunxcn uae1oaopc1aoH, co6paa a nee (ka1eropnk) ace 1o o6uee, u1o nH
caoc1aenno. D1a onepaunn nanne1cn a1opmH npnnunnoH o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn:
Bce o6Lek1m nannk1cn npepc1aan1ennHn, nnn akseHnnnpaHn, knaccoa. Me1op,
ak1nansnpyeHm o6Lek1oH a o1ae1 na coo6uenne, onpepenne1cn knaccoH, k ko1opoHy
npnnapnexn1 nonyua1enL coo6uennn. Bce o6Lek1m opnoro knacca ncnonLsyk1 opnn n 1e xe
He1opm a o1ae1 na opnnakoame coo6uennn.
Hpo6neHa coo6uec1aa o6Lek1no-opnen1npoaannmx nporpaHHnc1oa saknkuae1cn a
pacnpoc1panennoc1n pasnnunmx 1epHnnoa pnn o6osnauennn cxopnmx npe. Tak, a nsmke
Object Pascal knacc nasmaae1cn o6Lek1oH (1nn pannmx object), a napknaccm (ko1opme
akpa1ue 6ypy1 onncanm nnxe) nsaec1nm kak popn1enLckn knacc, knacc-npepok n 1. p.
CnoaapL-rnoccapn a konue a1o knnrn noHoxe1 aaH paso6pa1Lcn c nec1anpap1nmHn
1epHnnaHn. Mm 6ypeH ncnonLsoaa1L cornauenne, o6uee pnn o6Lek1no-opnen1npoaannmx
nsmkoa nporpaHHnpoaannn: acerpa o6osnaua1L knaccm npen1nqnka1opaHn, naunnakunHncn
c sarnaano 6ykam. HecHo1pn na caok pacnpoc1panennoc1L, pannoe cornauenne ne
nanne1cn o6nsa1enLnmH pnn 6onLunnc1aa nsmkoa nporpaHHnpoaannn.
1.4.4. Hepapxuu knaccoa u uacnepoaauue
O Ono y Henn nHee1cn 6onLue nnqopHaunn, ueH copepxn1cn a ka1eropnn Florist. H snak,
u1o ona pas6npae1cn a uae1ax n nanne1cn anapennue Harasnna (shopkeeper). H
converted to PDF by BoJIoc
porapmaakcL, u1o, aepon1no, Henn cnpocn1 o penLrax a npouecce o6pa6o1kn Hoero sanpoca
n u1o nocne onna1m Hne 6ype1 ampana kan1anunn. Bce amuenepeuncnennoe cnpaaepnnao
1akxe pnn senenunkoa, knockepoa, npopaauoa Harasnnoa n 1. p. HockonLky ka1eropnn
Florist nanne1cn 6onee ysko, ueH Shopkeeper, 1o nk6oe snanne, ko1opmH n o6napak o
ka1eropnn Shopkeeper, cnpaaepnnao 1akxe n pnn Florist, n, a uac1noc1n, pnn Ono.
Opnn ns cnoco6oa npepc1aan1L oprannsaunk Hoero snannn o Ono a1o nepapxnn
ka1eropn (pnc. 1.1). Ono npnnapnexn1 k ka1eropnn Florist; Florist nanne1cn nopka1eropne
ka1eropnn Shopkeeper. [anee, npepc1aan1enL Shopkeeper saaepoHo nanne1cn uenoaekoH, 1o
ec1L npnnapnexn1 k ka1eropnn Human 1eH caHmH n snak, u1o Ono c 6onLuo
aepon1noc1Lk nanne1cn paynornH cyuec1aoH. [anee, ka1eropnn Human aknkuena a
ka1eropnk Hnekonn1akunx (Mammal), ko1opme kopHn1 caonx pe1enmue HonokoH, a
Hnekonn1akune nannk1cn nopka1eropne xnao1nmx (Animal) n, cnepoaa1enLno, pmua1
kncnopopoH. B caok ouepepL xnao1nme nannk1cn Ha1epnanLnmHn o6Lek1aHnpyyHoa c
pasnnunmHn nnnnnHn nacnepoaannn. Knaccm npepc1aannk1cn a anpe nepapxnuecko
ppeaoanpno c1pyk1ypm, a ko1opo 6onee a6c1pak1nme knaccm (1akne, kak Material Object
nnn Animal) pacnonarak1cn a kopne pepeaa, a 6onee cneunannsnpoaannme knaccm n a
koneunoH n1ore nnpnanpyyHm pacnonarak1cn na ero konue, a ae1anx. Pncynok 1.2
nokasmaae1 1akyk nepapxnk knaccoa pnn Ono. D1a xe caHan nepapxnn aknku ae1 a ce6n
Hok xeny Be1, co6aky Oneu, Onna y1konoca, xnayuero a soonapke, a 1akxe uae1m,
ko1opme n nocnan caoe 6a6yuke.

Puc. 1.2. Hepapxnueckoe pepeao knaccoa, npepc1aannkunx pasnnunme Ha1epnanLnme
o6Lek1m
HockonLky Ono uenoaek, 1a nnqopHaunn o ne, ko1opo n o6napak, npnHennHa 1akxe, k
npnHepy, k Hoe xene Be1. Te pannme, ko1opmHn n pacnonarak a cnny npnnapnexnoc1n
nocnepne k knaccy Hnekonn1akunx, nHek1 1akxe o1nouenne k Hoe co6ake Oneu.
HnqopHaunn o6 o6Lek1ax kak o aeuax Ha1epnanLnmx nHee1 cHmcn a o1nouenn Ono, n ee
uae1oa. Mm ampaxaeH ace a1o a anpe npen nacnepoaannn:
converted to PDF by BoJIoc
Knaccm Hory1 6m1L oprannsoaanm a nepapxnueckyk c1pyk1ypy c nacneoaanneM caoc1a.
/ouepnn knacc (nnn noknacc) nacnepye1 a1pn6y1m popn1enLckoro knacca (nnn
napknacca), pacnonoxennoro amue a nepapxnueckoH pepeae
1
. Ac1pak1nu pon1enuckn
knacc a1o knacc, ne nHekun akseHnnnpoa (ero npnHepoH Hoxe1 cnyxn1L Mammal na
pnc. 1.2). On ncnonLsye1cn 1onLko pnn nopoxpennn nopknaccoa.
1.4.5. Cansmaauue u nepeonpepeneuue He1opoa
Y1konoc Onn npepc1aanne1 co6o npo6neHy pnn naue npoc1o c1pyk1ypm. H snak, u1o
Hnekonn1akune nannk1cn xnaopopnunHn, no Onn onpepenenno nanne1cn Hnekonn1akunH,
xo1n on (1ounee, ero noppyra Onnnnc) knape1 nua. 1o6m aknkun1L ero a nauy cxeHy, Hm
ponxnm na1n 1exnnky pnn npepc1aanennn ncknkuenn ns o6uero npaanna.
Mm cpenaeH a1o, ponyc1na npaanno, u1o nnqopHaunn, copepxauancn a nopknacce, Hoxe1
nepeonpepenn1L nnqopHaunk, nacnepyeHyk ns popn1enLckoro knacca. OuenL uac1o npn
peannsaunn 1akoro nopxopa He1op, coo1ae1c1aykun nopknaccy, nHee1 1o xe nHn, u1o n
coo1ae1c1aykun He1op a popn1enLckoH knacce. Hpn a1oH pnn noncka He1opa, nopxopnuero
pnn o6pa6o1kn coo6uennn, ncnonLsye1cn cnepykuee npaanno: Honck He1opa, ko1opm
amsmaae1cn a o1ae1 na onpepenennoe coo6uenne, naunnae1cn c He1opoa, npnnapnexaunx
knaccy nonyua1enn. Ecnn nopxopnun He1op ne napen, 1o nonck npoponxae1cn pnn
popn1enLckoro knacca. Honck npopanrae1cn aaepx no uenouke popn1enLcknx knaccoa po 1ex
nop, noka ne 6ype1 napen nyxnm He1op nnn noka ne 6ype1 ncuepnana
nocnepoaa1enLnoc1L popn1enLcknx knaccoa. B nepaoH cnyuae amnonnne1cn napennm
He1op, ao a1opoH ampae1cn coo6uenne o6 oun6ke. Ecnn amue a nepapxnn knaccoa
cyuec1ayk1 He1opm c 1eH xe nHeneH, u1o n 1ekyun, 1o roaopn1, u1o pannm He1op
nepeonpepenne1 nacnepyeHoe noaepenne.
[axe ecnn koHnnnn1op ne Hoxe1 onpepenn1L, kako nHenno He1op 6ype1 amsmaa1Lcn ao
apeHn amnonnennn nporpaHHm, 1o ao Hnornx nsmkax nporpaHHnpoaannn yxe na a1ane
koHnnnnpoaannn, a ne npn amnonnennn nporpaHHm Hoxno onpepenn1L, u1o nopxopnuero
He1opa ne1 aoo6ue, n ampa1L coo6uenne o6 oun6ke. Mm 6ypeH o6cyxpa1L peannsaunk
HexannsHa nepeonpepenennn a pasnnunmx nsmkax nporpaHHnpoaannn a rnaae 11.

1
3pecL npnpe1cn nonpocn1L un1a1enn aepny1Lcn k pnc. 1.2 n o6pa1n1L annHanne
na 1o, u1o cornacno npnnn1o cxeHe pepeao pac1e1 caepxy anns. HpnMeu. pe.
To1 qak1, u1o Hon xena Be1 n xosnka uae1ounoro Harasnna Ono 6ypy1 pearnpoaa1L na Hoe
coo6uenne c npnHenenneH pasnnunmx He1opoa, nanne1cn opnnH ns npnHepoa
nonnHopqnsHa. Mm 6ypeH o6cyxpa1L a1y aaxnyk coc1aanyk uac1L o6Lek1no-
opnen1npoaannoro nporpaHHnpoaannn a rnaae 14. To, u1o n, kak yxe roaopnnocL, ne snak n
ne xouy sna1L, kako nHenno He1op 6ype1 ncnonLsoaan Ono pnn amnonnennn Hoero
sanpoca, nanne1cn npnHepoH Hacknpoakn nnqopMann, ko1opan anannsnpye1cn a rnaae 17.
1.4.6. Kpa1koe usnoxeuue npuuunoa
Anan Ke, ko1oporo koe-k1o nasmaae1 o1uoH o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn, cun1ae1 cnepykune nonoxennn qynpaHen1anLnmHn xapak1epnc1nkaHn
OOH [Kay 1993]:
1. Bce nanne1cn o6Lek1oH.
2. Bmuncnennn ocyuec1annk1cn ny1eH asanHopec1ann (o6Hena pannmHn) Hexpy
o6Lek1aHn, npn ko1opoH opnn o6Lek1 1pe6ye1, u1o6m ppyro o6Lek1 amnonnnn
nekoe pec1ane. O6Lek1m asanHopec1ayk1, nocmnan n nonyuan coo6uennn.
Coo6uenne a1o sanpoc na amnonnenne pec1ann, pononnennm na6opoH
apryHen1oa, ko1opme Hory1 nonapo6n1Lcn npn amnonnennn pec1ann.
3. Kaxpm o6Lek1 nHee1 nesaancnHyk naHn1L, ko1opan coc1on1 ns ppyrnx o6Lek1oa.
converted to PDF by BoJIoc
4. Kaxpm o6Lek1 nanne1cn npepc1aan1eneH knacca, ko1opm ampaxae1 o6une
caoc1aa o6Lek1oa (1aknx, kak uenme uncna nnn cnnckn).
5. B knacce sapae1cn noaepenne (qynkunonanLnoc1L) o6Lek1a. TeH caHmH ace
o6Lek1m, ko1opme nannk1cn akseHnnnpaHn opnoro knacca, Hory1 amnonnn1L opnn n
1e xe pec1ann.
6. Knaccm oprannsoaanm a epnnyk ppeaoanpnyk c1pyk1ypy c o6unH kopneH,
nasmaaeHyk nepapxne nacnepoaannn. HaHn1L n noaepenne, cansannoe c
akseHnnnpaHn onpepenennoro knacca, aa1oHa1nueckn poc1ynnm nk6oHy knaccy,
pacnonoxennoHy nnxe a nepapxnueckoH pepeae.
1.5. Bmvucneuue u Hopenupoaauue
Bsrnnp na nporpaHHnpoaanne, nponnnkc1pnpoaannm na npnHepe c uae1aHn, aecLHa
o1nnuae1cn o1 npnamunoro nonnHannn 1oro, u1o 1akoe koHnLk1ep. Tpapnunonnan HopenL,
onncmaakuan amnonnenne nporpaHHm na koHnLk1epe, 6asnpye1cn na pyannsHe npouecc-
coc1onnne. C a1o 1oukn spennn koHnLk1ep nanne1cn apHnnnc1pa1opoH pannmx, cnepykunH
neko1opoHy na6opy nnc1pykun. On c1panc1aye1 no npoc1panc1ay naHn1n, nsmHae1
snauennn ns ee nueek (appecoa naHn1n), neko1opmH o6pasoH npeo6pasye1 nonyuennme
aennunnm, a sa1eH noHeuae1 nx a ppyrne nuekn (pnc. 1.3). Hpoaepnn snauennn,
naxopnunecn a pasnnunmx nuekax, Hm onpepenneH coc1onnne Haunnm nnn xe pesynL1a1
amuncnenn. Xo1n a1a HopenL n Hoxe1 paccHa1pnaa1Lcn kak 6onee nnn Henee 1ounm
o6pasxpanennn, nou1oamx nunkax nnn nuekax naHn1n, copepxaunx snauennn, Hano u1o ns
xn1eckoro onm1a Hoxe1 nopckasa1L, kak cnepye1 c1pyk1ypnpoaa1L sapauy.
Xo1n an1ponoHopqnme onncannn, nopo6nme 1eH, u1o un1npoaanncL amue a 1ekc1e Hnranca,
Hory1 uoknpoaa1L nkpe, qak1nueckn onn nannk1cn o1paxenneH orpoHno ampasn1enLno
cnnm He1aqop. Xypnannc1m ncnonLsyk1 He1aqopm kaxpm penL, nopo6no 1oHy, kak a1o
cpenano a nnxecnepykueH qparHen1e ns NewsWeek: B o1nnune o1 o6munoro He1opa
nporpaHHnpoaannn 1o ec1L nanncannn nporpaHHm c1pouka sa c1pouko, o6Lek1no-
opnen1npoaannan cnc1eHa koHnLk1epoa NeXT npepnarae1 c1pon1enLnme 6nokn 6onLuero
pasHepa, ko1opme paspa6o1unk Hoxe1 6mc1po co6npa1L aoepnno, nopo6no 1oHy, kak pe1n
cknapmaak1 Hosanky.
BosHoxno, nHenno a1o caoc1ao a 6onLue c1enenn, ueH ppyrne amsmaae1 uac1o
na6nkpaeHm aqqek1, korpa noanukoa o1 nnqopHa1nkn nerue yun1L nonn1nnH o6Lek1no-
opnen1npoaannoro nporpaHHnpoaannn, ueH yxe cnoxnaunxcn npoqeccnonanoa. MonopexL
6mc1po apan1npye1cn k coo1ae1c1aykunH o6mpenno xnsnn He1aqopaH, c ko1opmHn onn
ce6n uyac1ayk1 koHqop1no, a 1o apeHn kak ae1epanm o6peHenenm c1peHnenneH
npepc1aan1L ce6e npouecc amuncnenn, coo1ae1c1aykun 1papnunonnmH asrnnpaH na
nporpaHHnpoaanne.
1.5.2. Kak us6exa1u 6eckouevuo perpeccuu
Koneuno, o6Lek1m ne Hory1 ao acex cnyuanx pearnpoaa1L na coo6uenne 1onLko 1eH, u1o
aexnnao o6pauak1cn k ppyrnH c npocL6o amnonnn1L neko1opoe pec1ane. D1o npnaepe1 k
6eckoneunoHy unkny sanpocoa, kak ecnn 6m paa pxen1nLHena 1ak n ne aounn a paepL,
yc1ynan ppyr ppyry popory. Ha neko1opo c1apnn no kpane Hepe neko1opme o6Lek1m
ponxnm amnonnn1L kakyk-1o pa6o1y nepep nepecmnko sanpoca ppyrnH aren1aH. D1n
pec1ann amnonnnk1cn no-pasnoHy a pasnnunmx o6Lek1no-opnen1npoaannmx nsmkax
nporpaHHnpoaannn.
B nsmkax, rpe pnpek1nanm n o6Lek1no-opnen1npoaannm nopxopm yxnaak1cn aHec1e
(1aknx, kak C++, Object Pascal n Objective-C), peanLnme pec1ann amnonnnk1cn He1opaHn,
nanncannmHn na ocnoanoH (ne o6Lek1no-opnen1npoaannoH) nsmke. B unc1o o6Lek1no-
opnen1npoaannmx nsmkax (1aknx, kak Smalltalk n Java) a1o amnonnne1cn c noHouLk
npnHn1nanmx nnn ac1poennmx onepaun, ko1opme o6ecneunaak1cn ncnonnn1enLno
cnc1eHo 6onee nnskoro ypoann.
converted to PDF by BoJIoc
1.6. Bapuep cnoxuoc1u
Ha sape nnqopHa1nkn, 6onLunnc1ao nporpaHH nncanocL na acceH6nepe. Onn ne
coo1ae1c1ayk1 ceropnnunnH c1anpap1aH. Ho Hepe 1oro kak nporpaHHm c1anoannncL ace
cnoxnee, paspa6o1unkn o6napyxnnn, u1o onn ne a coc1onnnn noHnn1L ack nnqopHaunk,
neo6xopnHyk pnn o1napkn n coaepuenc1aoaannn nx nporpaHHnoro o6ecneuennn. Kakne
snauennn naxopn1cn a pernc1pax? Bc1ynae1 nn noam npen1nqnka1op a konqnnk1 c
onpepenennmHn panee? Kakne nepeHennme neo6xopnHo nnnunannsnpoaa1L nepep 1eH, kak
nepepa1L ynpaanenne cnepykueHy kopy?
Honanenne 1aknx nsmkoa nporpaHHnpoaannn amcokoro ypoann, kak Fortran, Cobol n Algol,
paspeunno neko1opme npo6neHm (6mno aaepeno aa1oHa1nueckoe ynpaanenne nokanLnmHn
nepeHennmHn n nenanoe npncaanaanne snauenn). OpnoapeHenno a1o aospocna aepa
nonLsoaa1ene a aosHoxnoc1n koHnLk1epa. Ho Hepe 1oro kak npepnpnnnHanncL nonm1kn
peun1L ace 6onee cnoxnme npo6neHm c ero ncnonLsoaanneH, aosnnkann cn1yaunn, korpa
paxe nyuune nporpaHHnc1m ne Hornn ypepxa1L ace a caoe naHn1n. HpnamunmHn c1ann
koHanpm nporpaHHnc1oa, pa6o1akunx coaHec1no.
1.6.1. Henuueuoe yaenuveuue cnoxuoc1u
Ho Hepe 1oro kak nporpaHHnme npoek1m c1anoannncL ace cnoxnee, 6mno saHeueno
nn1epecnoe nanenne. 3apaua, pnn peuennn ko1opo opnoHy nporpaHHnc1y 1pe6oaanocL paa
Hecnua, ne peuanacL payHn nporpaHHnc1aHn sa Hecnu. Cornacno saHeua1enLno qpase
Opepa Bpykca, poxpenne pe6enka sannHae1 pean1L Hecnuea nesaancnHo o1 1oro, ckonLko
xenunn sann1o a1nH [Brooks 1975].
Hpnunno 1akoro nennnenoro noaepennn nanne1cn cnoxnoc1L. B uac1noc1n, asanHocansn
Hexpy nporpaHHnmHn koHnonen1aHn c1ann cnoxnee, n paspa6o1unkn amnyxpenm 6mnn
noc1onnno o6Hennaa1Lcn Hexpy co6o snaun1enLnmHn o6LeHaHn nnqopHaunn. Bpykc 1akxe
ckasan:
HockonLky konc1pynpoaanne nporpaHHnoro o6ecneuennn no caoe any1penne npnpope
ec1L sapaua cnc1eHnan (1pe6ye1 cnoxnoro asanHopec1ann yuac1nnkoa), 1o pacxopm na
o6Hen pannmHn aennkn. Onn 6mc1po c1anoan1cn poHnnnpykunHn n nnaennpyk1
yHenLuenne nnpnanpyanLnmx sa1pa1, poc1nraeHoe sa cue1 pas6nennn sapaun na
qparHen1m. [o6aanenne noamx nkpe ypnnnne1, a ne cokpauae1 pacnncanne pa6o1.
Hopoxpae1 cnoxnoc1L ne npoc1o 6onLuo o6LeH paccHa1pnaaeHmx sapau, a ynnkanLnoe
caoc1ao nporpaHHnmx cnc1eH, paspa6o1annmx c ncnonLsoaanneH 1papnunonnmx
nopxopoa, 6onLuoe uncno nepekpec1nmx ccmnok Hexpy koHnonen1aHn (nHenno a1o
penae1 nx opnnHn ns nan6onee cnoxnmx nkpcknx 1aopenn). Hepekpec1nme ccmnkn a
pannoH cnyuae o6osnauak1 saancnHoc1L opnoro qparHen1a kopa o1 ppyroro.
[ec1an1enLno, kaxpm qparHen1 nporpaHHno cnc1eHm ponxen amnonnn1L nekyk
peanLnyk pa6o1y a npo1nanoH cnyuae on 6mn 6m ne nyxen. Ecnn a1a pen1enLnoc1L
okasmaae1cn neo6xopnHo pnn ppyrnx uac1e nporpaHHm, 1o ponxen npncy1c1aoaa1L no1ok
pannmx nn6o ns, nn6o a paccHa1pnaaeHyk koHnonen1y. Ho a1o npnunne nonnoe nonnHanne
qparHen1a nporpaHHm 1pe6ye1 snann kak kopa, ko1opm Hm paccHa1pnaaeH, 1ak n kopa,
ko1opm nonLsye1cn a1nH qparHen1oH. Kopoue roaopn, paxe o1nocn1enLno nesaancnHm
paspen kopa nenLsn nonnoc1Lk nonn1L a nsonnunn o1 ppyrnx.
1.6.2. MexauusHm a6c1parupoaauun
HporpaHHnc1m c1onknynncL c npo6neHo cnoxnoc1n yxe paano. 1o6m nonnoc1Lk nonn1L
aaxnoc1L o6Lek1no-opnen1npoaannoro nopxopa, naH cnepye1 paccHo1pe1L pasnoo6pasnme
HexannsHm, ko1opme ncnonLsoaanncL nporpaHHnc1aHn pnn kon1ponn nap cnoxnoc1Lk.
Inaanm ns nnx a1o a6c1parnpoaanne, 1o ec1L cnoco6noc1L o1penn1L nornueckn cHmcn
qparHen1a nporpaHHm o1 npo6neHm ero peannsaunn. B neko1opoH cHmcne o6Lek1no-
converted to PDF by BoJIoc
opnen1npoaannm nopxop aoo6ue ne nanne1cn peaonkunonnmH n ponxen paccHa1pnaa1Lcn
kak ec1ec1aennm pesynL1a1 nc1opnueckoro pasan1nn: o1 npouepyp k HopynnH, panee k
a6c1pak1nmH 1nnaH pannmx n nakoneu k o6Lek1aH.
Hpoeqypm
Hpouepypm n qynkunn 6mnn payHn nepamHn HexannsHaHn a6c1parnpoaannn,
npnHenennmHn a nsmkax nporpaHHnpoaannn. Hpouepypm nosaonnk1 ckonuen1pnpoaa1L a
opnoH Hec1e pa6o1y, ko1opan amnonnne1cn Hnorokpa1no (aosHoxno, c ne6onLunHn
aapnaunnHn), n sa1eH Hnorokpa1no ncnonLsoaa1L a1o1 kop, aHec1o 1oro u1o6m nnca1L ero
cnoaa n cnoaa. KpoHe acero npouero, npouepypm anepame o6ecneunnn aosHoxnoc1L
Hacknpoakn nnqopHaunn. HporpaHHnc1 Hor nannca1L npouepypy nnn na6op npouepyp,
ko1opme no1oH ncnonLsoaanncL ppyrnHn nkpLHn. Hocnepnne ne o6nsanm 6mnn sna1L
pe1ann ncnonLsoaannoro anropn1Ha nx nn1epecoaan 1onLko nn1epqec nporpaHHm. Ho
npouepypm ne peunnn acex npo6neH. B uac1noc1n, onn ne o6napann aqqek1nanmH
HexannsHoH Hacknpoakn pe1ane oprannsaunn pannmx n 1onLko o1uac1n cnnHann npo6neHy
ncnonLsoaannn pasnmHn nporpaHHnc1aHn opnnakoamx nHen.
HpnHep: c1ek
1o6m nponnnkc1pnpoaa1L a1n npo6neHm, paccHo1pnH cn1yaunk, korpa nporpaHHnc1y
nyxno peannsoaa1L ynpaanenne npoc1mH c1ekoH. Cnepyn c1apmH po6pmH npnnunnaH
paspa6o1kn nporpaHHnoro o6ecneuennn, nau nporpaHHnc1 npexpe acero onpepenne1
aneunn nn1epqec ckaxeH, na6op ns ue1mpex npouepyp init, push, pop n top. 3a1eH on
am6npae1 nopxopnun He1op peannsaunn. 3pecL ec1L ns uero am6pa1L: Haccna c
ykasa1eneH na aepunny c1eka, cansnm cnncok n 1. p. Hau 6ecc1paunm paspa6o1unk
am6npae1 opnn ns He1opoa, a sa1eH npnc1ynae1 k kopnpoaannk, kak nokasano a
nnc1nnre 1.1.
erko yanpe1L, u1o pannme, o6pasykune c1ek, ne Hory1 6m1L cpenanm nokanLnmHn pnn
kako-1o ns ue1mpex npouepyp, nockonLky a1n pannme nannk1cn o6unHn pnn acex ns nnx.
Ho ecnn y nac ec1L 1onLko nokanLnme nnn rno6anLnme nepeHennme (kak a1o nHee1 Hec1o
pnn Fortran nnn 6mno a C, po 1oro kak aaenn Hopnqnka1op static), 1o pannme c1eka ponxnm
copepxa1Lcn a rno6anLnmx nepeHennmx. Opnako ecnn nepeHennme nannk1cn rno6anLnmHn,
1o ne1 cnoco6a orpannun1L poc1yn k nnH nnn nx anpnHoc1L. HanpnHep, ecnn c1ek
peannsoaan kak Haccna c nHeneH datastack, 1o o6 a1oH ponxnm sna1L ace oc1anLnme
nporpaHHnc1m, nockonLky onn Hory1 saxo1e1L cospa1L nepeHennme c 1aknH xe nHeneH,
uero pena1L nn a koeH cnyuae nenLsn. 3anpe1 na ncnonLsoaanne nHenn datastack
neo6xopnH, paxe ecnn caHn pannme aaxnm 1onLko pnn nopnporpaHH pa6o1m co c1ekoH n ne
6ypy1 ncnonLsoaa1Lcn sa npepenaHn a1nx ue1mpex npouepyp. Ananornuno nHena init, push,
pop n top nannk1cn 1enepL sapesepanpoaannmHn n ne ponxnm ac1peua1Lcn a ppyrnx uac1nx
nporpaHHm (pasae u1o c uenLk amsoaa npouepyp), paxe ecnn a1n uac1n ne nHek1 nnuero
o6uero c nopnporpaHHaHn, o6cnyxnaakunHn c1ek.
Ruc1uur 1.1. Hpouepypm ne ropn1cn pnn Hacknpoakn nnqopHaunn
int datastack[100];
int datatop = 0;
void init()
{
datatop = 0;
}
void push(int val)
{ if (datatop < 100)
datastack [datatop++] = val;
}
int top()
{
converted to PDF by BoJIoc
if (datatop > 0)
return datastack [datatop 1];
return 0;
}
int pop()
{
if (datatop > 0)
return datastack [--datatop];
return 0;
}

O6nac1u anqnHoc1n qnn 6nokoa
MexannsH anpnHoc1n pnn 6nokoa, ncnonLsoaannm a nsmke Anron n ero npeeHnnkax (1aknx,
kak Pascal), npepnarae1 uy1L 6onLun kon1ponL nap anpnHoc1Lk nHen, ueH npoc1o
pasnnune Hexpy nokanLnmHn n rno6anLnmHn nHenaHn. Kaxe1cn, Hm Hornn 6m napen1Lcn,
u1o a1o peun1 npo6neHy ckpm1nn nnqopHaunn. K coxanennk, npo6neHa oc1ae1cn. B nk6o
o6nac1n, a ko1opo paspeuen poc1yn k nHenaH ue1mpex npouepyp, anpnm 1akxe n nx o6une
pannme. 1o6m peun1L a1y pnneHHy, 1pe6ye1cn paspa6o1a1L nno HexannsH
c1pyk1ypnpoaannn.
begin
var
datastack : array [1..100] of integer;
datatop : integer;
procedure init; . . .
procedure push(val : integer); . . .
function pop : integer; . . .
. . .
end;
Mopynu
B neko1opoH cHmcne Hopynn Hoxno paccHa1pnaa1L npoc1o kak ynyuuennm He1op cospannn
n ynpaanennn coaokynnoc1nHn nHen n cansannmHn c nnHn snauennnHn. Hau npnHep co
c1ekoH nanne1cn 1nnnunmH a 1oH acnek1e, u1o nHee1cn onpepenennan nnqopHaunn
(nn1epqecnme npouepypm), ko1opyk Hm xo1nH cpena1L unpoko n o1kpm1o ncnonLsyeHo,
a 1o apeHn kak poc1yn k neko1opmH pannmH (co6c1aenno pannme c1eka) ponxen 6m1L
orpannuen. Ecnn paccHa1pnaa1L HopynL kak a6c1pak1nyk konuenunk, caepennyk k caoe
npoc1eue qopHe, 1o ee cy1L coc1on1 a pas6nennn npoc1panc1aa nHen na pae uac1n.
O1kpm1an (public) uac1L nanne1cn poc1ynno nsane Hopynn, sakpm1an (private) uac1L
poc1ynna 1onLko any1pn Hopynn. Tnnm, pannme (nepeHennme) n npouepypm Hory1 6m1L
o1necenm k nk6o ns payx uac1e.
[aanp Hapnac [Parnas 1972] nonynnpnsoaan nonn1ne Hopyne. On cqopHynnpoaan
cnepykune paa npnnunna nx npaannLnoro ncnonLsoaannn:
1. HonLsoaa1enn, ko1opm naHepeaae1cn ncnonLsoaa1L HopynL, cnepye1 cna6pn1L ace
nnqopHaune, neo6xopnHo, u1o6m pena1L a1o koppek1no, n ne 6onee 1oro.
2. Paspa6o1unka cnepye1 cna6pn1L ace nnqopHaune, neo6xopnHo pnn cospannn
Hopynn, n ne 6onee 1oro.
D1a qnnocoqnn a snaun1enLno Hepe nanoHnnae1 aoennyk pok1pnny neo6xopnHoro
snannn: ecnn aaH ne nyxno sna1L onpepenennyk nnqopHaunk, am n ne ponxnm nHe1L k
ne poc1yna. D1o nanoe, naHepennoe n uenenanpaanennoe y1anaanne nnqopHaunn
nasmaae1cn Hacknpoako nnqopHaunn (information hiding).
converted to PDF by BoJIoc
Mopynn peuak1 neko1opme, no ne ace npo6neHm paspa6o1kn nporpaHHnoro o6ecneuennn.
HanpnHep, onn nosaonnk1 naueHy nporpaHHnc1y ckpm1L pe1ann peannsaunn c1eka, no u1o
pena1L, ecnn ppyrne nonLsoaa1enn saxo1n1 nHe1L paa (nnn 6onee) c1eka?
B kauec1ae 6onee cnoxnoro npnHepa npepnonoxnH, u1o nporpaHHnc1 sananne1, u1o nH
paspa6o1an noam 1nn uncnoamx o6Lek1oa, nasaannm Complex. On onpepennn
apnqHe1nueckne onepaunn pnn koHnnekcnmx aennunn cnoxenne, amun1anne, yHnoxenne
n 1. p. n aaen nopnporpaHHm pnn npeo6pasoaannn o6munmx uncen a koHnnekcnme n
o6pa1no. HHee1cn nnuL opna HanenLkan npo6neHa: Hoxno Hannnynnpoaa1L 1onLko c opnnH
koHnnekcnmH uncnoH.
KoHnnekcnme uncna apnp nn 6ypy1 nonesnm npn 1akoH orpannuennn, no a1o nHenno 1a
cn1yaunn, a ko1opo Hm okasmaaeHcn a cnyuae npoc1mx Hopyne. Hocnepnne, asn1me caHn
no ce6e, o6ecneunaak1 aqqek1nanm HexannsH Hacknpoakn nnqopHaunn, no onn ne
nosaonnk1 ocyuec1ann1L pasHnoxenne akseHnnnpoa, nop ko1opmH Hm nonnHaeH
aosHoxnoc1L cpena1L Hnoro konn o6nac1e pannmx. 1o6m cnpaan1Lcn c npo6neHo
pasHnoxennn, cneunannc1aH no nnqopHa1nke no1pe6oaanocL paspa6o1a1L noayk
konuenunk.
A6c1pak1ume 1unm pauumx
A6c1pak1nm 1nn pannmx sapae1cn nporpaHHnc1oH. C pannmHn a6c1pak1noro 1nna Hoxno
Hannnynnpoaa1L 1ak xe, kak n c pannmHn 1nnoa, ac1poennmx a cnc1eHy. Kak n nocnepnnH,
a6c1pak1noHy 1nny pannmx coo1ae1c1aye1 na6op (aosHoxno, 6eckoneunm) ponyc1nHmx
snauenn n pnp aneHen1apnmx onepaun, ko1opme Hory1 6m1L amnonnenm nap pannmHn.
HonLsoaa1enk paspeuae1cn cospaaa1L nepeHennme, ko1opme npnnnHak1 snauennn ns
ponyc1nHoro Hnoxec1aa, n Hannnynnpoaa1L nHn, ncnonLsyn nHekunecn onepaunn. K
npnHepy, nau 6ecc1paunm nporpaHHnc1 Hoxe1 onpepenn1L cao c1ek kak a6c1pak1nm 1nn
pannmx n c1ekoame onepaunn kak epnnc1aennme pec1ann, ko1opme ponyckae1cn
nponsaopn1L nap o1penLnmHn akseHnnnpaHn c1ekoa.
Mopynn uac1o ncnonLsyk1cn npn peannsaunn a6c1pak1nmx 1nnoa pannmx.
Henocpepc1aenno nornuecko asanHocansn Hexpy nonn1nnHn Hopynn n a6c1pak1noro 1nna
pannmx ne1. D1n pae npen 6nnskn, no ne npen1nunm. 1o6m noc1pon1L a6c1pak1nm 1nn
pannmx, Hm ponxnm yHe1L:
1. Dkcnop1npoaa1L onpepenenne 1nna pannmx.
2. [ena1L poc1ynnmH na6op onepaun, ncnonLsykunxcn pnn Hannnynnpoaannn
akseHnnnpaHn 1nna pannmx.
3. 3aunua1L pannme, cansannme c 1nnoH pannmx, u1o6m c nnHn Hoxno 6mno pa6o1a1L
1onLko uepes ykasannme nopnporpaHHm.
4. Cospaaa1L neckonLko akseHnnnpoa a6c1pak1noro 1nna pannmx.
B naueH onpepenennn Hopynn cnyxa1 1onLko kak HexannsH Hacknpoakn nnqopHaunn n 1eH
caHmH nenocpepc1aenno cansanm 1onLko co caoc1aaHn 2 n 3 ns nauero cnncka. Oc1anLnme
caoc1aa a npnnunne Hory1 6m1L peannsoaanm c ncnonLsoaanneH coo1ae1c1aykue 1exnnkn
nporpaHHnpoaannn. Hake1m, ko1opme ac1peuak1cn a 1aknx nsmkax nporpaHHnpoaannn, kak
CLU nnn Ada, 1ecno cansanm c nepeuncnennmHn amue 1pe6yeHmHn caoc1aaHn a6c1pak1nmx
1nnoa pannmx.
B onpepenennoH cHmcne o6Lek1 a1o npoc1o a6c1pak1nm 1nn pannmx. Ioaopnnn, k
npnHepy, u1o nporpaHHnc1m na nsmke Smalltalk nnuy1 nan6onee c1pyk1ypnpoaannme
nporpaHHm, no1oHy u1o onn ne nHek1 aosHoxnoc1n nannca1L u1o-nn6o kpoHe onpepenenn
a6c1pak1nmx 1nnoa pannmx. Hc1nnnan npaapa, u1o o6Lek1 nanne1cn a6c1pak1nmH 1nnoH
pannmx, no nonn1nn o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn, xo1n n c1pon1cn na
converted to PDF by BoJIoc
npenx a6c1pak1nmx 1nnoa pannmx, po6aannk1 k nnH aaxnme noauec1aa no uac1n
paspenennn n coaHec1noro ncnonLsoaannn nporpaHHnoro kopa.
O61ek1m: coo6euun, uacnepoaauue u nonuHopqusH
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne po6aanne1 neckonLko noamx aaxnmx npe k
konuenunn a6c1pak1nmx 1nnoa pannmx. Inaanan ns nnx nepecmnka coo6uenn. [ec1ane
nnnunnpye1cn no sanpocy, o6pauennoHy k konkpe1noHy o6Lek1y, a ne uepes amsoa
qynkunn. B snaun1enLno c1enenn a1o npoc1o cHeuenne ypapennn: 1papnunonnan 1ouka
spennn penae1 ocnoano ynop na onepaunn, a 1o apeHn kak OOH na nepaoe Hec1o c1aan1
co6c1aenno snauenne. (Bmsmaae1e nn am nopnporpaHHy push co c1ekoH n snauenneH a
kauec1ae apryHen1oa, nnn xe am npocn1e o6Lek1 stack noHec1n1L nyxnoe snauenne k neHy
any1pL?) Ecnn 6m a1o 6mno ace, u1o nHee1cn a o6Lek1no-opnen1npoaannoH
nporpaHHnpoaannn, a1a 1exnnka ne paccHa1pnaanacL 6m kak npnnunnnanLnoe
noaoaaepenne. Ho k nepecmnke coo6uenn po6aannk1cn Hounme HexannsHm
nepeonpepenennn nHen n coaHec1noro/Hnorokpa1noro ncnonLsoaannn nporpaHHnoro kopa.
Henano a npee nepecmnkn coo6uenn nanne1cn HmcnL o 1oH, u1o nn1epnpe1aunn
coo6uennn Hoxe1 Henn1Lcn pnn pasnnunmx o6Lek1oa. A nHenno noaepenne n peakunn,
nnnunnpyeHme coo6uenneH, saancn1 o1 o6Lek1a, ko1opm nonyuae1 coo6uenne. TeH caHmH
push Hoxe1 osnaua1L opno pec1ane pnn c1eka n coaceH ppyroe pnn 6noka ynpaanennn
Hexannuecko pyko. HockonLky nHena onepaun ne o6nsanm 6m1L ynnkanLnmHn, Hory1
ncnonLsoaa1Lcn npoc1me n nanme qopHm koHanp. D1o npnaopn1 k 6onee un1aeHoHy n
nonn1noHy kopy.
Hakoneu, o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne po6aanne1 HexannsHm
nacnepoaannn n nonnHopqnsHa. Hacnepoaanne nosaonne1 pasnnunmH 1nnaH pannmx
coaHec1no ncnonLsoaa1L opnn n 1o1 xe kop, npnaopn k yHenLuennk ero pasHepa n
noamuennk qynkunonanLnoc1n. HonnHopqnsH nepekpanaae1 a1o1 o6un kop 1ak, u1o6m
ypoane1aopn1L konkpe1nmH oco6ennoc1nH o1penLnmx 1nnoa pannmx. Ynop na
nesaancnHoc1L nnpnanpyanLnmx koHnonen1 nosaonne1 ncnonLsoaa1L npouecc nouaroao
c6opkn, npn ko1opo o1penLnme 6nokn nporpaHHnoro o6ecneuennn paspa6a1maak1cn,
nporpaHHnpyk1cn n o1naxnaak1cn po 1oro, kak onn o6Lepnnnk1cn a 6onLuyk cnc1eHy.
Bce a1n npen 6ypy1 onncanm 6onee noppo6no a nocnepykunx rnaaax.
1.7. Muorokpa1uo ucnonusyeHoe nporpaHHuoe
o6ecneveuue
[ecn1nne1nnHn nkpn cnpaunaann ce6n, noueHy cospanne nporpaHHnoro o6ecneuennn ne
Hoxe1 konnpoaa1L npouecc konc1pynpoaannn Ha1epnanLnmx o6Lek1oa. K npnHepy, korpa Hm
c1ponH spanne, aa1oHo6nnL nnn anek1ponnoe yc1poc1ao, Hm o6muno coepnnneH aHec1e
neckonLko ro1oamx koHnonen1 aHec1o 1oro, u1o6m nsro1oann1L kaxpm noam aneHen1 c
nynn. Moxno nn konc1pynpoaa1L nporpaHHnoe o6ecneuenne 1aknH xe o6pasoH?
Mnorokpa1noe ncnonLsoaanne nporpaHHnoro o6ecneuennn uenL, k ko1opo noc1onnno
c1peHn1cn n pepko poc1nrak1. Ocnoanan npnunna a1oro snaun1enLnan asanHosaancnHoc1L
6onLue uac1n nporpaHHnoro o6ecneuennn, cospannoro 1papnunonnmHn cnoco6aHn. Kak Hm
anpenn a npepmpyunx paspenax, 1pypno nsaneuL ns npoek1a qparHen1m nporpaHHnoro
o6ecneuennn, ko1opme 6m nerko ncnonLsoaanncL a ne nHekueH k neHy o1nouennn noaoH
nporpaHHnoH npopyk1e (kaxpan uac1L kopa o6muno cansana c oc1anLnmHn qparHen1aHn).
D1n asanHosaancnHoc1n Hory1 6m1L pesynL1a1oH onpepenennn c1pyk1ypm pannmx nnn
cnepc1aneH oco6ennoc1e qynkunonnpoaannn.
HanpnHep, oprannsaunn sannce a anpe 1a6nnum n ocyuec1anenne onepaunn ee
nnpekcnpoaannoro npocHo1pa nannk1cn o6munmH n a nporpaHHnpoaannn. TeH ne Henee po
cnx nop nopnporpaHHm noncka a 1a6nnuax sauac1yk nnuy1cn c nynn pnn kaxporo noaoro
npnnoxennn. HoueHy? Ho1oHy u1o a npnamunmx nsmkax nporpaHHnpoaannn qopHa1 sanncn
converted to PDF by BoJIoc
pnn aneHen1oa 1a6nnum xec1ko cansan c 6onee o6unH kopoH pnn ac1aakn n npocHo1pa.
Tpypno nannca1L kop, ko1opm 6m pa6o1an pnn nponsaonLno c1pyk1ypm pannmx n nk6oro
1nna sannce.
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne o6ecneunaae1 HexannsH pnn o1penennn
cyuec1aenno nnqopHaunn (sanecenne n nonyuenne sannce) o1 cneunannsnpoaanno
(konkpe1nm qopHa1 sannce). TeH caHmH npn ncnonLsoaannn o6Lek1no-opnen1npoaanno
1exnnkn Hm HoxeH cospaaa1L 6onLune nporpaHHnme koHnonen1m, npnropnme pnn
noa1opnoro ncnonLsoaannn. Mnorne koHHepueckne nake1m nporpaHHnmx koHnonen1oa,
npnropnmx pnn Hnorokpa1noro ncnonLsoaannn, yxe nHek1cn, n paspa6o1ka noa1opno
ncnonLsyeHmx nporpaHHnmx koHnonen1oa c1anoan1cn 6mc1po pasanaakuecn o1pacnLk
nnpyc1pnn nporpaHHnoro o6ecneuennn.
1.8. PesmHe
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne a1o ne npoc1o neckonLko noamx caoc1a,
po6aanennmx a yxe cyuec1aykune nsmkn. Ckopee a1o noam uar a ocHmcnennn
npoueccoa pekoHnosnunn sapau n paspa6o1kn nporpaHHnoro o6ecneuennn.
OOH paccHa1pnaae1 nporpaHHm kak coaokynnoc1L cao6opno (rn6ko) cansannmx Hexpy
co6o aren1oa, nasmaaeHmx o6Lek1aHn. Kaxpm ns nnx o1aeuae1 sa konkpe1nme sapaun.
Bmuncnenne ocyuec1anne1cn nocpepc1aoH asanHopec1ann o6Lek1oa. Cnepoaa1enLno, a
onpepenennoH cHmcne nporpaHHnpoaanne a1o nn Hnoro nn Hano, kak Hopennpoaanne
Hnpa.
O6Lek1 nonyuae1cn a pesynL1a1e nnkancynnunn coc1onnnn (pannmx) n noaepennn
(onepaun). TeH caHmH o6Lek1 ao Hnornx o1nouennnx ananornuen Hopynk nnn
a6c1pak1noHy 1nny pannmx.
Hoaepenne o6Lek1a pnk1ye1cn ero knaccoH. Kaxpm o6Lek1 nanne1cn akseHnnnpoH
neko1oporo knacca. Bce akseHnnnpm opnoro knacca 6ypy1 aec1n ce6n opnnakoamH o6pasoH
(1o ec1L amsmaa1L 1e xe He1opm) a o1ae1 na opnnakoame sanpocm.
O6Lek1 nponanne1 caoe noaepenne ny1eH amsoaa He1opa a o1ae1 na coo6uenne.
Hn1epnpe1aunn coo6uennn (1o ec1L konkpe1nm ncnonLsyeHm He1op) saancn1 o1 o6Lek1a n
Hoxe1 6m1L pasnnuno pnn pasnnunmx knaccoa o6Lek1oa.
O6Lek1m n knaccm pacunpnk1 nonn1ne a6c1pak1noro 1nna pannmx ny1eH aaepennn
nacnepoaannn. Knaccm Hory1 6m1L oprannsoaanm a anpe nepapxnueckoro pepeaa
nacnepoaannn. [annme n noaepenne, cansannme c knaccaHn, ko1opme pacnonoxenm amue a
nepapxnueckoH pepeae, poc1ynnm pnn nnxenexaunx knaccoa. Hponcxopn1 nacnepoaanne
noaepennn o1 popn1enLcknx knaccoa.
C noHouLk yHenLuennn asanHosaancnHoc1n Hexpy koHnonen1aHn nporpaHHnoro
o6ecneuennn OOH nosaonne1 paspa6a1maa1L cnc1eHm, npnropnme pnn Hnorokpa1noro
ncnonLsoaannn. Takne koHnonen1m Hory1 6m1L cospanm n o1naxenm kak nesaancnHme
nporpaHHnme epnnnum, a nsonnunn o1 ppyrnx uac1e npnknapno nporpaHHm.
Mnorokpa1no ncnonLsyeHme nporpaHHnme koHnonen1m nosaonnk1 paspa6o1unky nHe1L peno
c npo6neHaHn na 6onee amcoko c1ynenn a6c1parnpoaannn. Mm HoxeH onpepenn1L n
Hannnynnpoaa1L o6Lek1aHn npoc1o a 1epHnnax coo6uenn, ko1opme onn pacnosnak1, n
pa6o1m, ko1opyk onn amnonnnk1, nrnopnpyn pe1ann peannsaunn.
H1o vu1a1u panume
H o1He1nn panee, u1o Anan Ke cun1ae1cn o1uoH o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn. Hopo6no HnornH npoc1mH amckasmaannnH, pannoe y1aepxpenne
ampepxnaae1 kpn1nky nnuL o1uac1n. CaH Ke [Kay 1993] cun1ae1, u1o ero aknap coc1on1
converted to PDF by BoJIoc
npenHyuec1aenno a paspa6o1ke nsmka Smalltalk na ocnoae 6onee pannero nsmka
nporpaHHnpoaannn Simula, cospannoro a Ckanpnnaann a 60-x ropax [Dahl 1966,
Kirkerud 1989]. Hc1opnn canpe1enLc1aye1, u1o 6onLunnc1ao npnnunnoa o6Lek1no-
opnen1npoaannoro nporpaHHnpoaannn 6mno nonnoc1Lk paspa6o1ano cospa1ennHn nsmka
Simula, no a1o1 qak1 a snaun1enLno c1enenn nrnopnpoaancn npoqeccnonanaHn po 1ex nop,
noka onn (npnnunnm) ne 6mnn anoaL o1kpm1m KeeH npn paspa6o1ke nsmka
nporpaHHnpoaannn Smalltalk. HonLsykuncn unpoko nonynnpnoc1Lk xypnan Byte a 1981
ropy cpenan Hnoroe pnn nonynnpnsaunn konuenun, paspa6o1annmx KeeH n ero koHanpo
ns rpynnm Xerox PARC.
TepHnn kpnsnc nporpaHHnoro o6ecneuennn, no-anpnHoHy, 6mn nso6pe1en [yroH Mak-
HnpoeH ao apeHn konqepenunn HATO 1968 ropa no nporpaHHnmH 1exnonornnH. 3a6aano,
u1o Hm naxopnHcn a a1oH kpnsnce n ceuac, no npouec1ann nonoannm cpoka
cyuec1aoaannn nnqopHa1nkn kak nesaancnHo pncunnnnnm. HecHo1pn na okonuanne
xonopno aonm, amxop ns kpnsnca nporpaHHnoro o6ecneuennn ne 6nnxe k naH, ueH a1o
6mno a 1968 ropy cH., k npnHepy, c1a1Lk In66ca Xponnueckn kpnsnc nporpaHHnoro
o6ecneuennn a cen1n6pLckoH amnycke Scientific American sa 1994 rop [Gibbs 1994].
[o neko1opo c1enenn kpnsnc nporpaHHnoro o6ecneuennn a snaun1enLno Hepe nnnksnn.
HanpnHep, sapaun, paccHa1pnaaaunecn kak upesamuano cnoxnme nn1L ne1 nasap, pepko
cun1ak1cn 1akoamHn ceropnn. Hpo6neHm, ko1opme Hm xenaeH peun1L ceuac, panee
cun1anncL nenpeoponnHmHn no-anpnHoHy, a1o nokasmaae1, u1o paspa6o1ka
nporpaHHnoro o6ecneuennn rop o1 ropa nporpeccnpye1.
Un1a1a aHepnkanckoro nnnranc1a Dpaappa Cannpa (c1p. 21) asn1a ns c1a1Ln CansL
noaepennn n Hmunennn c nsmkoH, nepeneua1anno a c6opnnke Mmunenne n peanLnoc1L
[Whorf 1956]. B neH copepxn1cn neckonLko nn1epecnmx pa6o1 no cansnH Hexpy nsmkoH n
npoueccoH Hmunennn. H nac1on1enLno pekoHenpyk kaxpoHy cepLesnoHy c1ypen1y,
sannHakueHycn koHnLk1epnmHn nsmkaHn, npoun1a1L a1n c1a1Ln. Heko1opme ns nnx nHek1
ypnan1enLno 6nnskoe o1nouenne k nckycc1aennmH nsmkaH.
[pyran nn1epecnan knnra a1o Dqqek1 anqaan1a Po6ep1a orana [Logan 1986],
ko1opan o6Lncnne1 a nnnranc1nuecknx 1epHnnax, noueHy nornka n nayka 6mnn paspa6o1anm
na 3anape, a 1o apeHn kak a 1euenne aekoa Kn1a nHen onepexakuyk 1exnonornk. B 6onee
coapeHennoH nccnepoaannn o annnnnn ec1ec1aennoro nsmka na nnqopHa1nky [x. Mapuann
Ynrep [Unger 1987] onncmaae1 annnnne nnonckoro nsmka na nsaec1nm npoek1 Hn1oro
nokonennn koHnLk1epoa.
BceHn npnsnannoe na6nkpenne, u1o nsmk acknHocoa nHee1 Hnoro cnoa pnn o6osnauennn
1nnoa cnera, 6mno pasaenuano [xoqqpn HannyHoH a ero c6opnnke c1a1e no nnnranc1nke
[Pullum 1991]. B c1a1Le a Atlantic Monthly Hoxaana cnery (nnaapL 1995) Kannen Mepqn
ykasmaan, u1o na6op cnoa, ncnonLsyeHm pnn o6cyxpennn cnexno 1eHa1nkn nkpLHn,
roaopnunHn no-anrnnckn, no kpane Hepe c1onL xe pasnoo6pasen, kak n 1epHnnm
acknHocoa. Hpn a1oH, ec1ec1aenno, nHek1cn a anpy nkpn, pnn ko1opmx pasnnunn a 1nnax
cnera cyuec1aennm (npenHyuec1aenno a1o yuenme, ko1opme npoaopn1 nccnepoaannn a
panno o6nac1n).
B nk6oH cnyuae pannoe o6c1on1enLc1ao ne nHee1 snauennn pnn nauero o6cyxpennn.
Onpepenenno nc1nnno, u1o rpynnm nnpnanpyyHoa c o6unHn nn1epecaHn c1peHn1cn
paspa6o1a1L cao co6c1aennm cneunannsnpoaannm cnoaapL n, 6ypyun opnaxpm
cospannmH, on nHee1 1enpenunk nanpaann1L Hmcnn caonx 1aopuoa no ny1n, ko1opm ne
nanne1cn ec1ec1aennmH pnn nkpe sa npepenaHn rpynnm. HHenno 1akoaa cn1yaunn c OOH.
Xo1n o6Lek1no-opnen1npoaannme npen Hory1, npn napnexaue pncunnnnne, 6m1L
ncnonLsoaanm n 6es o6Lek1no-opnen1npoaannmx nsmkoa, ncnonLsoaanne nx 1epHnnoa
noHorae1 nanpaan1L yH nporpaHHnc1a no ny1n, ko1opm ne oueanpen 6es 1epHnnonornn
OOH.
Mo pacckas nanne1cn cnerka ne1ounmH a o1nouennn npnnunna pua n Haunn TLkpnnra.
pu qak1nueckn penan caoe y1aepxpenne o1nocn1enLno pekypcnanmx qynkun
[Church 1936], ko1opme anocnepc1ann okasanncL akanaanen1nmHn amuncnennnH,
converted to PDF by BoJIoc
npoaopnHmH c noHouLk Haunn TLkpnnra [Turing 1936]. B 1o qopHe, a ko1opo Hm ero
qopHynnpyeH specL, a1o1 npnnunn 6mn onncan Knnnn, n nH xe 6mno pano 1o nasaanne, nop
ko1opmH npnnunn 1enepL nsaec1en. Popxepc npnaopn1 xopouyk caopky apryHen1oa a
saun1y akanaanen1noc1n pasnnunmx Hopene amuncnenn [Rogers 1967].
Ecnn am noHnn1e, nHenno uaepckn 6o1annk Kapn nnne paspa6o1an npen popoa, anpoa n
1. p. D1o nanne1cn npo1o1nnoH cxeHm nepapxnuecko oprannsaunn, nnnkc1pnpykue
nacnepoaanne, nockonLky a6c1pak1nan knaccnqnkaunn onncmaae1 xapak1epnc1nkn,
caoc1aennme aceH knaccnqnkaunnH. BonLunnc1ao nepapxn nacnepoaannn cnepyk1
Hopenn nnnen.
Kpn1nka npouepyp kak He1opnkn a6c1parnpoaannn (nockonLky onn ne cnoco6nm o6ecneun1L
napnexaun HexannsH Hacknpoakn pannmx) 6mna anepame npoaepena BnnnLnHoH BynLqoH
n Mepn Loy [Wulf 1973] npn anannse Hnorouncnennmx npo6neH, cansannmx c
ncnonLsoaanneH rno6anLnmx nepeHennmx. D1a apryHen1aunn 6mna anocnepc1ann
pacunpena [aanpoH XanconoH [Hanson 1981].
Hopo6no HnornH cnoaaH, ko1opme naunn ce6e Hec1o a o6uenpnnn1oH xaprone, 1epHnn
o6Lek1no-opnen1npoaannm ncnonLsye1cn ropaspo unpe caoero qak1nueckoro snauennn.
TeH caHmH na aonpoc: 1o 1akoe o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne? ouenL
nenpoc1o o1ae1n1L. BLopn C1payc1pyn [Stroustrup 1988] ne 6es kHopa saHe1nn, u1o
6onLunnc1ao apryHen1oa caopn1cn k cnepykueHy cnnnornsHy:
X a1o xopouo.
O6Lek1nan opnen1npoaannoc1L a1o xopouo.
Cnepoaa1enLno, X nanne1cn o6Lek1no-opnen1npoaannmH.
Popxep Knnr apryHen1npoaanno nac1anaan, u1o ero ko1 nanne1cn o6Lek1no-
opnen1npoaannmH. KpoHe npounx caonx poc1onnc1a, ko1 peHonc1pnpye1 xapak1epnoe
noaepenne, pearnpye1 na coo6uennn, napenen ynacnepoaannmHn peakunnHn n ynpaanne1
caonH anonne nesaancnHmH any1pennnH coc1onnneH.
Mnorne aa1opm nm1anncL pa1L c1poroe onpepenenne 1ex caoc1a nsmka nporpaHHnpoaannn,
ko1opmHn on ponxen o6napa1L, u1o6m nasmaa1Lcn o6Lek1no-opnen1npoaannmH, cH., k
npnHepy, ananns, npoaepennm [xoseqnno Mnkaneq [Micallef 1988] nnn Hn1epoH
BernepoH [Wegner 1986].
Bernep, k npnHepy, pasnnuae1 nsmkn, ocnoaannme na o6Lek1ax, ko1opme noppepxnaak1
1onLko a6c1parnpoaanne (1akne, kak Ada), n o6Lek1no-opnen1npoaannme nsmkn, ko1opme
noppepxnaak1 nacnepoaanne.
[pyrne aa1opm cpepn nnx nan6onee saHe1en Bpap Kokc [Cox 1990] onpepennk1
1epHnn OOH snaun1enLno unpe. Cornacno Kokcy o6Lek1no-opnen1npoaannoe
nporpaHHnpoaanne npepc1aanne1 co6o He1op nnn uenL (objective) nporpaHHnpoaannn
ny1eH c6opkn npnnoxenn ns yxe nHekunxcn koHnonen1, a ne konkpe1nyk 1exnonornk,
ko1opyk Hm HoxeH ncnonLsoaa1L, u1o6m poc1nuL a1o uenn. BHec1o amnnunaannn pasnnun
Hexpy nopxopaHn Hm ponxnm o6Lepnnn1L aoepnno nk6me cpepc1aa, ko1opme okasmaak1cn
Hnoroo6euakunHn na ny1n k noao Hnpyc1pnanLno Peaonkunn a nporpaHHnpoaannn.
Knnra Kokca no OOH [Cox 1986], xo1n n nanncana na sape pasan1nn o6Lek1no-
opnen1npoaannoro nporpaHHnpoaannn, n a cnny a1oro o1uac1n yc1apeauan a o1nouennn
pe1ane, 1eH ne Henee nanne1cn opnnH ns nan6onee un1aeHmx Hannqec1oa o6Lek1no-
opnen1npoaannoro panxennn.


converted to PDF by BoJIoc
Ynpaxueuun
1. B o6Lek1no-opnen1npoaanno nepapxnn nacnepoaannn kaxpm cnepykun ypoaenL
nanne1cn 6onee cneunannsnpoaanno qopHo npepmpyuero. Hpnaepn1e npnHep
nepapxnn ns noacepneano xnsnn c a1nH caoc1aoH. Heko1opme ns nepapxn,
o6napyxnaaeHme a peanLno xnsnn, ne nannk1cn nepapxnnHn nacnepoaannn.
Ykaxn1e npnHep nepapxnn 6es caoc1aa nacnepoaannn.
2. HocHo1pn1e snauenne cnoaa napapnrHa no kpane Hepe a 1pex cnoaapnx.
Coo1necn1e a1n onpepenennn c nsmkaHn nporpaHHnpoaannn.
3. BosLHn1e sapauy ns peanLnoro Hnpa (ananornunyk nepecmnke uae1oa,
paccHo1penno panee) n onnun1e ee peuenne a 1epHnnax aren1oa (o6Lek1oa) n
o6nsannoc1e.
4. Ecnn am snakoHm c payHn (nnn 6onee) pasnnunmHn nsmkaHn nporpaHHnpoaannn,
npnaepn1e npnHep, korpa opnn nsmk nanpaanne1 HmcnL nporpaHHnc1a k
onpepenennoHy peuennk, a ppyro c1nHynnpye1 anL1epna1nanoe peuenne.
5. Ecnn am snakoHm c payHn (nnn 6onee) ec1ec1aennmHn nsmkaHn, onnun1e cn1yaunk,
korpa opnn nsmk nanpaanne1 roaopnuero a opnoH nanpaanennn, a 1o apeHn kak
ppyro nsmk npnaopn1 k nnoHy xopy Hmcnn.
6. Hpnaepn1e apryHen1m sa n npo1na 1o 1oukn spennn, u1o pa6o1a c koHnLk1epoH a
caoe ocnoae nanne1cn HopennpoaanneH. BosHoxno, aaH saxoue1cn npouec1L c1a1Lk
Anana Ken a Scientific American [Kay 1977].
Fnaaa 2

O61ek1uo- opueu1upoaauuoe npoek1upoaauue
Korpa nporpaHHnc1m cnpaunaak1 ppyr ppyra: eH xe, a konue konuoa, nanne1cn
o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne?, o1ae1 uaue acero nopuepknaae1
cnn1akcnueckne caoc1aa 1aknx nsmkoa, kak C++ nnn Object Pascal, no cpaanennk c nx
6onee pannnHn, ne o6Lek1no-opnen1npoaannmHn aepcnnHn, 1o ec1L C nnn Pascal. TeH
caHmH o6cyxpenne o6muno nepexopn1 na 1akne npepHe1m, kak knaccm n nacnepoaanne,
nepecmnka coo6uenn, anp1yanLnme n c1a1nueckne He1opm. Ho npn a1oH onyckak1
nan6onee aaxnm HoHen1 a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn, ko1opm ne
nHee1 nnuero o6uero c aonpocaHn cnn1akcnca.
Pa6o1a na o6Lek1no-opnen1npoaannoH nsmke (1o ec1L na nsmke, ko1opm noppepxnaae1
nacnepoaanne, nepecmnky coo6uenn n knaccm) ne nanne1cn nn neo6xopnHmH, nn
poc1a1ounmH ycnoaneH pnn 1oro, u1o6m sannHa1Lcn o6Lek1no-opnen1npoaannmH
nporpaHHnpoaanneH. Kak Hm nopuepknynn a rnaae 1, nan6onee aaxnm acnek1 a OOH
1exnnka npoek1npoaannn, ocnoaannan na ampenennn n pacnpepenennn o6nsannoc1e. Ona
6mna nasaana npoek1npoaanneH na ocnoae o6nsannoc1e nnn npoek1npoaanneH na ocnoae
o1ae1c1aennoc1n (responsibility-driven design) [Wirfs-Brock 1989b, Wirfs-Brock 1990].
2.1. O1ae1c1aeuuoc1u noppasyHeaae1 ueaHema1enuc1ao
Kak Hoxe1 konc1a1npoaa1L nk6o, k1o noHnn1 ce6n pe6enkoH (nnn k1o aocnn1maae1 pe1e),
o1ae1c1aennoc1L o6okpooc1pm Heu. Korpa am sac1aanne1e kako-nn6o o6Lek1 (nanne1cn
nn on pe6enkoH, nnn nporpaHHno cnc1eHo) 6m1L o1ae1c1aennmH sa konkpe1nme pec1ann,
am oxnpae1e c ero c1oponm onpepenennoro noaepennn, no kpane Hepe noka ne napyuenm
converted to PDF by BoJIoc
npaanna. Ho, a paano c1enenn aaxno, u1o o1ae1c1aennoc1L noppasyHeaae1 onpepenennm
ypoaenL nesaancnHoc1n nnn neaHeua1enLc1aa. Ecnn am ckaxe1e caoe pouke, u1o ona
o1aeuae1 sa y6opky caoe koHna1m, am, kak npaanno, ne c1on1e pnpoH c ne n ne
na6nkpae1e sa amnonnenneH pa6o1m a1o npo1naopeunno 6m nonn1nk o1ae1c1aennoc1n.
BHec1o a1oro am paccun1maae1e, u1o nocne ampaun pacnopnxennn 6ype1 nonyuen xenaeHm
pesynL1a1.
Ananornuno a cnyuae npnHepa c uae1aHn ns rnaam 1, korpa n nepepak sanpoc xosnke
uae1ounoro Harasnna c npocL6o nepecna1L uae1m, n ne sapyHmaakcL o 1oH, kak Ho
sanpoc 6ype1 o6cnyxen. Xosnka uae1ounoro Harasnna, pas yx ona asnna na ce6n
o1ae1c1aennoc1L, pec1aye1 6es aHeua1enLc1aa c Hoe c1oponm.
Pasnnua Hexpy 1papnunonnmH n o6Lek1no-opnen1npoaannmH nporpaHHnpoaanneH ao
Hnornx o1nouennnx nanoHnnae1 pasnnune Hexpy ak1nanmH na6nkpenneH sa 1eH, kak
pe6enok amnonnne1 pa6o1y, n nepepaue (penernpoaanneH) pe6enky o1ae1c1aennoc1n sa
a1y pen1enLnoc1L. Tpapnunonnoe nporpaHHnpoaanne ocnoamaae1cn a ocnoanoH na
npnkasannnx ueHy-nn6o cpena1L u1o-1o k npnHepy, Hopnqnunpoaa1L sanncL nnn
o6noan1L Haccna pannmx. TeH caHmH qparHen1 kopa npnansan nocpepc1aoH nepepaun
ynpaanennn n cornauenn o c1pyk1ype pannmx ko HnornH ppyrnH paspenaH nporpaHHno
cnc1eHm. Takne saancnHoc1n Hory1 aosnnka1L uepes ncnonLsoaanne rno6anLnmx
nepeHennmx, snauenn ykasa1ene nnn nonpoc1y ns-sa nenpaannLnoro npnHenennn nnn
saancnHo peannsaunn ppyrnx qparHen1oa kopa. Hpoek1npoaanne, ocnoaannoe na
o1ae1c1aennoc1n, c1apae1cn o1ceka1L a1n cansn nnn no kpane Hepe cpena1L nx nac1onLko
cna6mHn, nackonLko a1o aosHoxno.
C nepaoro asrnnpa npen kaxe1cn ne 6onee cnoxno, ueH nonn1nn Hacknpoakn nnqopHaunn
n HopynLnoc1n, ko1opme aaxnm npn nporpaHHnpoaannn a uenoH, a 1oH uncne n npn
ncnonLsoaannn 1papnunonnmx nsmkoa. Ho npoek1npoaanne, ocnoaannoe na pacnpepenennn
o1ae1c1aennoc1n, nopnnHae1 Hacknpoaky pannmx c ypoann 1exnnkn po ypoann nckycc1aa.
Hpnnunn Hacknpoakn nnqopHaunn c1anoan1cn xnsnenno aaxnmH npn nepexope o1
nporpaHHnpoaannn a HanoH k nporpaHHnpoaannk a 6onLuoH.
Opno ns ocnoanmx npenHyuec1a OOH na6nkpae1cn, korpa nporpaHHnme nopcnc1eHm
Hnorokpa1no ncnonLsyk1cn a pasnmx npoek1ax. HanpnHep, nporpaHHa, ynpaannkuan
HopennpoaanneH (nopo6no 1o, ko1opyk Hm 6ypeH paspa6a1maa1L a rnaae 6), Hoxe1
nHn1npoaa1L kak panxenne 6nnLnppnmx uapoa no c1ony, 1ak n nepeHeuenne pm6m a
unc1epnax. D1a cnoco6noc1L kopa k Hnorokpa1noHy ncnonLsoaannk nenanmH o6pasoH
noppasyHeaae1, u1o a nporpaHHnoH o6ecneuennn nou1n ne ponxno 6m1L npo6neHno-
saancnHmx koHnonen1oa ono ponxno nonnoc1Lk penernpoaa1L o1ae1c1aennoc1L sa
cneunqnunoe noaepenne k qparHen1aH konkpe1no cnc1eHm. YHennk cospaaa1L nopo6nm
Hnorokpa1no ncnonLsyeHm kop ne 1ak npoc1o nayun1Lcn ono 1pe6ye1 onm1a,
1ua1enLnoro nccnepoaannn yue6nmx npnHepoa (napapnrH, a ncxopnoH snauennn a1oro
cnoaa) n ncnonLsoaannn nsmkoa nporpaHHnpoaannn, a ko1opmx 1akoe penernpoaanne
nanne1cn ec1ec1aennmH n nerko ampaxaeHmH. B nocnepykunx rnaaax Hm npnaepeH
neckonLko npnHepoa.
2.2. HporpaHHupoaauue a HanoH u a 6onumoH
O paspa6o1ke nnpnanpyanLnoro npoek1a uac1o roaopn1 kak o nporpaHHnpoaannn a
HanoH, a o peannsaunn 6onLuoro npoek1a kak o nporpaHHnpoaannn a 6onLuoH.
[nn nporpaHHnpoaannn a HanoH xapak1epnm cnepykune npnsnakn:
Kop paspa6a1maae1cn epnnc1aennmH nporpaHHnc1oH nnn, aosHoxno, ne6onLuo
rpynno nporpaHHnc1oa. O1penLno asn1m nnpnanpyyH Hoxe1 nonn1L ace acnek1m
npoek1a, o1 n po.
Ocnoanan npo6neHa npn paspa6o1ke coc1on1 a npoek1npoaannn nporpaHHm n
nanncannn anropn1Hoa pnn peuennn noc1aanenno sapaun.
converted to PDF by BoJIoc
C ppyro c1oponm, nporpaHHnpoaanne a 6onLuoH napenne1 nporpaHHnm npoek1
cnepykunHn caoc1aaHn:
HporpaHHnan cnc1eHa paspa6a1maae1cn 6onLuo koHanpo nporpaHHnc1oa. Hpn
a1oH opna rpynna Hoxe1 sannHa1Lcn npoek1npoaanneH (nnn cneunqnkaune)
cnc1eHm, ppyran ocyuec1ann1L nanncanne kopa o1penLnmx koHnonen1, a
1pe1Ln o6Lepnnn1L qparHen1m a koneunm npopyk1. He1 epnnc1aennoro
uenoaeka, ko1opm snan 6m ace o npoek1e.
Ocnoanan npo6neHa a npouecce paspa6o1kn nporpaHHnoro o6ecneuennn
ynpaanenne npoek1oH n o6Hen nnqopHaune Hexpy rpynnaHn n any1pn rpynn.
B 1o apeHn kak naunnakun c1ypen1 o6muno snakoHn1cn c nporpaHHnpoaanneH a HanoH,
oco6ennoc1n Hnornx o6Lek1no-opnen1npoaannmx nsmkoa nannyuunH o6pasoH nonnHak1cn
npn ac1peue c npo6neHaHn, 1nnnunmHn pnn nporpaHHnpoaannn a 6onLuoH. TeH caHmH
neko1opoe npepc1aanenne o 1pypnoc1nx, aosnnkakunx npn paspa6o1ke 6onLunx cnc1eH,
nanne1cn nonesnmH pnn nonnHannn OOH.
2.3. HoveHy uapo uavuua1u c qyukuouupoaauun?
Hs-sa uero npouecc npoek1npoaannn naunnak1 c anannsa qynkunonnpoaannn nnn
noaepennn cnc1eHm? Hpoc1o o1ae1 coc1on1 a 1oH, u1o noaepenne cnc1eHm o6muno nsaec1no
saponro po oc1anLnmx ee caoc1a.
Hpepuec1aoaaaune He1opm paspa6o1kn nporpaHHnoro o6ecneuennn konuen1pnpoaanncL na
1aknx npenx, kak xapak1epnc1nkn ocnoanmx pannmx nnn xe o6uan c1pyk1ypa amsoaa
qynkun. Ho c1pyk1ypnme aneHen1m npnnoxennn Hory1 6m1L onpepenenm 1onLko nocne
nn1encnanoro anannsa sapaun. Coo1ae1c1aenno npouecc qopHanLno cneunqnkaunn uac1o
sakanunaancn cospanneH pokyHen1a, ko1opm ne nonnHann nn nporpaHHnc1m, nn knnen1m.
Ho noaepenne a1o neu1o, u1o Hoxe1 6m1L onncano a HoHen1 aosnnknoaennn npen
nporpaHHm n (a o1nnune o1 qopHanLno cneunqnkaunn cnc1eHm) ampaxeno a 1epHnnax,
nHekunx snauenne kak pnn nporpaHHnc1a, 1ak n pnn knnen1a.
Mm nponnnkc1pnpyeH npoek1npoaanne na ocnoae o6nsannoc1e (nnn RDD-
npoek1npoaanne Responsibility-Driven-Design) na yue6noH npnHepe.
2.4. Yve6um npuHep: npoek1upoaauue ua ocuoae
o6nsauuoc1e
Hpepc1aaL1e ce6e, u1o am nanne1ecL rnaanmH apxn1ek1opoH nporpaHHnmx cnc1eH a
aepyue koHnLk1epno qnpHe. B opnn npekpacnm penL aau nauanLnnk nonanne1cn a
oqnce c npee, ko1opan, kak on napee1cn, 6ype1 ouepepnmH ycnexoH koHnannn. BaH
nopyuak1 paspa6o1a1L cnc1eHy nop nasaanneH Interactive Intelligent Kitchen Helper
(Hn1epak1nanm pasyHnm kyxonnm noHounnk) (pnc. 2.1)
converted to PDF by BoJIoc

Puc. 2.1. Bneunn anp nporpaHHm Hn1epak1nanm pasyHnm kyxonnm noHounnk
3apaua, noc1aanennan nepep aaue koHanpo nporpaHHnc1oa, cqopHynnpoaana a
neckonLknx ckynmx cnoaax (nanncannmx na ueH-1o, u1o okasmaae1cn ncnonLsoaanno
o6epenno canqe1ko, npnueH nouepkoH, npnnapnexaunH aaueHy nauanLnnky).
2.4.1. Hu1epak1uaum pasyHum kyxouum noHouuk
HporpaHHa Hn1epak1nanm pasyHnm kyxonnm noHounnk (Interactive Intelligent Kitchen
Helper, IIKH) npepnasnauena pnn nepconanLnmx koHnLk1epoa. Ee uenL saHenn1L co6o
na6op kap1ouek c peuen1aHn, ko1opm Hoxno ac1pe1n1L nou1n a kaxpo kyxne. Ho noHnHo
aepennn 6asm pannmx peuen1oa, IIKH noHorae1 a nnannpoaannn nn1annn na pnn1enLnm
nepnop nanpnHep, na nepenk anepep. HonLsoaa1enL nporpaHHm IIKH capn1cn sa
koHnLk1ep, npocHa1pnaae1 6asy pannmx peuen1oa n a pnanoroaoH pexnHe onpepenne1
Henk na aecL 1pe6yeHm nepnop.
Kak a1o o6muno 6maae1 npn nepaonauanLnoH onncannn Hnornx nporpaHHnmx cnc1eH,
cneunqnkaunn pnn IIKH a snaun1enLno c1enenn paycHmcnenna a o1nouennn pnpa aaxnmx
nynk1oa. KpoHe 1oro, npoek1 n paspa6o1ka nporpaHHno cnc1eHm IIKH no1pe6ye1
coaHec1nmx ycnnn neckonLknx nporpaHHnc1oa. TeH caHmH nepaonauanLnan uenL koHanpm
paspa6o1unkoa coc1on1 a 1oH, u1o6m cpena1L ncnmHn paycHmcnennme Hec1a n naHe1n1L
pas6nenne npoek1a na koHnonen1m, c 1eH u1o6m pacnpepenn1L nx Hexpy o1penLnmHn
unenaHn koHanpm.
KpaeyronLnmH kaHneH a OOH nanne1cn xapak1epnc1nka nporpaHHnoro o6ecneuennn a
1epHnnax noaeennn, 1o ec1L a 1epHnnax pec1an, ko1opme ponxnm 6m1L amnonnenm. Mm
yanpnH aonnouenne a xnsnL a1oro npnnunna na Hnornx ypoannx npouecca paspa6o1kn
IIKH. HepaonauanLno koHanpa nonm1ae1cn oxapak1epnsoaa1L na ouenL amcokoH ypoane
a6c1parnpoaannn noaepenne npnnoxennn a uenoH. 3a1eH ona saHe1cn onncanneH
noaepennn pasnnunmx nporpaHHnmx nopcnc1eH. H 1onLko 1orpa, korpa ace acnek1m
noaepennn 6ypy1 ampenenm n onncanm, nporpaHHnc1m-paspa6o1unkn npnc1ynn1 k a1any
kopnpoaannn. B cnepykunx paspenax Hm 6ypeH o1cnexnaa1L a1anm pa6o1m koHanpm
nporpaHHnc1oa npn cospannn pannoro npnnoxennn.
2.4.2. Pa6o1a no ceuapum
Hepao sapaue nanne1cn y1ounenne cneunqnkaunn. Kak Hm yxe saHe1nnn, ncxopnme
cneunqnkaunn nou1n acerpa paycHmcnennm n nenonn1nm ao aceH, kpoHe nan6onee o6unx
converted to PDF by BoJIoc
nonoxenn. Ha a1oH a1ane c1aan1cn neckonLko uene. Opno ns nnx nanne1cn nyuuee
nonnHanne n ouyuenne 1oro, ueH 6ype1 koneunm npopyk1 (npnnunn nocHo1pn n
nouyac1ay pnn npoek1npoaannn cnc1eHm). 3a1eH a1a nnqopHaunn Hoxe1 6m1L
aosapauena nasap knnen1y (a pannoH cnyuae aaueHy nauanLnnky), u1o6m yanpe1L,
naxopn1cn nn ona a coo1ae1c1ann c ncxopno konuenune. Bepon1no n, aosHoxno,
nens6exno 1o, u1o cneunqnkaunn pnn koneunoro npopyk1a 6ypy1 nsHenn1Lcn ao apeHn
paspa6o1kn nporpaHHno cnc1eHm, n noa1oHy aaxno, u1o6m npoek1 Hor nerko aknkua1L a
ce6n noame npen, a 1akxe u1o6m no1enunanLno aosHoxnme ncnpaanennn 6mnn amnanenm
kak Hoxno panLue cH. paspen 2.6.2. Io1oanoc1L k nsHenennnH. Ha a1oH xe a1ane
npoaopn1cn o6cyxpenne c1pyk1ypm 6ypyue nporpaHHno cnc1eHm. B uac1noc1n, pec1ann,
ocyuec1anneHme nporpaHHno cnc1eHo, pas6naak1cn na koHnonen1m.
2.4.3. Hpeu1uqukaun koHnoueu1
Cospanne cnoxno qnsnuecko cnc1eHm, nopo6no spannk nnn panra1enk aa1oHo6nnn,
ynpouae1cn c noHouLk pas6nennn npoek1a na c1pyk1ypnme epnnnum. Touno 1ak xe
paspa6o1ka nporpaHHnoro o6ecneuennn o6neruae1cn nocne ampenennn o1penLnmx
koHnonen1 nporpaHHm. KoHnonen1a a1o npoc1o a6c1pak1nan epnnnua, ko1opan Hoxe1
amnonnn1L onpepenennyk pa6o1y (1o ec1L nHe1L onpepenennme o6nsannoc1n). Ha a1oH
a1ane ne1 neo6xopnHoc1n sna1L a 1ounoc1n 1o, kak sapae1cn koHnonen1a nnn kak ona 6ype1
amnonnn1L caok pa6o1y. KoHnonen1a Hoxe1 a koneunoH n1ore 6m1L npeo6pasoaana a
o1penLnyk qynkunk, c1pyk1ypy nnn knacc, nnn xe a coaokynnoc1L ppyrnx koHnonen1
(ua6non). Ha a1oH ypoane paspa6o1kn nHek1cn pae aaxnme oco6ennoc1n:
koHnonen1a ponxna nHe1L ne6onLuo na6op ue1ko onpepenennmx o6nsannoc1e;
koHnonen1a ponxna asanHopec1aoaa1L c ppyrnHn koHnonen1aHn nac1onLko cna6o,
nackonLko a1o aosHoxno.
Hospnee Hm noroaopnH o a1opo oco6ennoc1n noppo6nee. Ceuac Hm npoc1o sannHaeHcn
onpepenenneH o6nsannoc1e koHnonen1.
2.5. CRC-kap1ovka cnoco6 sanucu o6nsauuoc1e
1o6m amnan1L o1penLnme koHnonen1m n onpepenn1L nx o6nsannoc1n, koHanpa
nporpaHHnc1oa npopa6a1maae1 cuenapn cnc1eHm. To ec1L aocnponsaopn1cn sanyck
npnnoxennn, kak ecnn 6m ono 6mno yxe ro1oao. k6oe pec1ane, ko1opoe Hoxe1
nponso1n, npnnncmaae1cn neko1opo koHnonen1e a kauec1ae ee o6nsannoc1n.

B kauec1ae coc1aano uac1n a1oro npouecca nonesno nso6paxa1L koHnonen1m c noHouLk
ne6onLunx nnpekcnmx kap1ouek. Ha nnueao c1opone kap1oukn nanncanm nHn koHnonen1m,
converted to PDF by BoJIoc
ee o6nsannoc1n n nHena ppyrnx koHnonen1, c ko1opmHn ona ponxna asanHopec1aoaa1L.
Takne kap1oukn nnorpa nasmaak1cn CRC-kap1oukaHn o1 cnoa Component, Responsibility,
Collaborator (koHnonen1a, o6nsannoc1L, co1pypnnkn) [Beck 1989]. Ho Hepe 1oro kak pnn
koHnonen1 amnannk1cn o6nsannoc1n, onn sanncmaak1cn na nnueao c1opone CRC-
kap1oukn.
2.5.1. Qa1e koHnoueu1aH qusuvecku o6pas
Hpn npopa6o1ke cuenapnn nonesno pacnpepenn1L CRC-kap1oukn Hexpy pasnnunmHn
unenaHn npoek1no rpynnm. enoaek, nHekun kap1ouky, ko1opan npepc1aanne1
onpepenennyk koHnonen1y, sanncmaae1 ee o6nsannoc1n n ncnonnne1 qynkunn saHenn1enn
nporpaHHm ao apeHn Hopennpoaannn cuenapnn. On onncmaae1 pec1ann nporpaHHno
cnc1eHm, nepepaaan ynpaanenne cnepykueHy uneny koHanpm, korpa nporpaHHnan
cnc1eHa nyxpae1cn a ycnyrax ppyrnx koHnonen1.
HpenHyuec1ao CRC-kap1ouek a 1oH, u1o onn unpoko poc1ynnm, nepoporn n c nnx Hoxno
c1npa1L nnqopHaunk. D1o c1nHynnpye1 akcnepnHen1npoaanne, nockonLky anL1epna1nanme
npoek1m Hory1 6m1L ncnpo6oaanm, nsyuenm n o16pouenm c HnnnHanLnmHn sa1pa1aHn.
Onsnueckoe paspenenne kap1ouek c1nHynnpye1 nn1yn1nanoe nonnHanne aaxnoc1n
nornueckoro paspenennn koHnonen1, u1o noHorae1 cpena1L ynop na cansnoc1n any1pn
Hopyne n sauennennn Hexpy HopynnHn (ko1opme akpa1ue 6ypy1 onncanm nnxe).
He6onLuo pasHep nnpekcno kap1oukn cnyxn1 xopoue ouenko npnHepno cnoxnoc1n
o1penLnoro qparHen1a koHnonen1m, ko1opo npnnncmaae1cn 6onLue sapau, ueH Hoxe1
noHec1n1Lcn na ee kap1ouke, aepon1no, nanne1cn nsnnune cnoxno, n ponxno 6m1L
napeno 6onee npoc1oe peuenne. Moxe1 6m1L, cnepye1 nepecHo1pe1L paspenenne
o6nsannoc1e nnn pas6n1L koHnonen1y na pae.
2.5.2. ukn v1o/k1o
Kak Hm saHe1nnn a nauane nauero o6cyxpennn, ampenenne koHnonen1 nponsaopn1cn ao
apeHn npouecca Hmcnennoro npepc1aanennn pa6o1m cnc1eHm. ac1o a1o nponcxopn1 kak
unkn aonpocoa u1o/k1o. Bo-nepamx, koHanpa nporpaHHnc1oa onpepenne1: u1o 1pe6ye1cn
pena1L? D1o neHepnenno npnaopn1 k aonpocy: k1o 6ype1 amnonnn1L pec1ane? TenepL
nporpaHHnan cnc1eHa a snaun1enLno Hepe c1anoan1cn noxoxa na nekyk oprannsaunk,
ckaxeH, kap1ounm kny6. [ec1ann, ko1opme ponxnm 6m1L amnonnenm, npnnncmaak1cn
neko1opo koHnonen1e a kauec1ae ee o6nsannoc1e.
Honynnpnan nakneka o1 xeaa1enLno pesnnkn y1aepxpae1, u1o apeHn o1 apeHenn Hoxe1 n
ponxno cnon1anno nponcxopn1L neo6LncnnHoe. (Hakneka o1 xeaa1enLno pesnnkn
ncnonLsye1 uy1L 6onee kopo1kyk qpasy.) Mm snaeH, opnako, u1o a peanLno xnsnn a1o
apnp nn cnpaaepnnao. Ecnn nponcxopn1 neko1opoe pec1ane, ponxen 6m1L n aren1,
ko1opoHy npepnncano amnonnn1L a1o pec1ane. Touno 1ak xe kak a kap1ounoH kny6e
kaxpoe pec1ane npnnncano onpepenennmH nnpnanpyyHaH, npn oprannsaunn o6Lek1no-
opnen1npoaanno nporpaHHm kaxpoe pec1ane nanne1cn o6nsannoc1Lk neko1opo
koHnonen1m. Cekpe1 xopouero o6Lek1no-opnen1npoaannoro npoek1a coc1on1 a 1oH, u1o6m
yc1anoan1L aren1a pnn kaxporo pec1ann.
2.5.3. QokyHeu1upoaauue
Ha a1oH a1ane cnepye1 naua1L paspa6o1ky pokyHen1aunn. [aa pokyHen1a ponxnm nann1Lcn
cyuec1aennmHn coc1aanmHn uac1nHn nk6o nporpaHHno cnc1eHm: pykoaopc1ao
nonLsoaa1enn n npoek1nan pokyHen1aunn cnc1eHm. Pa6o1a nap kaxpmH ns nnx Hoxe1
naunna1Lcn po 1oro, kak nanncana nepaan c1pouka nporpaHHnoro kopa.
Pykoaopc1ao nonLsoaa1enn onncmaae1 asanHopec1ane c cnc1eHo c 1oukn spennn
nonLsoaa1enn. D1o o1nnunoe cpepc1ao npoaepkn 1oro, u1o konuenunn koHanpm
nporpaHHnc1oa-paspa6o1unkoa coo1ae1c1aye1 Hnennk knnen1a. HockonLky peuennn,
npnnn1me a npouecce npopa6o1kn cuenapnn, coo1ae1c1ayk1 pec1annH, ko1opme
converted to PDF by BoJIoc
no1pe6yk1cn o1 nonLsoaa1enn npn ncnonLsoaannn nporpaHHm, 1o nanncanne pykoaopc1aa
nonLsoaa1enn ec1ec1aennmH o6pasoH yansmaae1cn c npoueccoH npopa6o1kn cuenapnn.
Hepep 1eH kak nanncan kako-nn6o kycok kopa, Hmunenne koHanpm nporpaHHnc1oa ao
HnoroH noxoxe na cosnanne koneunmx nonLsoaa1ene. To ec1L nHenno na a1oH a1ane
paspa6o1unkn Hory1 nan6onee nerko npepyrapa1L 1e aonpocm, na ko1opme noanuky-
nonLsoaa1enk nonapo6n1cn o1ae1m.
B1opo cyuec1aennm pokyHen1 npoek1nan pokyHen1aunn. Ona npo1okonnpye1 ocnoanme
peuennn, npnnn1me npn nnannpoaannn nporpaHHm, n, cnepoaa1enLno, ponxna cospaaa1Lcn
a 1o1 HoHen1, korpa a1n peuennn eue caexn a naHn1n cospa1ene, a ne ropoH nosxe.
3auac1yk Hnoro npoue nannca1L o6uee rno6anLnoe onncanne nporpaHHno cnc1eHm a
nauane paspa6o1kn. 3a1eH, ec1ec1aenno, coaepuae1cn nepexop k ypoank o1penLnmx
koHnonen1 nnn Hopyne.
Xo1n a paano Hepe aaxno pokyHen1npoaa1L nporpaHHy na ypoane Hopyne, cnnukoH
6onLuoe annHanne k pe1annH oprannsaunn kaxporo qparHen1a cpenae1 cnoxnmH pnn
nocnepykunx nporpaHHnc1oa, ocyuec1annkunx conpoaoxpenne nporpaHHno cnc1eHm,
qopHnpoaanne o6ue kap1nnm npnnoxennn.
CRC-kap1oukn nannk1cn opnnH ns anpoa npoek1no pokyHen1aunn, no Hnorne ppyrne
aaxnme peuennn ne o1paxenm a nnx. ApryHen1m sa n npo1na kaxpo aaxno anL1epna1nam
npn npoek1npoaannn ponxnm sanncmaa1Lcn, paano kak n qak1opm, ko1opme noannnnn na
koneunoe peuenne. [onxen aec1ncL npo1okon nnn pneannk xopa npoek1a. Kak pykoaopc1ao
nonLsoaa1enn, 1ak n npoek1nan pokyHen1aunn y1ounnk1cn n nsHennk1cn a npouecce pa6o1m
a 1ounoH coo1ae1c1ann c 1eH, kak Hopnqnunpye1cn co6c1aenno nporpaHHa.
2.6. KoHnoueu1m u noaepeuue
BepneHcn k nporpaHHe IIKH. KoHanpa paspa6o1unkoa peuae1, u1o korpa cnc1eHa naunnae1
pa6o1y, nonLsoaa1enL anpn1 npnaneka1enLnoe nnqopHaunonnoe okno (cH. pnc. 2.1).
O1ae1c1aennoc1L sa ero o1o6paxenne npnnncana koHnonen1e, nasaanno Greeter.
Heko1opmH, noka eue ne onpepenennmH o6pasoH (c noHouLk acnnmaakunx Henk, knonok,
naxa1nn na knaanun knaana1ypm nnn ncnonLsoaannn cencopnoro akpana) nonLsoaa1enL
am6npae1 opno ns neckonLknx pec1an.
HepaonauanLno nnannpye1cn 1onLko nn1L pec1an:
1. HpocHo1pe1L 6asm pannmx c peuen1aHn, no 6es ccmnok na kako-1o konkpe1nm
nnan nn1annn.
2. [o6aan1L noam peuen1 a 6asy pannmx.
3. Pepak1npoaa1L nnn po6aan1L koHHen1apnn k cyuec1aykueHy peuen1y.
4. HepecHo1pe1L cyuec1aykun nnan a o1nouennn neko1opmx npopyk1oa.
5. Cospa1L noam nnan nn1annn.
D1n pec1ann ec1ec1aennmH o6pasoH pas6naak1cn na pae rpynnm. Hepame 1pn pec1ann
cansanm c 6aso pannmx peuen1oa, nocnepnne paa c nnannpoaanneH nn1annn. B
pesynL1a1e koHanpa npnnnHae1 cnepykuee peuenne: cospa1L koHnonen1m,
coo1ae1c1aykune a1nH payH o6nsannoc1nH. Hpoponxan npopa6a1maa1L cuenapn,
nnannpoaanne nn1annn na apeHn nrnopnpyeH n nepexopnH k y1ounennk pec1an,
cansannmx c koHnonen1o Recipe Database. Ha pnc. 2.2 nokasan
converted to PDF by BoJIoc

Puc. 2.2. CRC-kap1ouka pnn knacca sac1aakn Greeter
nepaonauanLnm anp CRC-kap1oukn pnn koHnonen1m Greeter.
B unpokoH cHmcne o6nsannoc1L koHnonen1m, pa6o1akue c 6aso pannmx, npoc1o
noppepxnaa1L sanncn c peuen1aHn.
Mm yxe ampennnn 1pn acnek1a a1o koHnonen1m: Recipe Database ponxna o6ecneunaa1L
npocHo1p 6n6nno1ekn cyuec1aykunx peuen1oa, pepak1npoaanne peuen1oa, aknkuenne
noamx peuen1oa a 6asy pannmx.
2.6.1. O1noxeuume pemeuun
B konue konuoa npnpe1cn peun1L, kak nonLsoaa1enL c1ane1 npocHa1pnaa1L 6asy pannmx.
HanpnHep, ponxen nn on cnauana axopn1L a cnncok ka1eropn 1aknx, kak cynm,
cana1m, ropnune 6nkpa, pecep1m?
C ppyro c1oponm, Hoxe1 nn nonLsoaa1enL sapaaa1L knkueame cnoaa pnn orpannuennn
o6nac1n noncka, aknkuan cnncok nnrpepnen1oa (HnnpanL, kny6nnka, cmp)? Hnn xe
ncnonLsoaa1L cnncok npepaapn1enLno sapannmx knkueamx cnoa (nk6nHme nnpoxnme
Bo6a)? Cnepye1 nn npnHenn1L nonocm npokpy1kn (scroll bars) nnn nHn1npoaa1L saknapkn a
anp1yanLno knnxke? PasHmunn1L o6 a1nx npepHe1ax poc1aanne1 ypoaonLc1ane, no aaxno
1o, u1o ne1 neo6xopnHoc1n npnnnHa1L konkpe1nme peuennn na pannoH a1ane
npoek1npoaannn (cH. paspen 2.6.2. Io1oanoc1L k nsHenennnH). HockonLky onn annnk1
1onLko na o1penLnyk koHnonen1y n ne sa1parnaak1 qynkunonnpoaanne oc1anLnmx uac1e
cnc1eHm, 1o ace, u1o napo pnn npoponxennn pa6o1m nap cuenapneH, a1o nnqopHaunn o
1oH, u1o nonLsoaa1enL Hoxe1 am6pa1L konkpe1nm peuen1.
2.6.2. Fo1oauoc1u k usHeueuunH
Kak 6mno ckasano, epnnc1aennoe, u1o nanne1cn noc1onnnmH a xnsnn, nens6exnoc1L
nsHenenn. To xe caHoe cnpaaepnnao pnn nporpaHHnoro o6ecneuennn. HesaancnHo o1 1oro
kak 1ua1enLno am nm1ae1ecL paspa6o1a1L ncxopnme cneunqnkaunn n npoek1 nporpaHHno
cnc1eHm, nou1n naaepnnka nsHenennn a xenannnx nnn no1pe6noc1nx nonLsoaa1enn 6ypy1
amsmaa1L coo1ae1c1aykune ncnpaanennn a nporpaHHe (sauac1yk a 1euenne acero
xnsnennoro unkna cnc1eHm). Paspa6o1unkn ponxnm npepanpe1L a1o n nnannpoaa1L caon
pec1ann coo1ae1c1aykunH o6pasoH:
converted to PDF by BoJIoc
Inaanan uenL coc1on1 a 1oH, u1o nsHenennn ponxnm sa1parnaa1L kak Hoxno HenLue
koHnonen1. [axe npnnunnnanLnme noauec1aa ao aneuneH anpe nnn
qynkunonnpoaannn npnnoxennn ponxnm sa1pony1L opnn nnn paa qparHen1a kopa.
C1apa1ecL npepckasa1L nan6onee aepon1nme nc1ounnkn nsHenenn n nosaonL1e nH
annn1L na aosHoxno HenLuee konnuec1ao koHnonen1 nporpaHHm. Han6onee o6unHn
npnunnaHn nsHenenn nannk1cn nn1epqec, qopHa1m o6Hena nnqopHaune, anp
amxopnmx pannmx.
C1apa1ecL nsonnpoaa1L n yHenLun1L saancnHoc1L nporpaHHnoro o6ecneuennn o1
annapa1ypm. HanpnHep, nn1epqec npocHo1pa peuen1oa a aaueH npnnoxennn
Hoxe1 uac1nuno saance1L o1 annapa1noro o6ecneuennn cnc1eHm, na ko1opo
pa6o1ae1 nporpaHHa. Hocnepykune aepcnn ponxnm nepenocn1Lcn na pasnnunme
nna1qopHm. Xopoun npoek1 ponxen npepanpe1L nopo6noe nsHenenne.
YHenLuenne konnuec1aa canse Hexpy qparHen1aHn nporpaHHm cnnxae1
asanHosaancnHoc1L Hexpy nnHn n yaennunaae1 aepon1noc1L 1oro, u1o kaxpyk
koHnonen1y ypac1cn nsHenn1L c HnnnHanLnmH aospec1aneH na ppyrne.
Akkypa1no sanocn1e sanncn o npouecce paspa6o1ke n o pnckyccnnx, npoaopnaunxcn
aokpyr npnnunnnanLnmx peuenn, a npoek1nyk pokyHen1aunk. Hou1n naaepnnka
konnek1na, o1aeuakun sa conpoaoxpenne cnc1eHm n paspa6o1ky cnepykunx
aepcn, 6ype1 o1nnua1Lcn o1 koHanpm, paspa6o1aaue nepaonauanLnyk aepcnk
nporpaHHm. Hpoek1nan pokyHen1aunn nosaonn1 a nocnepc1ann ysna1L o Ho1naax
npnnn1mx peuenn n noHoxe1 ns6exa1L sa1pa1 apeHenn na o6cyxpenne aonpocoa,
ko1opme yxe 6mnn paspeuenm.
2.6.3. Hpoponxeuue pa6o1m co ceuapueH
Kaxpm kynnnapnm peuen1 6ype1 npen1nqnunpoaa1Lcn c konkpe1no nporpaHHno
koHnonen1o. Ecnn peuen1 am6pan nonLsoaa1eneH, ynpaanenne nepepae1cn o6Lek1y,
accounnpoaannoHy c peuen1oH. Peuen1 ponxen copepxa1L onpepenennyk nnqopHaunk. B
ocnoanoH ona coc1on1 ns cnncka nnrpepnen1oa n pec1an, neo6xopnHmx pnn
1pancqopHaunn coc1aannkunx a koneunm npopyk1. Cornacno naueHy cuenapnk
koHnonen1a-peuen1 ponxna 1akxe amnonnn1L n ppyrne pec1ann. HanpnHep, ona 6ype1
o1o6paxa1L peuen1 na akpane. HonLsoaa1enL nonyun1 aosHoxnoc1L cna6xa1L peuen1
anno1aune, a 1akxe Henn1L cnncok nnrpepnen1oa nnn na6op nnc1pykun. C ppyro
c1oponm, nonLsoaa1enL Hoxe1 no1pe6oaa1L pacneua1a1L peuen1. Bce a1n pec1ann nannk1cn
o6nsannoc1Lk koHnonen1m Recipe. (BpeHenno Hm npoponxnH onncanne Recipe kak o1penLno
asn1oro o6Lek1a. Ha a1ane npoek1npoaannn Hm HoxeH paccHa1pnaa1L ero kak npo1o1nn
Hnorouncnennmx o6Lek1oa-peuen1oa. Hospnee Hm aepneHcn k o6cyxpennk anL1epna1nam
opnnounan koHnonen1a Hnoxec1ao koHnonen1.)
Onpepenna auepne, kak ocyuec1an1L npocHo1p 6asm pannmx, aepneHcn k ee 6noky
ynpaanennn n npepnonoxnH, u1o nonLsoaa1enL xoue1 po6aan1L noam peuen1. B 6noke
ynpaanennn 6aso pannmx neknH o6pasoH onpepenne1cn, a kako paspen noHec1n1L noam
peuen1 (a nac1onuee apeHn pe1ann nac ne nn1epecyk1), sanpaunaae1cn nHn peuen1a n
amaopn1cn okno pnn na6opa 1ekc1a. TaknH o6pasoH, a1y sapauy ec1ec1aenno o1nec1n k 1o
koHnonen1e, ko1opan o1aeuae1 sa pepak1npoaanne peuen1oa.
BepneHcn k 6noky Greeter. Hnannpoaanne Henk, kak am noHnn1e, 6mno nopyueno
nporpaHHno koHnonen1e Plan Manager. HonLsoaa1enL ponxen nHe1L aosHoxnoc1L
coxpann1L cyuec1aykun nnan. Cnepoaa1enLno, koHnonen1a Plan Manager Hoxe1
sanycka1Lcn nn6o a pesynL1a1e o1kpm1nn yxe cyuec1aykuero nnana, nn6o npn cospannn
noaoro. B nocnepneH cnyuae nonLsoaa1enn neo6xopnHo nonpocn1L aaec1n apeHennme
nn1epaanm (cnncok pa1) pnn noaoro nnana. Kaxpan pa1a accounnpye1cn c o1penLno
koHnonen1o 1nna Date. HonLsoaa1enL Hoxe1 am6pa1L konkpe1nyk pa1y pnn pe1anLnoro
nccnepoaannn a a1oH cnyuae ynpaanenne nepepae1cn coo1ae1c1aykue koHnonen1e Date.
KoHnonen1a Plan Manager ponxna yHe1L pacneua1maa1L Henk nn1annn na nnannpyeHm
converted to PDF by BoJIoc
nepnop. Hakoneu, nonLsoaa1enL Hoxe1 nonpocn1L koHnonen1y Plan Manager crenepnpoaa1L
cnncok npopyk1oa na ykasannm nepnop.
B koHnonen1e Date xpann1cn cnepykune pannme: cnncok 6nkp na coo1ae1c1aykun penL n
(neo6nsa1enLnme) 1ekc1oame koHHen1apnn, po6aanennme nonLsoaa1eneH (nnqopHaunn o
pnnx poxpennn, k6nnenme pa1m, nanoHnnannn n 1. p.). 1o ponxna yHe1L koHnonen1a?
Hpexpe acero amaopn1L na akpan amuenepeuncnennme pannme. KpoHe 1oro, a ne ponxna
6m1L npepycHo1pena qynkunn neua1n. B cnyuae xenannn nonLsoaa1enn 6onee pe1anLno
osnakoHn1Lcn c 1eH nnn nnmH 6nkpoH, cnepye1 nepepa1L ynpaanenne koHnonen1e Meal.
B koHnonen1e Meal xpann1cn noppo6nan nnqopHaunn o 6nkpe. He ncknkueno, u1o y
nonLsoaa1enn okaxe1cn neckonLko peuen1oa opnoro 6nkpa. Hoa1oHy neo6xopnHo po6aann1L
n ypann1L peuen1m. KpoHe 1oro, xena1enLno nHe1L aosHoxnoc1L pacneua1a1L nnqopHaunk
o 1oH nnn nnoH 6nkpe. PasyHee1cn, ponxen 6m1L o6ecneuen amaop na akpan.
HonLsoaa1enk, aepon1nee acero, saxoue1cn o6pa1n1Lcn k eue kaknH-nn6ypL peuen1aH
cnepoaa1enLno, neo6xopnHo nanapn1L kon1ak1 c 6aso pannmx peuen1oa. Pas 1ak,
koHnonen1m Meal n 6asa pannmx ponxnm asanHopec1aoaa1L Hexpy co6o.
[anee koHanpa paspa6o1unkoa npoponxae1 nccnepoaa1L ace aosHoxnme cuenapnn.
Heo6xopnHo npepycHo1pe1L o6pa6o1ky ncknkun1enLnmx cn1yaun. HanpnHep, u1o
nponcxopn1, ecnn nonLsoaa1enL sapae1 knkueaoe cnoao pnn noncka peuen1a, a nopxopnun
peuen1 ne napen? Kak nonLsoaa1enL cHoxe1 npepaa1L pec1ane (nanpnHep, aaop noaoro
peuen1a), ecnn on ne xoue1 npoponxa1L panLue? Bce a1o ponxno 6m1L nsyueno.
O1ae1c1aennoc1L sa o6pa6o1ky nopo6nmx cn1yaun cnepye1 pacnpepenn1L Hexpy
koHnonen1aHn.
Hsyuna pasnnunme cuenapnn, koHanpa paspa6o1unkoa a koneunoH cue1e peuae1, u1o ace
pec1ann Hory1 6m1L napnexaunH o6pasoH pacnpepenenm Hexpy uec1Lk koHnonen1aHn
(pnc. 2.3). KoHnonen1a Greeter asanHopec1aye1 1onLko c Plan Manager n Recipe Database.
KoHnonen1a Plan Manager sauennne1cn 1onLko c Date, a 1a a caok ouepepL c Meal.
KoHnonen1a Meal o6pauae1cn k Recipe Manager n uepes nocpepc1ao a1oro o6Lek1a k
konkpe1nmH peuen1aH.

Puc. 2.3. BsanHocansL Hexpy koHnonen1aHn nporpaHHm IIKH
2.6.4. QuarpaHHm asauHopec1aun
CxeHa, nso6paxennan na pnc. 2.3, xopouo nopxopn1 pnn o1o6paxennn c1a1nuecknx canse
Hexpy koHnonen1aHn. Ho ona ne ropn1cn pnn onncannn pnnaHnueckoro asanHopec1ann ao
apeHn amnonnennn nporpaHHm. [nn a1nx uene ncnonLsyk1cn pnarpaHHm asanHopec1ann.
Ha pnc. 2.4 nokasana uac1L pnarpaHHm asanHopec1ann pnn nporpaHHm IIKH. BpeHn
panxe1cn caepxy anns. Kaxpan koHnonen1a npepc1aanena aep1nkanLno nnnne.
Coo6uenne o1 opno koHnonen1m k ppyro nso6paxae1cn ropnson1anLno c1penko Hexpy
aep1nkanLnmHn nnnnnHn. Bosapa1 ynpaanennn (n, aosHoxno, pesynL1a1a) a koHnonen1y
npepc1aanen ananornuno c1penko. Heko1opme aa1opm ncnonLsyk1 pnn a1o uenn
nynk1npnyk c1penky. KoHHen1apnn cnpaaa o1 pncynka 6onee noppo6no o6Lncnnk1
asanHopec1ane.
converted to PDF by BoJIoc
Bnaropapn nannunk ocn apeHenn pnarpaHHa asanHopec1ann nyuue onncmaae1
nocnepoaa1enLnoc1L co6m1n a npouecce pa6o1m nporpaHHm. Hoa1oHy pnarpaHHm
asanHopec1ann nannk1cn nonesnmH cpepc1aoH pokyHen1npoaannn pnn cnoxnmx
nporpaHHnmx cnc1eH.

Puc. 2.4. HpnHep pnarpaHHm asanHopec1ann
2.7. KoHnoueu1m nporpaHHm
B a1oH paspene Hm nccnepyeH koHnonen1m nporpaHHm 6onee noppo6no. 3a a1nH aneune
npoc1mH nonn1neH npnue1cn Hnoro ne1pnananLnmx acnek1oa (u1o, anpoueH, cnpaaepnnao
nou1n pnn acex nonn1n sa ncknkuenneH coaceH aneHen1apnmx).
2.7.1. Hoaepeuue u coc1onuue
Mm yxe anpenn, u1o koHnonen1m xapak1epnsyk1cn caonH noaepenneH, 1o ec1L 1eH, u1o onn
ponxnm pena1L. Ho koHnonen1m 1akxe xpann1 onpepenennyk nnqopHaunk. BosLHeH, k
npnHepy, koHnonen1y-npo1o1nn Recipe ns nporpaHHm IIKH. Moxno npepc1aan1L ee ce6e kak
napy noaepennecoc1onnne.
Hoaepenne koHnonen1m a1o na6op pec1an, ek ocyuec1anneHmx. Honnoe
onncanne noaepennn koHnonen1m nnorpa nasmaak1 npo1okonoH. HanpnHep, a
npo1okone, noppepxnaaeHoH koHnonen1o Recipe, snaun1cn, u1o ona ocyuec1anne1
pepak1npoaanne nnc1pykun no npnro1oanennk 6nkp, o1o6paxae1 nx na akpane,
pacneua1maae1 peuen1m.
Ioaopn o coc1onnnn koHnonen1m, nHek1 a anpy ee any1pennee copepxanne. [nn
Recipe coc1onnne aknkuae1 a ce6n nnrpepnen1m n nnc1pykunn no npnro1oanennk
6nkp. Coc1onnne ne nanne1cn c1a1nuecknH n Hoxe1 nsHenn1Lcn c 1euenneH
apeHenn. HanpnHep, pepak1npyn 1ekc1, nonLsoaa1enL nsHenne1 coc1onnne peuen1a.
He ace koHnonen1m o6nsanm nHe1L coc1onnne. HanpnHep, y koHnonen1m Greeter, aepon1no,
ne 6ype1 any1pennnx pannmx, nockonLky e nn k ueHy noHnn1L kakyk-nn6o nnqopHaunk.
Opnako 6onLunnc1ao koHnonen1 xapak1epnsye1cn n noaepenneH, n coc1onnneH.
2.7.2. 3kseHnnnpm u knaccm
Paspenna nonn1nn o coc1onnnn n noaepennn, Hm HoxeH 1enepL sa1pony1L 1eHy, ko1opyk
panee ns6erann. Bepon1no, a peanLnoH npnnoxennn 6ype1 Hnoro peuen1oa. Opnako ace onn
6ypy1 aec1n ce6n opnnakoao. O1nnuae1cn 1onLko coc1onnne: cnncok nnrpepnen1oa n
nnc1pykun no npnro1oanennk. Ha pannnx c1apnnx paspa6o1kn nac ponxno nn1epecoaa1L
noaepenne, o6uee pnn acex peuen1oa. [e1ann, cneunqnueckne pnn o1penLnoro peuen1a, ne
aaxnm.
TepHnn knacc ncnonLsye1cn pnn onncannn Hnoxec1aa o6Lek1oa c noxoxnH noaepenneH. Mm
yanpnH a nocnepykunx rnaaax, u1o knacc npnHenne1cn kak cnn1akcnueckn HexannsH
nou1n ao acex o6Lek1no-opnen1npoaannmx nsmkax. Konkpe1nme npepc1aan1enn knacca
nasmaak1cn akseHnnnpaHn. 3aHe1nH, u1o noaepenne accounnpoaano c knaccoH, a ne c
nnpnanpyanLnmHn npepc1aan1ennHn. To ec1L ace akseHnnnpm knacca aocnpnnnHak1 opnn n
converted to PDF by BoJIoc
1e xe koHanpm n amnonnnk1 nx cxopnmH cnoco6oH. C ppyro c1oponm, coc1onnne nanne1cn
nnpnanpyanLnmH. Mm anpnH a1o na npnHepe pasnnunmx akseHnnnpoa knacca Recipe. Bce
onn Hory1 amnonnn1L opnn n 1e xe pec1ann (pepak1npoaanne, amaop na akpan, neua1L), no
ncnonLsyk1 pasnnunme pannme. Mm paccHo1pnH konuenunk knacca 6onee noppo6no a
rnaae 3.
2.7.3. 3aenneuue u cansuoc1u
[ayHn aaxnmHn nonn1nnHn npn paspa6o1ke nporpaHH nannk1cn sauennenne (coupling) n
cansnoc1L (cohesion). Cansnoc1L a1o Hepa 1oro, nackonLko o1penLnan koHnonen1a
o6pasye1 nornueckn sakonuennyk, ocHmcnennyk epnnnuy. Bmcokan cansnoc1L poc1nrae1cn
o6LepnnenneH a opno koHnonen1e coo1nocnunxcn (a 1oH nnn nnoH cHmcne) ppyr c ppyroH
qynkun. Han6onee uac1o qynkunn okasmaak1cn cansannmHn ppyr c ppyroH npn
neo6xopnHoc1n nHe1L poc1yn k o6unH pannmH. HHenno a1o o6Lepnnne1 pasnme uac1n
koHnonen1m Recipe.
C ppyro c1oponm, sauennenne xapak1epnsye1 asanHosaancnHoc1L Hexpy koHnonen1aHn
nporpaHHm. B o6ueH cnyuae xena1enLno yHenLun1L c1enenL sauennennn kak 1onLko
aosHoxno, nockonLky cansn Hexpy koHnonen1aHn nporpaHHm npenn1c1ayk1 nx
Hopnqnkaunn n Heuak1 panLneue paspa6o1ke nnn noa1opnoHy ncnonLsoaannk a ppyrnx
nporpaHHax.
B uac1noc1n, sauennenne aosnnkae1, ecnn opna nporpaHHnan koHnonen1a ponxna nHe1L
poc1yn k pannmH (coc1onnnk) ppyro koHnonen1m. Cnepye1 ns6era1L nopo6nmx cn1yaun.
Bosnoxn1e o6nsannoc1L ocyuec1ann1L poc1yn k pannmH na koHnonen1y, ko1opan nHn
anapee1. HanpnHep, a cnyuae c naunH npoek1oH kaxe1cn, u1o o1ae1c1aennoc1L sa
pepak1npoaanne peuen1oa ponxna nexa1L na koHnonen1e Recipe Database, nockonLky
nHenno a ne anepame aosnnkae1 a a1oH neo6xopnHoc1L. Ho 1orpa o6Lek1 Recipe Database
ponxen nanpnHyk Hannnynnpoaa1L coc1onnneH o1penLnmx peuen1oa (nx any1pennnHn
pannmHn: cnnckoH nnrpepnen1oa n nnc1pykunnHn no npnro1oanennk). yuue ns6exa1L
c1onL 1ecnoro sauennennn, nepepaa o6nsannoc1L pepak1npoaannn nenocpepc1aenno
peuen1y.
Bonee noppo6no o cansnoc1n n sauennennn, a 1akxe o coo1ae1c1aykue 1exnnke
nporpaHHnpoaannn pacckasmaae1cn a rnaae 17.
2.7.4. Hu1epqec u peanusaun Hopynn npuuunm
Hapuaca
Hpen xapak1epnsaunn koHnonen1 nporpaHHm uepes nx noaepenne nHee1 opno upesamuano
aaxnoe cnepc1ane. HporpaHHnc1 snae1, kak ncnonLsoaa1L koHnonen1y, paspa6o1annyk
ppyrnH nporpaHHnc1oH, n npn a1oH eHy ne1 neo6xopnHoc1n sna1L, kak ona peannsoaana.
HanpnHep npepnonoxnH, u1o uec1L koHnonen1 npnnoxennn IIKH cospak1cn uec1Lk
nporpaHHnc1aHn. HporpaHHnc1, paspa6a1maakun koHnonen1y Meal, ponxen o6ecneun1L
npocHo1p 6asm pannmx c peuen1aHn n am6op o1penLnoro peuen1a npn coc1aanennn 6nkpa.
[nn a1oro koHnonen1a Meal npoc1o amsmaae1 qynkunk browse, npnansannyk k koHnonen1e
Recipe Database. Oynkunn browse aosapauae1 o1penLnm peuen1 Recipe ns 6asm pannmx.
Bce a1o cnpaaepnnao ane saancnHoc1n o1 1oro, kak konkpe1no peannsoaan any1pn Recipe
Database npocHo1p 6asm pannmx.
Mm npnueH noppo6noc1n peannsaunn sa qacapoH nn1epqeca. Hponcxopn1 Hacknpoaka
nnqopHaunn. Ioaopn1, u1o koHnonen1a nnkancynnpye1 noaepenne, ecnn ona yHee1
amnonnn1L neko1opme pec1ann, no noppo6noc1n, kak nHenno a1o penae1cn, oc1ak1cn
ckpm1mHn. D1o ec1ec1aennmH o6pasoH npnaopn1 k payH pasnnunmH npepc1aanennnH o
nporpaHHno cnc1eHe. Bnp co c1oponm nn1epqeca a1o nnueaan c1opona; ee anpn1
ppyrne nporpaHHnc1m. B nn1epqecno uac1n onncmaae1cn, u1o yHee1 pena1L koHnonen1a.
converted to PDF by BoJIoc
Bnp co c1oponm peannsaunn a1o nsnanka, anpnHan 1onLko 1eH, k1o pa6o1ae1 nap
konkpe1no koHnonen1o. 3pecL onpepenne1cn, kak koHnonen1a amnonnne1 sapanne.
Paspenenne nn1epqeca n peannsaunn nanne1cn, aosHoxno, nan6onee aaxno npee a
nporpaHHnpoaannn. Ee nenpoc1o o6Lncnn1L c1ypen1aH. Macknpoaka nnqopHaunn nHee1
snauenne a ocnoanoH 1onLko a kon1ekc1e nporpaHHnmx npoek1oa, a ko1opmx sann1o Hnoro
nkpe. Hpn 1aknx pa6o1ax nnHn1npykunH qak1opoH uac1o nanne1cn ne konnuec1ao
npnaneuennmx nkpe, a uac1o1a o6Hena nnqopHaune n pannmHn kak Hexpy
nporpaHHnc1aHn, 1ak n Hexpy paspa6a1maaeHmHn nHn nporpaHHnmHn cnc1eHaHn. Kak
6ype1 nokasano nnxe, koHnonen1m uac1o paspa6a1maak1cn napannenLno pasnmHn
nporpaHHnc1aHn a nsonnunn ppyr o1 ppyra.
Hn1epec k Hnorokpa1noHy ncnonLsoaannk nporpaHHnmx koHnonen1 o6uero nasnauennn a
pasnmx npoek1ax aospac1ae1. [nn ocyuec1anennn nopo6noro, cansn Hexpy pasnnunmHn
uac1nHn cnc1eHm ponxnm 6m1L HnnnHanLnm n npospaunm.
Kak Hm yxe o1Heuann a npepmpyue rnaae, a1n npen 6mnn cqopHynnpoaanm cneunannc1oH
no nnqopHa1nke [aanpoH HapnacoH a anpe npaann, uac1o nasmaaeHmx npnnunnaHn
Hapnaca:
Paspa6o1unk nporpaHHm ponxen npepoc1aan1L nonLsoaa1enk ack nnqopHaunk,
ko1opan nyxna pnn aqqek1nanoro ncnonLsoaannn npnnoxennn, n nnuero kpoHe
a1oro.
Paspa6o1unk nporpaHHnoro o6ecneuennn ponxen sna1L 1onLko 1pe6yeHoe
noaepenne koHnonen1m n nnuero kpoHe a1oro.
Cnepc1ane npnnunna o1penennn nn1epqeca o1 peannsaunn coc1on1 a 1oH, u1o nporpaHHnc1
Hoxe1 akcnepnHen1npoaa1L c pasnnunmHn anropn1HaHn, ne sa1parnaan oc1anLnme
koHnonen1m nporpaHHm.
2.8. 4opHanusaun uu1epqeca
HpoponxnH paspa6o1ky nporpaHHm IIKH. Ha cnepykunx neckonLknx a1anax y1ounne1cn
onncanne koHnonen1. Cnauana qopHannsyk1cn cnoco6m asanHopec1ann.
Cnepye1 onpepenn1L, kak 6ype1 peannsoaana kaxpan ns koHnonen1. KoHnonen1a,
xapak1epnsyeHan 1onLko noaepenneH (ne nHekuan any1pennero coc1onnnn), Hoxe1 6m1L
oqopHnena a anpe qynkunn. HanpnHep, koHnonen1y, saHennkuyk ace sarnaanme 6ykam a
1ekc1oao c1poke na c1pounme, pasyHnee acero cpena1L nHenno 1ak. KoHnonen1m c HnornHn
qynkunnHn nyuue peannsoaa1L a anpe knaccoa. Kaxpo o6nsannoc1n, nepeuncnenno na
CRC-kap1ouke koHnonen1m, npncaanaae1cn nHn. D1n nHena c1any1 sa1eH nasaannnHn
qynkun nnn npouepyp. BHec1e c nHenaHn onpepennk1cn 1nnm apryHen1oa, nepepaaaeHmx
qynkunnH. 3a1eH onncmaae1cn (acn) nnqopHaunn, copepxauancn any1pn koHnonen1m. Ecnn
koHnonen1e 1pe6yk1cn nekne pannme pnn amnonnennn konkpe1noro sapannn, nx nc1ounnk
(apryHen1 qynkunn, rno6anLnan nnn any1pennnn nepeHennan) ponxen 6m1L nano onncan.
2.8.1. Bm6op uHeu
HHena, cansannme c pasnnunmHn pec1annHn, ponxnm 1ua1enLno nop6npa1Lcn. Lekcnnp
ckasan, u1o nepenHenoaanne ne Henne1 cy1n o6Lek1a
1
, no onpepenenno ne ace nHena
6ypy1 amsmaa1L a aoo6paxennn cnyua1enn opnnakoame Hmcnennme o6pasm.
Kak paano nsaec1no npaan1enLc1aennmH unnoannkaH, nencnme n ncnonLsyeHme a
nepenocnoH cHmcne nHena npnpak1 o1nyrnaakun anp paxe npoc1eunH pec1annH.
Bm6op ypo6nmx nHen neo6muano aaxen. Onn ponxnm 6m1L any1penne coaHec1nHm,
snaunHm, kopo1kn, copepxa1enLnm. ac1o snaun1enLnoe apeHn 1pa1n1cn na naxoxpenne
npaannLnoro na6opa nHen pnn amnonnneHmx sapann n o6Lek1oa. HannncL paneko ne
converted to PDF by BoJIoc
6ecnnopnmH n ne 6ecnonesnmH npoueccoH, napnexaun am6op nHen na panne c1apnn
npoek1npoaannn snaun1enLno ynpouae1 n o6neruae1 panLneuyk paspa6o1ky.
Bmnn npepnoxenm cnepykune nonoxennn o6uero xapak1epa, perynnpykune a1o1 npouecc
[Keller 1990]:
HcnonLsy1e nHena, ko1opme Hoxno nponsnec1n acnyx. Ocnoanoe npaanno: ecnn am
ne Hoxe1e rpoHko npoun1a1L nHn, sa6ypL1e o neH.
HpnHenn1e sarnaanme 6ykam nnn cnHaonm nopuepknaannn pnn 1oro, u1o6m
o1He1n1L nauano noaoro cnoaa a coc1aanoH nHenn: CardReader nnn Card_reader
aHec1o neun1aeHoro cardreader.
Tua1enLno npoaepn1e cokpauennn. Cokpauenne, ncnoe pnn opnoro uenoaeka,
Hoxe1 6m1L sarapounmH pnn ppyroro. O6osnauae1 nn nHn TermProcess nocnepnn
npouecc a uenouke (terminal process), nnn neu1o, u1o npekpauae1 amnonnenne
npouecca (terminate process), nnn xe npouecc, cansannm c 1epHnnanoH
koHnLk1epa?
Hs6era1e Hnorosnaunoc1n nHen. HHn empty pnn qynkunn o6osnauae1 nn ono
npoaepky 1oro, u1o neko1opm o6Lek1 nyc1, nnn xe ona ypanne1 ace snauennn ns
o6Lek1a (penae1 ero nyc1mH)?
He ncnonLsy1e unqpm a nHenax. Hx nerko cny1a1L c 6ykaaHn (0 kak O, 1 kak l, 2
kak Z, 5 kak S).
Hpncaanaa1e qynkunnH, ko1opme aosapauak1 nornueckne (6yneackne) snauennn,
1akne nHena, u1o6m 6mno ncno, kak nn1epnpe1npoaa1L true n false. HanpnHep,
PrinterIsReady ncno nokasmaae1, u1o snauenne true coo1ae1c1aye1 npnn1epy a
pa6oueH coc1onnnn, a 1o apeHn kak PrinterStatus nanne1cn ropaspo Henee 1ounmH.

1
1o snaun1 nHn? Posa naxne1 poso, xo1L poso nasoan ee, xo1L ne1.
PoHeo nop nk6mH nasaanLeH 6mn 6m 1eH aepxoH coaepuenc1a, kako on
ec1L. BnnLnH Lekcnnp, PoHeo n [xynLe11a, pec1ane II, cuena 2
(nep. Bopnca Hac1epnaka).
[a1e poporoc1onunH (c 1oukn spennn koHnLk1epnmx pecypcoa) n pepko
ncnonLsyeHmH onepaunnH ynnkanLnme, ue1ko ampenneHme nHena. Hpn 1akoH
nopxope yHenLuae1cn aepon1noc1L ncnonLsoaannn ne 1ex qynkun.
Kak 1onLko pnn acex pec1an am6panm nHena, CRC-kap1ouka pnn kaxpo koHnonen1m
nepenncmaae1cn sanoao c ykasanneH nHen qynkun n cnncka qopHanLnmx apryHen1oa.
HpnHep CRC-kap1oukn pnn koHnonen1m Date npnaepen na pnc. 2.5. 1o oc1anocL ne
yc1anoanennmH, 1ak a1o 1o, kak nHenno kaxpan koHnonen1a 6ype1 amnonnn1L ykasannme
pec1ann.
Heo6xopnHo eue pas npokpy1n1L cuenapn 6onee pe1anLno, u1o6m rapan1npoaa1L, u1o
ace pec1ann yu1enm n acn neo6xopnHan nnqopHaunn nHee1cn n poc1ynna pnn
coo1ae1c1aykunx koHnonen1.
converted to PDF by BoJIoc

Puc. 2.5. O6noanennan CRC-kap1ouka pnn koHnonen1m Date
2.9. Bm6op npepc1aaneuun pauumx
Ha pannoH a1ane, ecnn 1onLko a1o ne 6mno cpenano panLue, nponcxopn1 paspenenne
koHanpm paspa6o1unkoa na rpynnm, kaxpan ns ko1opmx o1aeuae1 sa konkpe1nme
koHnonen1m nporpaHHm. 3apaua 1enepL coc1on1 a nepexope o1 onncannn koHnonen1m k
konkpe1noHy kopy. Inaanoe specL npoek1npoaanne c1pyk1yp pannmx, ko1opme 6ypy1
ncnonLsoaa1Lcn kaxpo ns nopcnc1eH pnn xpanennn any1penne nnqopHaunn, neo6xopnHo
pnn amnonnennn npepnncannmx o6nsannoc1e.
HHenno na a1oH a1ane a nrpy ac1ynak1 knaccnueckne c1pyk1ypm pannmx, ncnonLsyeHme a
nnqopHa1nke. Bm6op c1pyk1ypm pannmx nanne1cn aaxnmH, uen1panLnmH HoHen1oH c 1oukn
spennn npoek1npoaannn. Ecnn npepc1aanenne pannmx am6pano npaannLno, 1o kop,
ncnonLsyeHm koHnonen1o npn amnonnennn ee o6nsannoc1e, c1anoan1cn nou1n
caHooueanpnmH.
C1pyk1ypm pannmx ponxnm 1ouno coo1ae1c1aoaa1L paccHa1pnaaeHo sapaue. HenpaannLnm
am6op c1pyk1ypm Hoxe1 npnaec1n k cnoxnmH n neaqqek1nanmH nporpaHHaH.
Ha a1oH xe a1ane onncanne noaepennn koHnonen1 ponxno 6m1L npeo6pasoaano a
anropn1Hm. Peannsoaannme qynkunn sa1eH conoc1aannk1cn c no1pe6noc1nHn koHnonen1m,
nannkuecn knnen1oH pannoro qparHen1a, u1o6m rapan1npoaa1L, u1o ace ee sanpocm
okasmaak1cn amnonnennmHn n u1o ace neo6xopnHme pnn ee pa6o1m pannme nannk1cn
poc1ynnmHn.
2.10. Peanusaun koHnoueu1
Korpa npoek1 a uenoH onpepenen n pas6n1 na nopcnc1eHm, cnepykunH uaroH nanne1cn
peannsaunn koHnonen1. Ecnn npepmpyune a1anm 6mnn amnonnenm koppek1no, kaxpan
o6nsannoc1L nnn noaepenne 6ypy1 kpa1ko oxapak1epnsoaanm. 3apaue pannoro a1ana
nanne1cn aonnouenne xenaeHmx pec1an na koHnLk1epnoH nsmke. B cnepykueH paspene
Hm onnueH neko1opme ns nan6onee 1nnnunmx aapnc1nuecknx nopxopoa, ncnonLsyeHmx c
a1o uenLk.
Ecnn a1o ne 6mno cpenano panee (nanpnHep, kak uac1L a1ana cneunqnkaunn ace cnc1eHm),
1o 1enepL Hoxno peun1L, kak 6ypy1 yc1poenm any1pennne pe1ann o1penLnmx koHnonen1. B
cnyuae nauero npnHepa na pannoH a1ane cnepye1 nopyHa1L, kak nonLsoaa1enL 6ype1
npocHa1pnaa1L 6asy pannmx peuen1oa.
Ho Hepe 1oro kak nporpaHHnme npoek1m c 6onLunH uncnoH paspa6o1unkoa c1anoan1cn
nopHo, ace pexe ac1peuae1cn cn1yaunn, korpa opnnepnnc1aennm nporpaHHnc1 o1aeuae1
sa ack cnc1eHy. Han6onee aaxnme pnn nporpaHHnc1a kauec1aa a1o cnoco6noc1L
nonnHa1L, kak o1penLnm qparHen1 kopa nopknkuae1cn k 6onee amcokoHy nporpaHHnoHy
ypoank, n xenanne pa6o1a1L coaHec1no c oc1anLnmHn unenaHn koHanpm.
ac1o a npouecce peannsaunn opno koHnonen1m c1anoan1cn ncno, u1o neko1opan
nnqopHaunn nnn pec1ann ponxnm 6m1L npncaoenm coaceH ppyro koHnonen1e, ko1opan
converted to PDF by BoJIoc
pa6o1ana 6m sa cueno, ne anpnHo pnn nonLsoaa1enn. Takne koHnonen1m nnorpa
nasmaak1 cyqnepaHn. Mm ac1pe1nH coo1ae1c1aykune npnHepm a neko1opmx nocnepykunx
rnaaax.
Baxno uac1Lk anannsa n kopnpoaannn na a1oH a1ane nanne1cn nonnan xapak1epnsaunn n
pokyHen1npoaanne neo6xopnHmx npepaapn1enLnmx ycnoan, ko1opme 1pe6yk1cn
nporpaHHno koHnonen1e pnn amnonnennn sapannn. Takxe cnepye1 npoaepn1L, npaannLno
nn pa6o1ae1 nporpaHHnan koHnonen1a, ecnn amsaa1L ee c npaannLnmHn axopnmHn
snauennnHn. D1o nop1aeppn1 koppek1noc1L anropn1Hoa, ncnonLsoaannmx npn peannsaunn
koHnonen1m.
2.11. Hu1erpaun koHnoueu1
Korpa nnpnanpyanLnme nopcnc1eHm paspa6o1anm n npo1ec1npoaanm, onn ponxnm 6m1L
nn1erpnpoaanm a koneunm npopyk1. D1o penae1cn noa1anno. Haunnan c aneHen1apno
ocnoam, k cnc1eHe noc1enenno po6aannk1cn (n 1ec1npyk1cn) noame aneHen1m. Hpn a1oH
pnn eue ne peannsoaannmx uac1e ncnonLsyk1cn 1ak nasmaaeHme sarnyukn (stubs)
nopnporpaHHm 6es kakoro-nn6o noaepennn nnn c orpannuenno qynkunonanLnoc1Lk.
HanpnHep, npn paspa6o1ke nporpaHHm IIKH 6mno 6m pasyHnmH naua1L nn1erpnpoaanne c
koHnonen1m Greeter. 1o6m npo1ec1npoaa1L ee a nsonnunn o1 oc1anLnmx 6nokoa
nporpaHHm, no1pe6yk1cn sarnyukn pnn ynpaannkuero kopa 6asm pannmx c peuen1aHn
Recipe Database n 6noka ynpaanennn nnannpoaanneH nn1annn Meal Plan. 3arnyukn npoc1o
ponxnm ampaaa1L nnqopHaunonnme coo6uennn n aosapaua1L ynpaanenne. TaknH o6pasoH,
koHanpa paspa6o1unkoa koHnonen1m Greeter cHoxe1 npo1ec1npoaa1L pasnnunme acnek1m
panno koHnonen1m (nanpnHep, npoaepn1L, amsmaae1 nn naxa1ne knaanun nyxnyk
peakunk). O1napky o1penLnmx koHnonen1 uac1o nasmaak1 1ec1npoaanneH 6nokoa.
3a1eH sarnyukn saHennk1cn 6onee cepLesnmH kopoH. HanpnHep, aHec1o sarnyukn pnn
koHnonen1m Recipe Database Hoxno ac1aan1L peanLnyk nopcnc1eHy, coxpanna sarnyukn
pnn oc1anLnmx qparHen1oa. Tec1npoaanne npoponxae1cn po 1ex nop, noka ne c1ane1 ncno,
u1o cnc1eHa pa6o1ae1 npaannLno. D1o1 npouecc nasmaak1 1ec1npoaanneH cnc1eHm a uenoH.
Korpa ace sarnyukn saHenenm pa6o1akunHn koHnonen1aHn, npnnoxenne saaepueno.
Hpouecc 1ec1npoaannn saHe1no o6neruae1cn, ecnn uncno canse Hexpy koHnonen1aHn
neaennko a a1oH cnyuae ne npnpe1cn nnca1L Hnoxec1ao nporpaHH-sarnyuek.
Bo apeHn nn1erpaunn cnc1eHm anonne aosHoxno, u1o oun6ka, nponannkuancn a opno ns
nporpaHHnmx cnc1eH, amsaana nekoppek1nmH kopoH a ppyroH qparHen1e. TeH caHmH
oun6kn, amnanneHme a npouecce nn1erpaunn, npnaopn1 k neo6xopnHoc1n ncnpaann1L
neko1opme koHnonen1m. Bcnep sa a1nH nsHenennme koHnonen1m ponxnm anoaL
1ec1npoaa1Lcn nsonnpoaanno nepep ouepepno nonm1ko nn1erpaunn. Hoa1opnan nporonka
paspa6o1annmx panee 1ec1oamx npnHepoa, amnonnneHan nocne nsHenenn a koHnonen1e
nporpaHHm, nnorpa nasmaae1cn perpeccnonnmH 1ec1npoaanneH (regression testing).
2.12. Conpoaoxpeuue u pasau1ue
Xo1enocL 6m, u1o6m c nepepaue nonLsoaa1enk qynkunonnpykuero npnnoxennn, pa6o1a
koHanpm paspa6o1unkoa saaepuanacL. K coxanennk, 1akoro nou1n nnkorpa ne nponcxopn1.
Heo6xopnHo pononnn1enLnoe conpoaoxpenne nporpaHHnoro o6ecneuennn. Bo1 neko1opme
npnunnm, amsmaakune ero nens6exnoc1L:
B nepepannoH npopyk1e Hory1 6m1L o6napyxenm oun6kn. Onn ponxnm 6m1L
ncnpaanenm nn6o uepes sanna1kn (patches) k cyuec1aykue aepcnn, nn6o a
noao aepcnn.
HsHenenne 1pe6oaann aosHoxno, ns-sa noamx rocypapc1aennmx noc1anoanenn
nnn c1anpap1nsaunn.
converted to PDF by BoJIoc
Hepexop na ppyroe annapa1noe o6ecneuenne. HanpnHep, a pesynL1a1e nepenoca
cnc1eHm na ppyrne nna1qopHm nnn noppepxkn noamx yc1poc1aa aaopa (cae1oaoe
nepo nnn cencopnm akpan). Moxe1 nsHenn1Lcn 1exnonornn amaopa: ckaxeH, am
nepeunn o1 1ekc1oaoro nn1epqeca k rpaqnueckoHy.
HsHenenne sanpocoa nonLsoaa1ene. HonLsoaa1enn Hory1 1pe6oaa1L yaennuennn
aosHoxnoc1e nporpaHHm, cnnxennn uenm, 6onee npoc1oro ncnonLsoaannn n 1. p.
Kak npaanno, 1akne noamuennme 1pe6oaannn pnk1yk1 konkypnpykune
npopyk1m.
Ho1pe6noc1L a ynyuuenno pokyHen1aunn.
Xopoun npoek1 npepycHa1pnaae1 nens6exnoc1L nsHenenn n nopro1aannaae1 nx c caHoro
nauana.
Ynpaxueuun
1. Onnun1e pacnpepenenne o6nsannoc1e a oprannsaunn, ko1opan aknkuae1 no
kpane Hepe uec1L unenoa. PaccHo1pn1e yue6noe saaepenne (c1ypen1m,
npenopaaa1enn, pnpek1op, rappepo6unk), qnpHy (coae1 pnpek1opoa, npesnpen1,
pa6oun) n kny6 (npesnpen1, anue-npesnpen1, pnpoao unen). Onnun1e
o6nsannoc1n kaxporo unena oprannsaunn n ero co1pypnnkoa (ecnn onn ec1L).
2. Cospa1e c noHouLk pnarpaHHm asanHopec1ann cuenapn pnn oprannsaunn ns
ynpaxnennn 1.
3. [nn 1nnnuno kap1ouno nrpm onnun1e nporpaHHnyk cnc1eHy, ko1opan 6ype1
asanHopec1aoaa1L c nonLsoaa1eneH a kauec1ae npo1naononoxnoro nap1nepa.
Tnnnunme koHnonen1m ponxnm aknkua1L nrpoao c1on n konopy kap1.
4. Onnun1e nporpaHHnyk cnc1eHy pnn ynpaanennn ATM (Automatic Teller Machine).
HockonLky cnoao Teller poc1a1ouno Hnorosnauno (pacckasunk, cue1unk ronocoa npn
am6opax, kaccnp a 6anke, pnk1op papnonokaunonno c1anunn HBO n 1. p.), 1o y aac
nHee1cn 6onLuan cao6opa a am6ope npepnasnauennn a1o Haunnm. Hpnaepn1e
pnarpaHHm asanHopec1ann pnn pasnnunmx cuenapnea ncnonLsoaannn a1o
Haunnm.
Fnaaa 3

Knaccm u He1opm
Xo1n 1epHnnm, ko1opme ncnonLsyk1cn a o6Lek1no-opnen1npoaannmx nsmkax, o1nnuak1cn,
nonn1nn knaccoa, akseHnnnpoa, nepecmnkn coo6uenn, He1opoa n nacnepoaannn nannk1cn
o6unHn. D1n 1epHnnm 6mnn aaepenm a rnaae 1. Kak yxe o1HeuanocL, ncnonLsoaanne
pasnnunmx 1epHnnoa pnn opnnx n 1ex xe nonn1n unpoko pacnpoc1paneno a o6Lek1no-
opnen1npoaannmx nsmkax. Mm 6ypeH npnHenn1L epnnyk n, kak Hm napeeHcn, ncnyk
1epHnnonornk pnn acex nsmkoa nporpaHHnpoaannn. B aaopnmx paspenax 6ypeH o1Heua1L
cneunqnky konkpe1nmx nsmkoa n pasnnunme cnnonnHm pnn naunx 1epHnnoa. O6paua1ecL
k paspeny "Inoccapn" sa pasLncnenneH nesnakoHmx nonn1n.
B a1o rnaae peuL npe1 o c1a1nuecknx a1pn6y1ax knaccoa; a rnaae 4 Hm paccHo1pnH nx
pnnaHnueckoe ncnonLsoaanne. 3pecL xe Hm nponnnkc1pnpyeH HexannsHm o6Lnanennn
knacca n onpepenennn He1opoa. B rnaae 4 o6Lncnne1cn, kak cospak1cn akseHnnnpm knacca
n kak nH nepepak1cn coo6uennn. Mm o1noxnH ananns HexannsHoa nacnepoaannn po
rnaam 7.
converted to PDF by BoJIoc
Baxnm HoHen1, ko1opm neo6xopnHo nponcnn1L, pasnnune Hexpy o6LnanenneH knacca
nnn o6Lek1a n ero nopoxpenneH. B nepaoH cnyuae (ko1opm nanne1cn 1eHo a1o rnaam)
npoc1o ykasmaae1cn 1nn pannmx. O6Lnanenne xapak1epnsye1 o6Lek1 (any1pennne
nepeHennme, 1nnm noaepennn), no caHo no ce6e ne cospae1 noamx pannmx. Hopoxpenne, 1o
ec1L cospanne noamx akseHnnnpoa knacca, Hoxe1 paccHa1pnaa1Lcn kak pasnoanpnoc1L
o6Lnanennn nepeHenno n 6ype1 nann1Lcn 1eHo cnepykue rnaam. Pasnnune Hexpy a1nHn
payHn nonn1nnHn o1uac1n Hacknpye1cn 1eH qak1oH, u1o a nsmkax c kon1poneH 1nnoa
pannmx (1aknx, kak C++) onpepenenne, nopoxpakuee nepeHennyk, amrnnpn1 1ak xe, kak
n unc1oe o6Lnanenne.
3.1. Hukancynnun
B rnaae1 Hm saHe1nnn, u1o o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne n, a
oco6ennoc1n, o6Lek1m Hory1 paccHa1pnaa1Lcn co Hnornx 1ouek spennn. B a1o rnaae Hm
6ypeH npepc1aann1L ce6e o6Lek1m kak a6c1pak1nme 1nnm pannmx.
B nporpaHHnpoaannn, ocnoaannoH na a6c1pakunn pannmx, nnqopHaunn cosna1enLno
npnue1cn a ne6onLuo uac1n nporpaHHm. B uac1noc1n, kaxpm o6Lek1 ns na6opa
a6c1pak1nmx 1nnoa pannmx, paspa6a1maaeHoro nporpaHHnc1oH, nHee1 paa "nnua". D1o
ananornuno pnxo1oHnn npnnunnoa Hapnaca, ko1opme o6cyxpanncL a rnaae 2. C aneune
1oukn spennn (knnen1a nnn nonLsoaa1enn) a6c1pak1nm 1nn pannmx npepc1aanne1 co6o
acero nnuL coaokynnoc1L onepaun, ko1opme onpepennk1 noaepenne a6c1pakun. C
npo1naononoxno c1oponm, sa qacapoH nn1epqeca, nporpaHHnc1, ko1opm onpepenne1
a6c1pak1nm 1nn, anpn1 snauennn nepeHennmx, ko1opme ncnonLsyk1cn pnn noppepxkn
any1pennero coc1onnnn o6Lek1a.
HanpnHep, pnn a6c1pak1noro 1nna pannmx stack nonLsoaa1enL anpn1 1onLko onncanne
ponyc1nHmx onepaun ckaxeH, push, pop, top. C ppyro c1oponm, nporpaHHnc1y,
peannsykueHy stack, neo6xopnHo Hannnynnpoaa1L c konkpe1nmHn c1pyk1ypaHn pannmx
(pnc. 3.1). Konkpe1nme pe1ann nnkancynnpoaanm a 6onee a6c1pak1nm o6Lek1.

Puc. 3.1. Hn1epqec n peannsaunn pnn 1nna stack
Mm ncnonLsoaann 1epHnn akseHnnnp pnn o6osnauennn npepc1aan1enn knacca.
Coo1ae1c1aenno Hm 6ypeH ncnonLsoaa1L 1epHnn nepeHennan akseHnnnpa pnn o6osnauennn
any1penne nepeHenno, copepxauecn a akseHnnnpe. Kaxpm akseHnnnp nHee1 caok
co6c1aennyk coaokynnoc1L nepeHennmx. D1n snauennn ne ponxnm nsHenn1Lcn knnen1aHn
nanpnHyk, a 1onLko c noHouLk He1opoa, accounnpoaannmx c knaccaHn.
O6Lek1 nanne1cn, 1aknH o6pasoH, koH6nnaune coc1onnnn n noaepennn. Coc1onnne
onncmaae1cn nepeHennmHn akseHnnnpa, a 1o apeHn kak noaepenne xapak1epnsye1cn
He1opaHn. Cnapyxn knnen1m Hory1 ysna1L 1onLko o noaepennn o6Lek1oa. Hsny1pn poc1ynna
nonnan nnqopHaunn o 1oH, kak He1opm o6ecneunaak1 neo6xopnHoe noaepenne, nsHennk1
coc1onnne n asanHopec1ayk1 c ppyrnHn o6Lek1aHn.
3.2. Pasuoaupuoc1u knaccoa
converted to PDF by BoJIoc
Knaccm a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn nHek1 neckonLko pasnnunmx
qopH n ncnonLsyk1cn pnn pasnmx uene. Cnepykune ka1eropnn oxaa1maak1 6onLuyk
uac1L knaccoa:
ynpaanenne pannmHn;
nc1ounnkn pannmx nnn nocpepnnkn a nepepaue pannmx;
knaccm pnn npocHo1pa pannmx;
acnoHora1enLnme, nnn ynpouakune npoek1npoaanne, knaccm.
D1o1 cnncok ne nanne1cn ncuepnmaakunH, opnako on anonne nopxopn1 pnn yue6nmx uene.
BonLunnc1ao o6Lek1no-opnen1npoaannmx npnnoxenn aknkuak1 kak knaccm
amuenepeuncnennmx ka1eropn, 1ak n ppyrne. Ecnn okasmaae1cn, u1o knacc "paspmaae1cn"
Hexpy payHn ka1eropnnHn, 1o sauac1yk ero Hoxno pas6n1L na paa knacca.
Knaccm-apHnnnc1pa1opm pannmx Data Managers, uac1o nonyuakune nHena Data nnn
State, a1o knaccm, ocnoano o6nsannoc1Lk ko1opmx nanne1cn noppepxka pannmx nnn
nnqopHaunn o coc1onnnn uero-nn6o. HanpnHep, pnn a6c1pak1no Hopenn nrpm a kap1m
ocnoanan sapaua knacca Card coc1on1 a 1oH, u1o6m xpann1L Hac1L n panr (poc1onnc1ao)
kap1m. Knaccm-apHnnnc1pa1opm pannmx o6muno nannk1cn qynpaHen1anLnmHn
c1pon1enLnmHn 6nokaHn npoek1a, a nx npo1o1nnaHn a cneunqnkaunn npoek1a nannk1cn
cyuec1an1enLnme.
Hc1ounnkn pannmx Data Sources a1o knaccm, ko1opme renepnpyk1 pannme (nanpnHep,
cnyuanme uncna). Hocpepnnkn npn nepepaue pannmx Data Sinks, ec1ec1aenno, cnyxa1 pnn
npneHa n panLneue nepepaun pannmx (nanpnHep, sanncL a qan). B o1nnune o1
apHnnnc1pa1opoa pannmx, nc1ounnkn n nocpepnnkn ne xpann1 any1pn ce6n pannme a
1euenne neonpepenennoro apeHenn, no renepnpyk1 nx no sanpocy (nc1ounnkn pannmx) nnn
o6pa6a1maak1 nx npn amsoae (nocpepnnkn pannmx).
Knaccm pnn npocHo1pa pannmx View n Observer 1akxe nesaHennHm npak1nueckn a nk6oH
npnnoxennn. Bce nporpaHHm 1ak nnn nnaue ocyuec1annk1 amaop nnqopHaunn (kak
npaanno, na akpan). Coo1ae1c1aykun nporpaHHnm kop nepepko nanne1cn cnoxnmH, uac1o
Hopnqnunpye1cn n a snaun1enLno c1enenn ne saancn1 o1 amaopnHmx pannmx. Hoa1oHy
xopounH 1onoH a nporpaHHnpoaannn cun1ae1cn nsonnunn any1pennnx pannmx o1 He1opoa,
ocyuec1annkunx amaop nnqopHaunn.
Honesno o1penn1L co6c1aenno o6Lek1 (nasmaaeHm uac1o HopenLk) o1 ero nso6paxennn
(ansyanLnoro npepc1aanennn). Bnaropapn a1oHy npnnunny cnc1eHm, o6ecneunaakune
rpaqnueckn amaop nnqopHaunn, a snaun1enLno c1enenn Hory1 6m1L ynpouenm. B
npeanLnoH cnyuae HopenL ne 1pe6ye1 n ne copepxn1 nnqopHaunn o caoeH ansyanLnoH
npepc1aanennn. D1o ynpouae1 Hnorokpa1noe ncnonLsoaanne kopa, nockonLky opna n 1a xe
HopenL Hoxe1 npnHenn1Lcn ao Hnornx npnnoxennnx. MopenL sauac1yk nHee1 6onee opnoro
ansyanLnoro npepc1aanennn. HanpnHep, qnnancoaan nnqopHaunn Hoxe1 6m1L
npepc1aanena a anpe rnc1orpaHH, kpyroamx pnarpaHH, 1a6nnu nnn pncynkoa. Hpn a1oH caHa
nnqopHaunn oc1ae1cn nensHenno.
K acnoHora1enLnmH knaccaH (Facilitator n Helper) pasyHno o1nec1n 1e knaccm, ko1opme ne
copepxa1 nonesno nnqopHaunn, no o6neruak1 amnonnenne cnoxnmx sapann. HanpnHep,
npn o1o6paxennn nrpanLno kap1m Hm ncnonLsyeH acnoHora1enLnm knacc, pncykun
nnnnn n 1ekc1 na yc1poc1ae amaopa. [pyro cnyxe6nm knacc Hoxe1, nanpnHep,
o6cnyxnaa1L cansnm cnncok kap1 (konopy).
3.3. HpuHep: urpa a kap1m
converted to PDF by BoJIoc
Mm ncnonLsyeH nporpaHHnyk a6c1pakunk 1nnnuno nrpm a kap1m, u1o6m nosnakoHn1L aac
c pasnnunmHn o6Lek1no-opnen1npoaannmHn nsmkaHn nporpaHHnpoaannn, ko1opme
paccHa1pnaak1cn a a1o knnre. B cnepykue rnaae Hm ncnonLsyeH paspa6o1annm specL
knacc Card pnn nanncannn nacLnnca "kocmnka". Knacc Card, nopo6no nac1onunH nrpanLnmH
kap1aH, Hano u1o snae1 o caoeH npepnonaraeHoH ncnonLsoaannn n Hoxe1 npnHenn1Lcn a
kap1ouno nrpe nk6oro 1nna.

Puc. 3.2. CRC-kap1ouka pnn knacca Card
Ha pnc. 3.2 nokasana CRC-kap1ouka, ko1opan onncmaae1 noaepenne nrpanLno kap1m.
O6nsannoc1n knacca Card ouenL orpannuenm. B caoe ocnoae on nanne1cn npoc1o
apHnnnc1pa1opoH pannmx, ko1opm xpann1 n aosapauae1 snauennn Hac1n n panra, a 1akxe
pncye1 kap1y.
Kak Hm o1Heuann a rnaae 2, CRC-kap1oukn Hnoro pas y1ounnk1cn n nepepncoamaak1cn,
Hepnenno aaonkunonnpyn o1 ec1ec1aennoro nsmka k nporpaHHnoHy kopy. Kak Hm noHnnH,
na cnepykueH a1ane kaxpm He1op cna6xae1cn nHeneH n cnnckoH apryHen1oa. He
ncknkueno, u1o onncanne amnese1 sa kap1ouky, n 1orpa nx npnpe1cn ckpennn1L ckpenkaHn
(nHee1 cHmcn aoo6ue o1kasa1Lcn o1 kap1ouek, saHenna nx ueH-1o apope o1ue1oa).
CRC-kap1ouka, nso6paxennan na pnc. 3.3, coo1ae1c1aye1 cnepykueHy a1any. 3aHe1L1e, u1o
paxe ecnn o6nsannoc1L coc1on1 acero nnuL a aosapa1e snauennn (nanpnHep, npnsnaka
"kap1nnka aaepx"), Hm ace paano onpepenneH qynkunk pnn nocpepnnuec1aa a amnonnennn
sanpoca. HHek1cn kak npak1nueckne, 1ak n 1eope1nueckne coo6paxennn a nonLsy a1oro. Mm
aepneHcn k nnH a rnaae 17.
Kak 6mno npepnoxeno panee, Hoxno ampenn1L n sannca1L na o6opo1no c1opone CRC-
kap1oukn snauennn pannmx, ko1opme ponxnm copepxa1Lcn a kaxpoH akseHnnnpe knacca
nrpanLno kap1m. Cnepykun a1an coc1on1 a nepeaope noaepennn n coc1onnnn, onncannmx
na CRC-kap1ouke, a amnonnneHm kop. Mm paccHo1pnH a1o1 a1an nocne 1oro, kak nccnepyeH
pnxo1oHnk Hexpy o6LnanenneH n onpepenenneH.
converted to PDF by BoJIoc

Puc. 3.3. Y1ounenne CRC-kap1oukn pnn knacca Card
3.4. Hu1epqec u peanusaun
B rnaae 1 Hm pacckasann o6 nc1opnn o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn, o
1oH, u1o ono ocnoamaae1cn na npepuec1aykunx npenx HopynLnoc1n n Hacknpoakn
nnqopHaunn. B npouecce aaonkunn neko1opme coo6paxennn n konuenunn 6mnn
o16pouenm, korpa okasanocL, u1o onn pacxopn1cn c konuenune o6Lek1no-
opnen1npoaannoro npoek1npoaannn, no ppyrne nonn1nn coxpannnncL. B uac1noc1n,
npnnunnm Hapnaca npnHennHm k o6Lek1no-opnen1npoaanno 1exnonornn a 1o xe Hepe,
kak n k HopynLnoHy nopxopy. Mm HoxeH cnepykunH o6pasoH nepeqpasnpoaa1L npen
Hapnaca a 1epHnnax o6Lek1oa:
O6Lnanenne knacca ponxno o6ecneunaa1L knnen1a ace nnqopHaune,
neo6xopnHo pnn ycneuno pa6o1m c knaccoH, n nnkako ppyro.
Me1opaH ponxna 6m1L poc1ynna acn nnqopHaunn, neo6xopnHan pnn amnonnennn nx
o6nsannoc1e, n nnkakan ppyran.
Hpnnunnm Hapnaca penn1 Hnp o6Lek1a na pae uac1n. HHee1cn aneunn o6pas,
na6nkpaeHm nonLsoaa1eneH o6Lek1a, Hm 6ypeH nasmaa1L a1o npepc1aanenne o6
o6Lek1e nn1epqecoH (interface), nockonLky ono onncmaae1, kak o6Lek1 asanHopec1aye1 c
aneunnH HnpoH. O6pa1nan c1opona o6Lek1a cansana c ero peannsaune (implementation).
HonLsoaa1enk paspeuen poc1yn 1onLko k 1oHy, u1o onncano a nn1epqecno uac1n.
Peannsaunn onpepenne1, kak poc1nrae1cn amnonnenne o6nsannoc1e, sananennmx a
nn1epqecno uac1n.
3a ncknkuenneH nsmka Smalltalk ace nsmkn nporpaHHnpoaannn, ko1opme Hm
paccHa1pnaaeH, noppepxnaak1 pas6nenne knacca na 6nok nn1epqeca n 6nok peannsaunn.
Mm onnueH coo1ae1c1aykun HexannsH a paspenax, nocanuennmx oco6ennoc1nH kaxporo
nsmka. 3aHe1L1e, u1o paspenenne nn1epqeca n peannsaunn ne nanne1cn a 1ounoc1n
nnkancynnune pannmx, paccHo1penno panee. Hepaoe nanne1cn a6c1pak1nmH nonn1neH,
a1opoe HexannsHoH ero aonnouennn. [pyrnHn cnoaaHn, Hopynn ncnonLsyk1cn a
npouecce peannsaunn o6Lek1oa, npnnapnexaunx k a6c1pak1nmH 1nnaH pannmx, no Hopynn
caHn no ce6e ne nannk1cn a6c1pak1nmHn 1nnaHn pannmx.
3.5. Knaccm u He1opm a OOH
converted to PDF by BoJIoc
B cnepykunx paspenax noppo6no onncmaae1cn HexannsH onpepenennn knaccoa n He1opoa
pnn kaxporo ns nsmkoa nporpaHHnpoaannn, ko1opme Hm paccHa1pnaaeH. O6pa1n1e
annHanne, u1o neko1opme o6Lek1no-opnen1npoaannme nsmkn paccHa1pnaak1 knaccm kak
cneunannsnpoaannyk qopHy sannce, a 1o apeHn kak ppyrne nsmkn ncnonLsyk1 nnme
nopxopm.
3.5.1. Knaccm u He1opm a nsmke Object Pascal
Ho kpane Hepe paa pasnnunmx nsmka nocn1 nHn Object Pascal. HcxopnmH nanne1cn nsmk,
cospannm appn TecnepoH ns koHnannn Apple Computer [Tesler 1985]. Hsmk 6mn noc1poen
na ocnoae Hopyne ns nsmka Apple Pascal. B1opo aapnan1 nsmka Object Pascal
nepaonauanLno nasmaancn Turbo Pascal [Turbo 1988, OBrian 1989]. Ero paspa6o1ana n
pacnpoc1pannna koHnannn Borland International. Hepam ynoHnny1m nsmk ouenL uac1o
ac1peuae1cn na koHnLk1epax Macintosh, a1opo 6onLue uac1Lk cansan c IBM PC. Hsmk,
cospannm koHnanne Borland, anoaL npnanek annHanne k Object Pascal. Ceuac a1o1 nsmk
ncnonLsye1cn a kauec1ae qynpaHen1a a cpepe Delphi pnn paspa6o1kn Windows-npnnoxenn
[Borland 1995]. B nero 6mnn aaepenm noame caoc1aa, o1cy1c1aoaaaune a ncxopnoH nsmke
Turbo Pascal. B panno knnre Hm noc1apaeHcn onnca1L o6e aepcnn nsmka, o1Heuan oco6o,
rpe n a ueH onn pasnnuak1cn.
B nsmke Object Pascal HopynL nasmaae1cn 6n6nno1eko npouepyp (unit). B o1nnune o1
nsmkoa C++ n Objective-C 6n6nno1eka npouepyp copepxn1cn a epnnoH qane, a ne
pas6naae1cn na paa. TeH ne Henee 6n6nno1eka npouepyp coc1on1 ns nn1epqeca (interface)
n peannsaunn (implementation). Bn6nno1eka npouepyp Hoxe1 nopknkua1L ppyrne
6n6nno1ekn. D1o1 npouecc penae1 poc1ynnmHn caoc1aa, onncannme a paspene nn1epqeca
nopknkuaeHo 6n6nno1ekn.
ac1L 6n6n1o1ekn pnn knacca Card a nsmke Object Pascal aepcnn qnpHm Apple nokasana a
nnc1nnre 3.1. Paspen interface ananornuen onncannnH qynkun a Pascal. On Hoxe1
copepxa1L noppaspenm, o6osnauaeHme knkueamHn cnoaaHn const, type n var. 3pecL xe
sapak1cn neo6Lek1nme 1nnm pannmx (1akne, kak nepeuncnneHme 1nnm suits n colors).
Onncanne knacca nanoHnnae1 sanncL (record), sa ncknkuenneH 1oro, u1o knacc Hoxe1
copepxa1L saronoakn npouepyp n qynkun napnpy c nonnHn pannmx. Hocnepnne ponxnm
6m1L nepeuncnenm nepep o6LnanennnHn qynkun. Hone pannmx n He1opm ponxnm nHe1L
pasnme nHena, noa1oHy none pannmx nasmaae1cn suitValue, a qynkunn suit. B opno
6n6nno1eke Hoxno onpepenn1L neckonLko knaccoa.
Onncanne knacca nsyuae1cn nonLsoaa1ennHn naHnoro uaue, ueH co6c1aenno kop. Ho a1o
npnunne pnn o6neruennn nonnHannn a onncannn ncnonLsyk1cn koHHen1apnn. Onncannn
pannmx ponxnm o1penn1Lcn o1 onncannn He1opoa. Me1opm rpynnnpyk1cn a coo1ae1c1ann c
a6c1pak1no knaccnqnkaune nx noaepennn. B npepenax kaxpo rpynnm He1opm Hoxno
nepeuncnn1L a anqaan1noH nopnpke. Honesno ncnonLsoaa1L 1a6ynnunk, a1o noHoxe1
nonLsoaa1enk 6mc1po na1n nHena He1opoa.
Ruc1uur 3.1. Hn1epqecnm paspen 6n6nno1ekn pnn nsmka Object Pascal qnpHm Apple
unit card;
interface
type
suits = (Heart, Club, Diamond, Spade);
colors = (Red, Black);
Card = object
(* nonn zann-x *)
suitValue : suits;
rankValue : integer;
faceUp : boolean;
(* wnwuwanwsauwn *)
procedure setRankAndSuit (c : integer; s : suits);
converted to PDF by BoJIoc
(* paouwe ynxuww *)
function color : colors;
procedure draw (win : windows; x, y : integer);
function faceUp : boolean;
procedure flip;
function rank : integer;
function suit : suits;
end;
implementation
...
end.

Ruc1uur 3.2. Hn1epqecnm paspen 6n6nno1ekn pnn nsmka 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;
...
end.

3.5.2. Knaccm u He1opm a nsmke Smalltalk
Onncanne nsmka Smalltalk nou1n nepaspmano cansano c nonLsoaa1enLcknH nn1epqecoH
cpepm Smalltalk. TaknH o6pasoH, o6Lncnenne 1oro, kak a nsmke Smalltalk cospak1cn noame
knaccm, ponxno o6nsa1enLno naunna1Lcn c onncannn nporpaHHm npocHo1pa nnn 6poysepa
Smalltalk. He 1onLko co6c1aenno 6poysep nanne1cn poc1a1ouno cnoxnmH, no n pe1ann ero
peannsaunn o1nnuak1cn pnn pasnnunmx cnc1eH. Hoa1oHy naue o6cyxpenne neo6xopnHmH
o6pasoH 6ype1 noaepxnoc1nmH. n1a1enL, sann1epecoaannm a 6onee noppo6no
nnqopHaunn, ponxen o6pa1n1Lcn k pykoaopc1ay no 1o aepcnn nsmka Smalltalk, ko1opyk on
ncnonLsye1 [Goldberg 1984, LaLonde 1990b, Korienek 1993, Smith 1995].
[nn nonLsoaa1enn 6poysep npepc1aanne1 co6o 6onLuoe okno, paspenennoe na nn1L
okouek ue1mpe HanenLknx n opno 6onLuoe (pnc. 3.4). Kaxpoe ns aepxnnx okon nHee1
nonocm npokpy1kn. Hnxnee okno ncnonLsye1cn pnn amcaeunaannn n pepak1npoaannn
nnqopHaunn. Bpoysep ynpaanne1cn nocpepc1aoH Hmun, ko1opan ponxna nHe1L 1pn knonkn:
neaan knonka ncnonLsye1cn pnn onepaun am6opa n pepak1npoaannn, cpepnnn n npaaan
knonkn amsmaak1 Henk c onepaunnHn.
Knaccm a nsmke Smalltalk crpynnnpoaanm a ka1eropnn. B nepaoH okne npokpyunaae1cn
cnncok acex ka1eropn, nsaec1nmx cnc1eHe Smalltalk. Xo1n Hoxno cospa1L noayk
ka1eropnk, pnn naunx uene poc1a1ouno am6pa1L cyuec1aykuyk ka1eropnk n
ckonc1pynpoaa1L o1nocnuncn k ne noam knacc. Bm6op aneHen1a "Graphics-Primitives" a
nepaoH okne npnaepe1 k amnonnennk payx pec1an: ao a1opoH okne o1o6pasn1cn cnncok
acex knaccoa, o1nocnunxcn k panno ka1eropnn, a a 6onLuoH okne pepak1npoaannn
nonan1cn 1ekc1 coo6uennn, amsmaaeHoro npn cospannn noamx knaccoa.
converted to PDF by BoJIoc
Hocne pepak1npoaannn c ncnonLsoaanneH Hmun nonLsoaa1enL Hoxe1 saHenn1L a1o
coo6uenne na:
Object subclass: #Card
instanceVariableNames: 'suit rank'
classVariableNames: ''
poolDictionaries: ''
category: 'Graphics-Primitives'
B pannm HoHen1 Hm 6ypeH paccHa1pnaa1L a1o coo6uenne npoc1o a kauec1ae nnnkc1paunn
1oro, u1o Card cospae1cn kak nopknacc Object.
Kaxpm akseHnnnp knacca Card copepxn1 paa nonn pannmx. Kak n a nsmke Delphi Pascal,
ace knaccm ponxnm 6m1L nopknaccaHn yxe cyuec1aykunx knaccoa. Knacc Object nanne1cn
nan6onee o6unH nopoxpakunH knaccoH.
3aHe1nH, u1o nHena none pannmx ne cansanm c kaknH-nn6o konkpe1nmH 1nnoH pannmx.
Hsmk Smalltalk ne nHee1 onepa1opoa o6Lnanennn 1nna, n nepeHennme Hory1 npnnnHa1L
nponsaonLnme snauennn. Mm noroaopnH o npnnunnnanLno pasnnue Hexpy nsmkaHn c
1nnaHn pannmx n nsmkaHn 6es 1akoamx nospnee, npn o6cyxpennn nepecmnkn coo6uenn. B
naueH knacce ne1 nepeHennmx knacca n nepeHennmx-cnoaape (pool variables). D1n
nonn1nn o1nocn1cn k cnepykueHy ypoank cnoxnoc1n. HepeHennme knacca 6ypy1
o6cyxpa1Lcn a nocnepykunx rnaaax. HepeHennme-cnoaapn amxopn1 sa paHkn nac1onue
knnrn.
3nak # nepep cnoaoH Card npen1nqnunpye1 ero kak cnHaon. Han6onee aaxnoe caoc1ao
cnHaonoa opnosnaunoe coo1ae1c1ane Hexpy nHeneH n snauenneH. To ec1L nHenoaannme
cnHaonm Hory1 nHe1L pasnnunme snauennn, no ace cnHaonm c opnnH nHeneH coo1ae1c1ayk1
opnoHy n 1oHy xe snauennk. TeH caHmH cnHaonm o6muno ncnonLsyk1cn kak knkun nnn
saHenn1enn ka1eropn.
3akonuna c onpepenenneH xapak1epnc1nk noaoro knacca, nonLsoaa1enL am6npae1 koHanpy
accept ns Henk onepaun. TenepL noam knacc aaepen a cnc1eHy, n 1pe1Le okno
amcaeunaae1 paspeuennme onepaunn (nepaonauanLno cnncok nyc1).
Bm6op okna c rpynnaHn onepaun ak1nansnpye1 nocnepnee okno a aepxne rpynne a neH
ykasanm konkpe1nme He1opm. Kak n co cnnckoH ka1eropn, npn am6ope nHenn rpynnm a
ue1aep1oH okne amcaeunaak1cn cyuec1aykune He1opm, npnnapnexaune rpynne;
opnoapeHenno a nnxneH okne amaopn1cn ua6non, ko1opm Hoxe1 pepak1npoaa1Lcn pnn
renepnpoaannn noamx He1opoa. Hopo6nm ua6non pepak1npoaannn nokasan na
npepmpyueH pncynke.
1o6m cospa1L noam He1op, nonLsoaa1enL pepak1npye1 ua6non n am6npae1 koHanpy accept
ns Henk onepaun, korpa pepak1npoaanne sakonueno. Hnxe nokasan He1op,
nnnunannsnpykun Hac1L n panr nrpanLno kap1m.
setSuit: s rank: r
" ycianasnwsaei snauenwn nepemenn-x oxsemnnnpa suit w rank "
suit := s.
rank := r
B nsmke Smalltalk apryHen1m paspennk1cn knkueamHn cnoaaHn, ko1opme nerko
pacnosnak1cn, nockonLky okanunaak1cn paoe1ouneH (npen1nqnka1opm ne Hory1
okanunaa1Lcn paoe1ouneH). TeH caHmH nHeneH He1opa, onpepenennoro amue, nanne1cn
setSuit:rank:. Me1op nHee1 paa apryHen1a, ko1opme nsaec1nm eHy kak npen1nqnka1opm s n
r. B neko1opmx aepcnnx nsmka Smalltalk onepa1op npncaanaannn sanncmaae1cn kak c1penka
, a 6onLunnc1ae ppyrnx aepcn ncnonLsye1cn 6onee 1papnunonnoe o6osnauenne :=.
Hakoneu, Hoxno saHe1n1L, u1o 1ouka npnHenne1cn a kauec1ae paspenn1enn onepa1opoa, n
pnn nocnepnero onepa1opa ee Hoxno onyc1n1L.
converted to PDF by BoJIoc
[oc1yn k nepeHennmH akseHnnnpa ne ns He1opoa knacca a nsmke Smalltalk sanpeuen.
Cnepoaa1enLno, Hm ponxnm onpepenn1L nanme qynkunn poc1yna (accessor functions).
Me1op suit, nokasannm nnxe, aosapauae1 1ekyuee snauenne nepeHenno akseHnnnpa c
1eH xe nHeneH:
suit
" sepnyis snauenwe maciw znn zannon xapi- "
suit
C1penka, nanpaanennan aaepx, a1o 1o xe caHoe, u1o knkueaoe cnoao return a ppyrnx
nsmkax nporpaHHnpoaannn. Ona nokasmaae1, u1o cnepykuee sa ne ampaxenne
aosapauae1cn a kauec1ae pesynL1a1a npn amxope ns He1opa. 3aHe1nH, u1o He1opaH
paspeueno nHe1L 1e xe nHena, u1o n nepeHennmH akseHnnnpa, n nnkako ny1annum ne
aosnnkae1 (no kpane Hepe pnn cnc1eHm Hm nnuero ne HoxeH ckasa1L o
nporpaHHnc1ax).
Uenme uncna o1 1 po 13 a1o snauennn, npepc1aannkune panr kap1m (1o ec1L snauenne
nonn rank). Mm 6ypeH ncnonLsoaa1L cnHaonm (a cHmcne SmallTalk) pnn npepc1aanennn
Hac1n kap1m. Coo1ae1c1aenno He1op color (uae1 kap1m) 1oxe 6ype1 aosapaua1L cnHaon a
kauec1ae pesynL1a1a. Cnepykun kop onncmaae1 a1o1 He1op:
color
" sepnyis usei zannon xapi- "
(suit = #diamond) ifTrue: [ #red ]
(suit = #club) ifTrue: [ #black ]
(suit = #spade) ifTrue: [ #black ]
(suit = #heart) ifTrue: [ #red ]
O6pa1n1e annHanne, u1o ycnoanme onepa1opm a nsmke Smalltalk sanncmaak1cn 1ak, kak
ecnn 6m onn 6mnn coo6uennnHn, nepecmnaeHmHn ycnoano uac1n (na caHoH pene 1ak ono n
ec1L). Kaappa1nme cko6kn o6pasyk1 1o, u1o a Smalltalk nasmaae1cn blocks, nx Hoxno
paccHa1pnaa1L kak konc1pykunk, ananornunyk 6nokaH a nsmke Pascal (napa koHanp begin,
end). (B pec1an1enLnoc1n ace cnoxnee. Oak1nueckn 6nok caH no ce6e nanne1cn o6Lek1oH,
ko1opm nepecmnae1cn a kauec1ae apryHen1a aHec1e c coo6uenneH ifTrue k 6yneackoHy
o6Lek1y. HaunnakunH Smalltalk-nporpaHHnc1aH nyuue nponrnopnpoaa1L noppo6noc1n.)
3.5.3. Knaccm u He1opm a nsmke Objective-C
Hsmk nporpaHHnpoaannn Objective-C a1o o6Lek1no-opnen1npoaannoe pacunpenne
pnpek1nanoro nsmka C. B kauec1ae 1akoaoro on nacnepye1 6onLuyk uac1L c1pyk1yp n
He1opoa ncnonLsoaannn C. B uac1noc1n, peannsaunn Hopyne ocnoaana na c1anpap1noH
cornauennn nsmka C o paspenennn qanoa na pae ka1eropnn: nn1epqecnme qanm
(o6muno c pacunpenneH ".h") n qanm peannsaunn (a nsmke C onn o6muno nHek1
pacunpenne ".c", a a Objective-C ".m"). Hpepnonarae1cn, u1o nonLsoaa1enk knacca
(nepaan ka1eropnn nkpe, nepeuncnneHan a pnxo1oHnn Hapnaca) 1pe6ye1cn npocHo1pe1L
1onLko nn1epqecnme qanm.
Hn1epqecnm qan, nopo6nm 1oHy, u1o ncnonLsye1cn pnn naue a6c1pakunn nrpanLnmx
kap1 (nnc1nnr 3.4), cnyxn1 payH uennH. [nn nporpaHHnc1a on nanne1cn ypo6nmH cpepc1aoH
pokyHen1npoaannn nasnauennn n qynkunonnpoaannn knacca. [nn cnc1eHm on nepepae1
nnqopHaunk o 1nnax pannmx n 1pe6oaannnx k onepa1nano naHn1n. Hnorpa a1n paa
npnHenennn okasmaak1cn a konqnnk1e ppyr c ppyroH. HanpnHep, a nsmke Objective-C, kak
n a nsmke Smalltalk,




converted to PDF by BoJIoc
Ruc1uur 3.4. Hn1epqecnm qan knacca Card na nsmke Objective-C
/*
onwcanwe wniepenca xnacca Card
ns-x nporpammwposanwn: Objective-C
asiop: Twmoiw Bazz, 1995
*/
# import <objc/Object.h>
/* onpezenwis cwmsonsn-e xonciani- znn macien */
# define Heart 0
# define Club 1
# define Diamond 2
# define Spade 3
/* onpezenwis cwmsonsn-e xonciani- znn useios */
# define Red 0
# define Black 1
/* wniepenc xnacca Card */
@ interface Card : Object
{
int suit;
int rank;
int faceup;
}
/* meioz- xnacca Card */
- (void) suit: (int) s rank: (int) c;
- (int) color;
- (int) rank;
- (int) suit;
- (void) flip;
- (void) drawAt: (int) and: (int);
@ end

nonLsoaa1ennH knacca ne paspeuae1cn poc1yn k nnqopHaunn any1pn akseHnnnpoa (1o ec1L
k any1penneHy coc1onnnk). TonLko cansannme c knaccoH He1opm Hory1 nHe1L poc1yn nnn
Hopnqnunpoaa1L pannme akseHnnnpa. Opnako u1o6m onpepenn1L 1pe6yeHyk onepa1nanyk
naHn1L, cnc1eHa ponxna sna1L pasHep kaxporo o6Lek1a. TeH caHmH nepeHennme
akseHnnnpa onncmaak1cn a nn1epqecnoH qane ne pnn nonLsoaa1enn (xo1n onn n
o6ecneunaak1 nonLsoaa1enn nnqopHaune, no nannk1cn nepoc1ynnmHn), no pnn
koHnnnn1opa.
HeckonLko nepamx c1pok nn1epqecnoro qana copepxa1 kop, o6un pnn nsmkoa C n
Objective-C. [npek1naa import ananornuna pnpek1nae include ns nsmka C, sa ncknkuenneH
1oro, u1o ona rapan1npye1, u1o qan nopknkuae1cn 1onLko opnn pas. B pannoH cnyuae
nHnop1npyeHm qan a1o onncanne nn1epqeca knacca Object. [npek1naa define sapae1
neko1opme cnHaonLnme konc1an1m, ko1opme Hm 6ypeH ncnonLsoaa1L pnn o6osnauennn
Hac1e n uae1oa.
CnHaon @ o6osnauae1 nauano kopa, cneunqnueckoro pnn Objective-C. B pannoH cnyuae kop
onncmaae1 nn1epqec knacca Card. Paspeuae1cn sanncmaa1L nn1epqecm pnn neckonLknx
knaccoa a opnoH nn1epqecnoH qane, xo1n o6muno kaxpm knacc nHee1 o1penLnm qan.
B nsmke Objective-C, 1ak xe kak n a nsmkax Smalltalk n Delphi Pascal, kaxpm knacc ponxen
nann1Lcn nopknaccoH yxe cyuec1aykuero knacca; knacc Object nanne1cn nan6onee o6unH
nopoxpakunH knaccoH.
Cnncok nepeHennmx, saknkuennm a qnrypnme cko6kn, ko1opm cnepye1 sa npnsnakoH
nauana knacca, copepxn1 onncannn nepeHennmx (pannmx) o6Lek1a knacca. Kaxpm
akseHnnnp knacca nHee1 o1penLnyk o6nac1L pannmx. Hsmk Objective-C penae1 pasnnune
converted to PDF by BoJIoc
Hexpy 1papnunonnmHn snauennnHn nsmka C (uenmHn, aeuec1aennmHn, c1pyk1ypaHn n 1. p.)
n o6Lek1aHn. O6Lek1m o6Lnannk1cn c 1nnoH pannmx id.
Kak n pnn ykasa1ene a nsmke C, nepeHennan, o6Lnanennan c 1nnoH pannmx id, Hoxe1
copepxa1L nn6o ponyc1nHoe snauenne, nn6o cneunanLnoe snauenne Null.
C1pokn, cnepykune sa onncanneH pannmx, onncmaak1 He1opm, ko1opme cansanm c pannmH
knaccoH. Onncanne kaxporo He1opa naunnae1cn c cnHaona "-" (peqnc) a nepao kononke,
sa ko1opmH Hoxe1 cnepoaa1L ampaxenne, ananornunoe npnaepennk 1nnoa pannmx a C. Ono
nokasmaae1 1nn snauennn, aosapauaeHoro He1opoH. Tnn o6Lek1a (id) npepnonarae1cn no
yHonuannk, ecnn ne ykasano nnuero ppyroro. TeH caHmH He1op suit (saHe1L1e, u1o He1opm
Hory1 nHe1L 1e xe nHena, u1o n nonn pannmx) aosapauae1 snauenne 1nna integer. Me1op flip
onncan kak nHekun 1nn void. B nsmke C a1o nanne1cn nnpnka1opoH 1oro, u1o
aosapauaeHoe snauenne o1cy1c1aye1 (1o ec1L He1op nanne1cn npouepypo, a ne qynkune).
Tounee 6ype1 ckasa1L, u1o aosapauaeHoe snauenne nrnopnpye1cn. Kak n panLue,
1a6ynnunn, koHHen1apnn n anqaan1noe ynopnpounaanne penak1 onncanne 6onee
nonn1nmH.
Me1opm, ko1opmH 1pe6yk1cn apryHen1m (apope qynkunn nepeHeuennn kap1m nnn He1opa,
npoaepnkuero nonapanne 1oukn any1pL o6nac1n, orpannuenno noneH kap1m),
sanncmaak1cn a c1nne nsmka Smalltalk c knkueamHn cnoaaHn, paspennkunHn cnncok
apryHen1oa. Opnako a o1nnune o1 nsmka Smalltalk kaxpm apryHen1 ponxen
conpoaoxpa1Lcn onncanneH 1nna pannmx, npnueH npn o1cy1c1ann 1akoro onncannn
noppasyHeaae1cn 1nn id. Ykasanne 1nna pae1cn 1ako xe cnn1akcnuecko konc1pykune,
kakan ncnonLsye1cn pnn onncannn 1nna pannmx pesynL1a1a qynkunn.
Oan peannsaunn (nnc1nnr 3.5) naunnae1cn c nHnop1a nn1epqecnoro qana pnn naue
a6c1pakunn nrpanLno kap1m. Kop nsmka Objective-C Hoxe1 cao6opno cHeunaa1Lcn c kopoH
o6munoro C. HanpnHep, a nnc1nnre 3.5 pae c1poukn, onpepennkune cnHaonLnme konc1an1m
pnn pnnnm n unpnnm nrpanLno kap1m, ncnonLsyk1 cnn1akcnc C.
[npek1naa implementation onpepenne1 qak1nueckn kop pnn He1opoa, cansannmx c
knaccoH. Kak nHn popn1enLckoro knacca, 1ak n onpepenennn nepeHennmx akseHnnnpa a
o6nac1n implementation nnorpa onyckak1 onn Hory1 6m1L asn1m ns onncannn
nn1epqeca.
Ruc1uur 3.5. Oan peannsaunn knacca Card na nsmke Objective-C
/*
ann peanwsauww znn xnacca Card
ns-x nporpammwposanwn: Objective-C
asiop: Twmoiw Bazz, 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;
converted to PDF by BoJIoc
faceup = 0;
}
// ... xoe-uio onymeno
@ end

He1 neo6xopnHoc1n, u1o6m He1opm a o6nac1n implementation cnepoaann a 1oH xe nopnpke,
kak onn 6mnn sapanm a nn1epqecno uac1n. 1o6m ynpoc1n1L nonck konkpe1noro He1opa,
nx uac1o nepeuncnnk1 a anqaan1noH nopnpke. 3aronoakn He1opoa noa1opnk1
nn1epqecnm qan, no 1onLko 1enepL sa nnHn cnepye1 1eno He1opa. Kak n a nsmke C, 1eno
qynkunn saknkueno a qnrypnme cko6kn.
3.5.4. Knaccm u He1opm a nsmke C++
Hsmk C++, nopo6no Objecive-C, nanne1cn o6Lek1no-opnen1npoaannmH pacunpenneH
pnpek1nanoro nsmka nporpaHHnpoaannn C. Kak n a C, nonesno pasnnua1L nn1epqecnme
qanm (nHekune pacunpenne ".h") n qanm peannsaunn (pacunpenne saancn1 o1
cnc1eHm).
Kak n a nsmke Objective-C, nn1epqecnm qan (onncmaakun, nanpnHep, a6c1pakunk
nrpanLno kap1m) Hoxe1 copepxa1L onncannn 6onee ueH opnoro knacca, xo1n o6muno a1o
nponcxopn1, 1onLko ecnn knaccm 1ecno cansanm. HockonLky nsmkn C n C++ ne
noppepxnaak1 knkueaoro cnoaa import (a o1nnunn o1 Objective-C), pnn poc1nxennn 1oro xe
aqqek1a ncnonLsye1cn ycnoanoe nopknkuenne qana. Korpa qan card.h cun1maae1cn
anepame, cnHaon CARDH (npepnonarae1cn, u1o on ne ac1peuae1cn a ppyrnx Hec1ax)
nanne1cn neonpepenennmH, n 1eH caHmH cpa6a1maae1 ycnoanm onepa1op ifndef (ecnn ne
onpepeneno), 1ak kak snauenne CARDH pec1an1enLno ne onpepeneno. 3naun1, qan card.h
6ype1 cun1an. Hpn acex nocnepykunx nonm1kax cun1a1L a1o1 qan cnHaon 6ype1 nsaec1en,
n sarpyska qana 6ype1 nponyuena.
# ifndef CARDH // ann zon+en uwiaiscn nwms ozwn pas
# define CARDH
. . .
# endif
Onncanne knacca naunnae1cn c knkueaoro cnoaa class (nnc1nnr 3.6). B C++ onncanne
knacca ao HnoroH nanoHnnae1 c1pyk1ypy a nsmke C, sa ncknkuenneH 1oro, u1o aHec1e c
nonnHn pannmx c1on1 saronoakn npouepyp. Knkueaoe cnoao private: npepuec1aye1
qparHen1aH kopa, poc1yn k ko1opoH paspeuen 1onLko ns caHoro knacca. Knkueaoe cnoao
public: o6osnauae1 o6nac1L nn1epqeca 1o ec1L 1o, u1o anpno nsane knacca. Kak n a
nsmke Objective-C, onncanne nepeHennmx akseHnnnpa a o6nac1n private pae1cn a
nn1epqecnoH qane 1onLko papn koHnnnn1opa (u1o6m on Hor onpepenn1L pasHep naHn1n,
1pe6yeHo pnn o6Lek1a), a pnn nonLsoaa1enn pannoro knacca a1n nonn oc1ak1cn
nepoc1ynnmHn (u1o nanne1cn napyuenneH nepaoro npnnunna Hapnaca).
HockonLky nonLsoaa1enn uac1o nn1epecyk1cn o1kpm1o o6nac1Lk nn1epqeca public, a1a
uac1L ponxna np1n nepao. Kak n amue, cnepye1 ncnonLsoaa1L koHHen1apnn, 1a6ynnunk,
rpynnnpoaanne n ynopnpounaanne no anqaan1y, u1o6m cpena1L onncanne 6onee un1aeHmH.
Oynkunn card(suit, int) a onncannn knacca nanne1cn ynnkanLno ao Hnornx o1nouennnx
ne 1onLko no1oHy, u1o ee nHn coanapae1 c nHeneH knacca, no n no1oHy, u1o y nee ne1
aosapauaeHoro snauennn. D1a qynkunn nasmaae1cn konc1pyk1opoH, ona ncnonLsye1cn npn
nnnunannsaunn cospaaaeHmx akseHnnnpoa knacca. Mm o6cypnH konc1pyk1opm 6onee
noppo6no a rnaae 4.
Knkueaoe cnoao void, kak n a nsmke Objective-C, nokasmaae1 o1cy1c1ane 1nna. Korpa ono
ncnonLsye1cn kak 1nn aosapauaeHoro snauennn, a1o osnauae1, u1o He1op npnHenne1cn kak
npouepypa papn no6ounoro aqqek1a, a ne pnn amuncnennn pesynL1a1a.
converted to PDF by BoJIoc
Me1opm draw n halfdraw nnnkc1pnpyk1 onncanne 1nnoa napaHe1poa kak coc1aano uac1n
o6Lnanennn qynkunn. D1o1 c1nnL peknapnpoaannn nasmaae1cn npo1o1nnoH qynkunn.
TenepL on nanne1cn uac1Lk ANSI c1anpap1a nsmkoa C n C++. 3aHe1L1e, u1o npo1o1nn
ananornuen cnncky apryHen1oa, xo1n apryHen1m npepc1aanenm kak 1nnm pannmx n nx
nHena nannk1cn neo6nsa1enLnmHn.
ApryHen1 c 1nnoH pannmx window, o6pa6a1maaeHm qynkune draw, nepepae1cn uepes
ccmnky. Ha a1o ykasmaae1 & a cnncke apryHen1oa. BonLune c1pyk1ypm, apope onncannn
okon (1nn pannmx window a naueH npnHepe), uac1o nepepak1cn uepes ccmnky.
Ruc1uur 3.6. Onncanne knacca card na nsmke C++
enum suits {diamond, club, heart, spade};
enum colors {red, black};
// acipaxuwn wrpansnon xapi-
// wcnonssyeicn s nacsnnce
// ns-x nporpammwposanwn: C++
// asiop: Twmoiw Bazz, 1995
class card{
public:
// xoncipyxiop
card (suits, int);
// zociyn x aipwyiam xapi-
colors color ();
bool faceUp ();
int rank ();
suits suit ();
// s-nonnnem-e zenciswn
void draw (window &);
void halfdraw (window &, int x, int y);
void flip ();
private:
bool faceup;
int r; // panr
suits s; // macis
};

HockonLky He1opm paccHa1pnaak1cn npoc1o kak nonn cneunanLnoro anpa, npnnapnexaune
o6Lek1y n nepasnnunHme o1 none pannmx, He1op n none pannmx ne Hory1 nHe1L o6uero
nHenn. TeH caHmH nepeHennan s xpann1 snauenne, npepc1aannkuee co6o Hac1L kap1m, a
1o apeHn kak He1op suit aosapauae1 a1o snauenne. Ananornuno npen1nqnka1opm r n rank
nyxnm pnn xpanennn n pnn aosapa1a panra kap1m.
Oan peannsaunn pnn pannoro knacca ponxen o6ecneun1L pa6o1y He1opoa, onncannmx a
nn1epqecnoH qane. Hauano qana peannsaunn pnn naue a6c1pakunn nrpanLno kap1m
nokasano nnxe.
//
// ann peanwsauww
// znn acipaxuww wrpansnon xapi-
//
# include "card.h"
card::card (suits sv, int rv)
{
s = sv; // wnwuwanwswposais macis
r = rv; // wnwuwanwswposais panr
faceup = true; // nauansnoe nono+enwe xapiwnxon ssepx
}
int card::rank()
converted to PDF by BoJIoc
{
return r;
}
Teno He1opa sanncmaae1cn kak c1anpap1nan qynkunn nsmka C, no nHenn He1opa
npepuec1aye1 nHn knacca n paa paoe1ounn. Ha nepeHennme akseHnnnpa (nonn pannmx
knacca) Hoxno ccmna1Lcn any1pn He1opa kak na o6munme nepeHennme. KoH6nnaunn nHenn
knacca n nHenn He1opa o6pasye1 nonnoe nHn, ona Hoxe1 paccHa1pnaa1Lcn kak ananor
nHenn n qaHnnnn npn npen1nqnkaunn nnunoc1n.
1o6m apoxnoan1L nporpaHHnc1oa ncnonLsoaa1L 1akne npnnunnm paspa6o1kn nporpaHH kak
a6c1parnpoaanne n nnkancynnunn, nsmk nporpaHHnpoaannn C++ npepoc1aanne1 nH
aosHoxnoc1L onpepenn1L ac1panaaeHme qynkunn. [nn 1oro k1o k ne o6pauae1cn,
ac1panaaeHan qynkunn amrnnpn1 1ouno 1ak xe, kak n o6munan, c 1eHn xe caHmHn
cnn1akcnuecknHn npaannaHn pnn sapannn apryHen1oa. Epnnc1aennan pasnnua coc1on1 a
1oH, u1o koHnnnn1op nHee1 npaao npeo6pasoaa1L amsoa ac1panaaeHo qynkunn
nenocpepc1aenno a kop a 1ouke ee amsoaa, cokpauan 1eH caHmH pacxopm na o6pauenne k
qynkunn n aosapa1 ynpaanennn. (Kak n a cnyuae pnpek1nam register, inline-peannsaunn
nanne1cn noxenanneH, koHnnnn1op nHee1 npaao ero nponrnopnpoaa1L.)
inline int Card::rank()
{
return r;
}
A6c1parnpoaanne n nnkancynnunn uac1o cnoco6c1ayk1 nonanennk 6onLuoro konnuec1aa
qynkun, ko1opme amnonnnk1 nesnaun1enLnyk uac1L pa6o1m n, cnepoaa1enLno, nHek1
ne6onLuo pasHep. Onpepennn nx kak ac1panaaeHme qynkunn, nporpaHHnc1 Hoxe1
coxpann1L amropm nnkancynnunn n ns6exa1L sa1pa1 na amsoa qynkun ao apeHn
amnonnennn. Xo1n upesHepnoe annHanne k aqqek1nanoc1n Hoxe1 6m1L apepnmH c 1oukn
spennn paspa6o1kn napexnoro kopa, nporpaHHnc1m uac1o 6onesnenno aocnpnnnHak1
cn1yaunk, korpa 1eno qynkunn coc1on1 1onLko ns opnnokoro onepa1opa return. D1o
osnauae1, u1o amsoa qynkunn Hoxe1 sann1L 6onLue apeHenn, ueH amnonnenne ee 1ena. C
noHouLk ac1panaaeHmx qynkun a1nx npo6neH Hoxno ns6exa1L. Ecnn 1akan qynkunn
onpepenne1cn a o1kpm1o nn1epqecno uac1n onncannn knacca, 1o n ace onpepenenne
ac1panaaeHo qynkunn sapae1cn a nn1epqecnoH qane, a ne a qane peannsaunn.
Ruc1uur 3.7. Onncanne knacca card c inline-He1opaHn, nsmk C++
// acipaxuwn wrpansnon xapi-
// ns-x nporpammwposanwn: C++
// asiop: Twmoiw Bazz, 1995
class card
{
public:
// xoncipyxiop-
card (suits, int);
card ();
card (const card & c);
// zociyn x aipwyiam xapi-
int rank()
{return r;}
suits suit()
{return s;}
colors color();
bool faceUp()
{return faceup;}
//s-nonnnem-e zenciswn
void draw (window & w, int x, int y);
void halfdraw (window & w, int x, int y);
converted to PDF by BoJIoc
void flip()
{faceup = ! faceup;}
private:
bool faceup;
int r; // panr
suits s; // macis
};

Kak cnepye1 ns ee nasaannn, ac1panaaeHan qynkunn 6ype1 ac1poena a kop. TaknH o6pasoH,
amsoaa qynkunn (c 1oukn spennn Haunnnoro kopa) ne nponsope1. C ppyro c1oponm,
uac1o aosnnkae1 Hnoxec1ao konn 1ena qynkunn, 1ak u1o ac1panaanne cnepye1
ncnonLsoaa1L pnn qynkun, 1eno ko1opmx ouenL Hano nnn ko1opme pepko amsmaak1cn.
HoHnHo 1oro u1o ac1panaaeHme qynkunn nHek1 npenHyuec1ao a aqqek1nanoc1n no
cpaanennk c o6munmHn, onn npoponxak1 nonn1nky Hacknpoakn pannmx. HanpnHep,
knnen1m ne nHek1 npnHoro poc1yna k pannmH, onpepenennmH c knkueamH cnoaoH private.
Hpn nn1encnanoH ncnonLsoaannn ac1panaaeHmx qynkun anonne peanLno, u1o qan
peannsaunn okaxe1cn kopoue qana c nn1epqecoH.
Bc1panaaeHme qynkunn 1akxe Hoxno onpepenn1L, sapaaan 1eno qynkunn nenocpepc1aenno
any1pn onpepenennn knacca (cH. nnc1nnr 3.7). Opnako a1o npnaopn1 k 1oHy, u1o
onpepenenne knacca penae1cn 6onee 1pypnmH pnn u1ennn n noa1oHy ponxno
ncnonLsoaa1Lcn 1onLko 1orpa, korpa He1opoa neHnoro, a nx kop ouenL kopo1kn. KpoHe
1oro, neko1opme koHnnnn1opm 1pe6yk1, u1o6m nonn pannmx n ac1panaaeHme qynkunn 6mnn
onpepenenm po 1oro, kak onn ncnonLsyk1cn. D1o amnyxpae1 noHeua1L any1pennne (private)
nonn pannmx po onpepenennn nn1epqecnmx koHnonen1 (public), n npnaopn1 k nsHenennk
nopnpka qynkun.
O1pennn onpepenenne ac1panaaeHmx qynkun o1 onncannn knacca, Hoxno a o6nac1n
onncannn nepeuncnn1L He1opm a nornueckoH nopnpke, a 1o apeHn kak peannsaunn,
cnepykuan sa onncanneH knacca, aepon1no, npopnk1ye1 ppyro nopnpok.
3.5.5. Knaccm u He1opm a nsmke Java
Tpypno ckasa1L, cnepye1 nn onncmaa1L nsmk Java kak pnanek1 C++. Xo1n cnauana kaxe1cn,
u1o a1n paa nsmka nHek1 Hnoro o6uero, any1pennne pasnnunn poc1a1ouno snaun1enLnm,
u1o onpaapmaae1 pnn Java c1a1yc coaepuenno noaoro nsmka. C opno c1oponm, nsmk Java
ne nHee1 ykasa1ene, ccmnok, c1pyk1yp, o6Lepnnenn, onepa1opa goto, qynkun (ec1L
He1opm), neperpyskn onepa1opoa. C ppyro c1oponm, on noppepxnaae1 c1pokn kak
npnHn1nanm 1nn pannmx (u1o ne penae1 C++) n ncnonLsye1 "c6opky Hycopa" pnn
ynpaanennn naHn1Lk.
Xo1n caH no ce6e Java nanne1cn nsmkoH nporpaHHnpoaannn o6uero nasnauennn, nepaann
nn1epec k neHy cansan c ero ncnonLsoaanneH a kauec1ae cpepc1aa paspa6o1kn pnn World
Wide Web. B naueH nsnoxennn Hm 6ypeH nrnopnpoaa1L a1o1 acnek1 n ckonuen1pnpyeHcn na
caoc1aax Java kak opnoro ns nsmkoa nporpaHHnpoaannn.
Onncanne knacca na nsmke Java (npnHep npnaepen a nnc1nnre 3.8) ouenL noxoxe na
onpepenenne knacca a nsmke C++ sa ncknkuenneH cnepykunx o1nnun:
O1cy1c1ayk1 npenpoueccop, rno6anLnme nepeHennme, nepeuncnneHme 1nnm
pannmx. CnHaonLnme konc1an1m Hory1 6m1L cospanm ny1eH onncannn n
nnnunannsaunn nokanLnmx nepeHennmx c ncnonLsoaanneH knkueaoro cnoaa final.
Takne "1epHnnanLnme" snauennn ne Hory1 anocnepc1ann nsHenn1Lcn n 1eH caHmH
okasmaak1cn akanaanen1nmHn cnHaonLnmH konc1an1aH.

converted to PDF by BoJIoc
Ruc1uur 3.8. C1anpap1noe onncanne knacca na nsmke Java
class Card
{
// ciaiwuecxwe snauenwn useios w macien
final public int red = 0;
final public int black = 1;
final public int spade = 0;
final public int heart = 1;
final public int diamond = 2;
final public int club = 3;
// nonn zann-x
private boolean faceup;
private int r;
private int s;
// xoncipyxiop
Card (int sv, int rv)
{ s = sv; r = rv; faceup = false; }
// zociyn x aipwyiam xapi-
public int rank ()
{ return r; }
public int suit()
{ return s; }
public int color ()
{ if ( suit() == heart SS suit() == diamond )
return red;
return black; }
public boolean faceUp()
{ return faceup; }
// s-nonnnem-e zenciswn
public void draw (Graphics g, int x, int y)
{
/* ... nponymeno ... */
}
public void flip ()
{ faceup = ! faceup; }
};


Peannsaunn He1opoa npnaopn1cn nenocpepc1aenno any1pn onpepenennn knacca, a
ne rpe-nn6o a ppyroH Hec1e. (D1o paspeueno a nsmke C++ a kauec1ae onunn, no
nanne1cn o6nsa1enLnmH pnn nsmka Java.)
BHec1o pas6nennn onncannn knacca na private n public a1n knkueame cnoaa
npncoepnnnk1cn a nanoH anpe k kaxpo nepeHenno nnn He1opy.
ornueckn 1nn pannmx nHenye1cn boolean aHec1o bool, ncnonLsyeHoro a nsmke
C++.
3a ncknkuenneH konc1pyk1opoa (ko1opme, kak n a nsmke C++, pacnosnak1cn a cnny
1oro qak1a, u1o nx nHena coanapak1 c nasaanneH knacca) ace He1opm ponxnm
nHe1L aosapauaeHoe snauenne.
B rnaae 8 Hm paccHo1pnH yue6nm npnHep, uennkoH nanncannm na nsmke Java.

converted to PDF by BoJIoc
Ynpaxueuun
1. HpepnonoxnH, aaH 1pe6ye1cn nporpaHHa na 1papnunonnoH (ne o6Lek1no-
opnen1npoaannoH) nsmke nporpaHHnpoaannn apope Hackann nnn C. Kak 6m am
cHopennpoaann knaccm n He1opm?
2. B nsmkax Smalltalk n Objective-C He1opm, nHekune neckonLko apryHen1oa,
onncmaak1cn c ncnonLsoaanneH knkueamx cnoa, o1pennkunx kaxpm apryHen1. B
nsmke C++ cnncok apryHen1oa npe1 cpasy sa nHeneH He1opa. Onnun1e
npenHyuec1aa n nepoc1a1kn, caoc1aennme kaxpoHy nopxopy, a uac1noc1n,
o6Lncnn1e annnnne na un1aeHoc1L n c1enenL nonnHannn 1ekc1a nporpaHHm.
3. Unqpoao cue1unk a1o nepeHennan c orpannuennmH pnanasonoH, ko1opan
c6pacmaae1cn, korpa ee uenouncnennoe snauenne poc1nrae1 onpepenennoro
HakcnHyHa. HpnHepm: unqpoame uacm n cue1unk knnoHe1paxa. Onnun1e knacc pnn
1akoro cue1unka. O6ecneuL1e aosHoxnoc1L yc1anoanennn HakcnHanLnmx n
HnnnHanLnmx snauenn, yaennuennn snauenn cue1unka na epnnnuy, aosapauennn
1ekyunx snauenn.
4. Onpepenn1e knacc pnn koHnnekcnmx uncen. Hannun1e He1opm pnn cnoxennn,
amun1annn n yHnoxennn koHnnekcnmx uncen.
5. Onpepenn1e knacc pnn ppo6e paunonanLnmx uncen, nannkunxcn o1nouenneH
payx uenmx uncen. Hannun1e He1opm pnn cnoxennn, amun1annn, yHnoxennn n
penennn ppo6e. Kak am npnaopn1e ppo6n k nanHenLueHy snaHena1enk?
6. PaccHo1pnH cnepykune pae koH6nnaunn knacca n qynkunn c ncnonLsoaanneH
nsmka C++. O6Lncnn1e pasnnuy a npnHenennn qynkunn addi c 1oukn spennn
nonLsoaa1enn.
7. class example1
8. { public:
9. int i;
10. };
11. int addi(example1 &x, int j)
12. {
13. x.i = x.i + j;
14. return x.i;
15. }
16. class example2
17. {
18. public:
19. int i;
20. int addi(int j)
21. { i = i+j; return i; }
22. };
23. B a6c1pakunnx nrpanLno kap1m, cospannmx na nsmkax C++ n Objective-C,
ncnonLsye1cn uenouncnennoe penenne pnn onpepenennn uae1a kap1m no ee Hac1n.
Hanne1cn nn a1o xopounH npneHoH? Onnun1e neko1opme poc1onnc1aa n nepoc1a1kn.
Hepennun1e He1opm 1ak, u1o6m y6pa1L saancnHoc1L o1 konkpe1nmx snauenn,
npnnncannmx Hac1nH kap1.
24. Kak am pyHae1e, u1o nyuue: nHe1L knkueame cnoaa public n private
npncoepnnneHmHn k kaxpoHy o1penLnoHy o6Lek1y (kak a nsmke Java) nnn xe
cospaaa1L c nx noHouLk uenme o6nac1n (kak a nsmkax C++, Objective-C n Delphi
Pascal)? O6ocny1e aauy 1ouky spennn.
25. Cpaann1e nnkancynnunk, o6ecneunaaeHyk HexannsHoH knaccoa, n nnkancynnunk a
Hopynnx. eH onn o1nnuak1cn? B ueH noxoxn?
converted to PDF by BoJIoc
Fnaaa 4

Coo6euun, skseHnnnpm u uuuuanusaun
B rnaae 3 Hm akpa1ue oxapak1epnsoaann o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne a
c1a1nke. A nHenno Hm onncann, kak cospak1cn noame 1nnm, knaccm n He1opm. B a1o rnaae
Hm npoponxnH nccnepoaanne HexannsHoa OOH, nsyuna pnnaHnueckne caoc1aa. Onn
onpepennk1 1o, kak cospak1cn noame nepeHennme, kak onn nnnunannsnpyk1cn n
asanHopec1ayk1 ppyr c ppyroH ny1eH nepecmnkn coo6uenn.
B nepaoH paspene Hm paccHo1pnH o6Hen coo6uennnHn. 3a1eH Hm nccnepyeH cospanne n
nnnunannsaunk noamx nepeHennmx. Hop cospanneH Hm 6ypeH nonnHa1L ampenenne naHn1n
nop noam o6Lek1 n cansmaanne a1oro ee yuac1ka c nHeneH nepeHenno. Hop
nnnunannsaune Hm noppasyHeaaeH ne 1onLko yc1anoaky nauanLnmx snauenn none
pannmx o6Lek1a, u1o ananornuno nnnunannsaunn sannce, no 1akxe n npouecc 6onee
o6uero xapak1epa, ko1opm yc1anaannaae1 o6Lek1 a nekoe ncxopnoe nauanLnoe coc1onnne.
C1enenL ckpm1noc1n, c ko1opo a1o nponcxopn1 a 6onLunnc1ae o6Lek1no-opnen1npoaannmx
nsmkoa, nanne1cn aaxnmH acnek1oH nnkancynnunn, ko1opyk Hm cun1aeH opnnH ns
rnaaneunx poc1onnc1a OOH.
4.1. Cuu1akcuc nepecmnku coo6euu
Mm ncnonLsyeH 1epHnn nepecmnka coo6uenn (nnorpa roaopn1 o noncke He1opoa) pnn
o6osnauennn pnnaHnueckoro npouecca o6pauennn k o6Lek1y c 1pe6oaanneH amnonnn1L
onpepenennoe pec1ane. B rnaae 1 Hm neqopHanLno onncann nepecmnky coo6uenn n
o1He1nnn, ueH ona o1nnuae1cn o1 o6munoro amsoaa npouepypm. B uac1noc1n:
Coo6uenne acerpa o6paueno k neko1opoHy o6Lek1y, nasmaaeHoHy nony-ua1eneH
nnn appeca1oH.
[ec1ane, amnonnneHoe a o1ae1 na coo6uenne, ne nanne1cn qnkcnpoaannmH n
Hoxe1 aapLnpoaa1Lcn a saancnHoc1n o1 knacca nonyua1enn. Pasnnunme o6Lek1m,
npnnnHan opno n 1o xe coo6uenne, amnonnnk1 pasnnunme pec1ann.
B npouecce nepecmnkn coo6uenn nHek1cn 1pn ue1ko ampenneHme koHnonen1m:
nonyua1enL (o6Lek1, ko1opoHy nocmnae1cn coo6uenne), cenek1op coo6uennn (1ekc1,
npen1nqnunpykun konkpe1noe coo6uenne) n cnncok apryHen1oa, ko1opme ncnonLsyk1cn
npn peakunn na coo6uenne.
Xo1n a1n nonn1nn nannk1cn uen1panLnmHn pnn o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn, no 1epHnnonornn n cnn1akcnc, ncnonLsyeHme a pasnnunmx nsmkax,
aapLnpyk1cn a unpoknx npepenax. B nocnepykunx paspenax Hm onnueH caoc1aa,
cneunqnunm pnn kaxporo ns paccHa1pnaaeHmx nsmkoa nporpaHHnpoaannn, paano kak n
1epHnnm, ncnonLsyeHme pasnnunmHn coo6uec1aaHn nporpaHHnc1oa.
4.1.1. Cuu1akcuc nepecmnku coo6euu a Object
Pascal
B o1nnune o1 oc1anLnoro OOH-coo6uec1aa nporpaHHnc1m na nsmke Delphi Pascal
ncnonLsyk1 1epHnn coo6uenne pnn cneunqnuecko koHanpm ynpaanennn oknoH. Bonee
1papnunonnoe ero nc1onkoaanne nsaec1no cpepn nnx kak nonck He1opa.
Honck He1opa a nsmke Object Pascal a1o npoc1o sanpoc, nocmnaeHm o6Lek1y, u1o6m
amsaa1L opnn ns ero He1opoa. Kak Hm saHe1nnn a rnaae 3, He1op onncmaae1cn npn
converted to PDF by BoJIoc
onpepenennn o6Lek1a 1ak xe, kak none pannmx a sanncn. Ananornuno, c1anpap1nan
cnn1akcnueckan konc1pykunn c ncnonLsoaanneH 1oukn, ko1opan npnHenne1cn pnn onncannn
nonn pannmx, pacunpena n o6osnauae1 1akxe amsoa He1opa. Cenek1op coo6uennn 1o
ec1L 1ekc1, cnepykun sa 1ouko, ponxen coo1ae1c1aoaa1L opnoHy ns He1opoa,
onpepenennmx pnn knacca nnn nacnepyeHmx o1 popn1enLckoro knacca (Hm nsyuaeH
nacnepoaanne a rnaae 7). TeH caHmH ecnn npen1nqnka1op aCard onncan kak o6Lek1 knacca
Card, 1o cnepykuan koHanpa npnkasmaae1 nrpanLno kap1e napncoaa1L ce6n a ykasannoH
okne a nyxno 1ouke.
aCard.draw (win, 25, 37);
O6Lek1 cneaa o1 1oukn nasmaak1 nonyua1eneH coo6uennn. 3aHe1nH, u1o sa
ncknkuenneH ykasannn nonyua1enn, cnn1akcnc coo6uennn npen1nuen amsoay
1papnunonno qynkunn nnn npouepypm. Ecnn apryHen1m ne sapanm, 1o cnncok a
kpyrnmx cko6kax onyckae1cn. HanpnHep, cnepykuan koHanpa ykasmaae1 nrpanLno
kap1e nepeaepny1Lcn:
aCard.flip;
Ecnn sapannm He1op o6Lnanen kak npouepypa, ampaxenne 1akxe ponxno
ncnonLsoaa1Lcn a kauec1ae npouepypm. Ananornuno, ecnn He1op o6Lnanen kak
qynkunn, on n ponxen ncnonLsoaa1Lcn a a1oH kauec1ae.
KoHnnnn1op npoaepne1 koppek1noc1L nepecmnkn coo6uenn. Honm1ka nepecna1L
coo6uenne, ko1opoe ne ykasano a onncannn knacca o6Lek1a-nonyua1enn, npnaopn1
k oun6ke. Bny1pn He1opa nceaponepeHennan self ncnonLsye1cn pnn ccmnkn na
nonyua1enn coo6uennn. D1a nepeHennan ne onncmaae1cn, no Hoxe1 6m1L
ncnonLsoaana kak apryHen1 nnn kak nonyua1enL ppyrnx coo6uenn.
HanpnHep, He1op color, ko1opm aosapauae1 uae1 nrpanLno kap1m, Hoxe1 6m1L
sanncan cnepykunH o6pasoH:
function Card.color : colors;
var ss : suits;
begin
ss := self.suit;
if (ss = Heart) or (ss = Diamond) then
color:=Red
else
color:=Black;
end;
3pecL He1op suit amsmaae1cn c uenLk nonyun1L snauenne Hac1n. D1o cun1ae1cn
6onee ypauno c1pa1erne nporpaHHnpoaannn, ueH npnHo poc1yn k nonk pannmx
suitValue. Delphi Pascal 1akxe nosaonne1 aosapaua1L pesynL1a1 qynkunn,
npncaanaan ero cneunanLno nepeHenno Result, a ne npen1nqnka1opy qynkunn
(color a naueH cnyuae).
4.1.2. Cuu1akcuc nepecmnku coo6euu a C++
Kak Hm o1He1nnn a rnaae 3, necHo1pn na 1o u1o konuenunn He1opoa n coo6uenn
npnHennHm k nsmky C++, co6c1aenno He1opm n coo6uennn (kak 1epHnnm) pepko
ncnonLsyk1cn a 1ekc1ax no C++. Me1op npnnn1o nasmaa1L qynkune unenoH
knacca (member function); o nepecmnke coo6uenn roaopn1 kak o amsoae qynkunn-
unena.
Onncanne knacca nanoHnnae1 onpepenenne c1pyk1ypm. Cnn1akcnc amsoaa qynkunn-
unena ananornuen poc1yny k nonnH pannmH (nepeHennmH akseHnnnpa).
Cnn1akcnueckan qopHa coc1on1 ns nonyua1enn, sa ko1opmH cnepye1 1ouka,
cenek1opa coo6uennn (ko1opm ponxen coo1ae1c1aoaa1L nHenn qynkunn-unena pnn
knacca nonyua1enn) n cnncka apryHen1oa (a kpyrnmx cko6kax).
converted to PDF by BoJIoc
Ecnn theCard onncan kak akseHnnnp knacca Card, 1o cnepykun onepa1op
npnkasmaae1 nrpanLno kap1e o1o6pasn1L ce6n a sapannoH okne a 1ouke c
kooppnna1aHn 25 n 37:
theCard.draw(win, 25, 37);
[axe ecnn apryHen1m ne 1pe6yk1cn, kpyrnme cko6kn ace paano nyxnm, u1o6m
o1nnun1L amsoa qynkunn-unena o1 o6pauennn k nonnH pannmx. Hnxecnepykun
kop onpepenne1, nexn1 nn kap1a nnueao c1opono aaepx:
if ( theCard.faceUp() )
{
...
}
else
{
...
}
Kak n a nsmke Object Pascal, koHnnnn1op npoaepne1 npaannLnoc1L nepecmnkn
coo6uennn, o6pauancL k onncannk knacca nonyua1enn. Honm1ka nepepa1L
coo6uenne, ko1opoe ne nanne1cn uac1Lk knacca, o1Heuae1cn kak oun6ka npn
koHnnnnunn.
Oynkunn-unen knacca, ko1opan onncana c 1nnoH void, Hoxe1 ncnonLsoaa1Lcn kak
onepa1op (1o ec1L kak amsoa npouepypm). Oynkunn-unenm, ko1opme nHek1
aosapauaeHoe snauenne ppyrnx 1nnoa, amsmaak1cn a coo1ae1c1ann co c1nneH
nsmka C n kak onepa1opm (npouepypm), n kak qynkunn (nopo6no o6pauennnH k
1papnunonnmH qynkunnH).
C kaxpmH He1opoH accounnpoaana nceaponepeHennan, a ko1opo ykasan
nonyua1enL coo6uennn. B nsmke C++ ona nasmaae1cn this n nanne1cn ykasa1eneH
na nonyua1enn, a ne co6c1aenno nonyua1eneH. Hoa1oHy ncnonLsye1cn
pasmHenoaanne ykasa1enn (onepaunn ->) pnn nepecmnkn nocnepykunx coo6uenn
k 1oHy xe nonyua1enk. HanpnHep, He1op color, ko1opm ncnonLsye1cn pnn
onpepenennn uae1a kap1m, Hoxe1 6m1L sanncan cnepykunH o6pasoH (ecnn Hm
xo1nH ns6exa1L npnHoro poc1yna k any1penneHy nonk, copepxaueHy snauenne
Hac1n):
colors Card::color()
{
switch ( this->suit() )
{ case heart:
case diamond:
return red;
}
return black;
}
HepeHennan this 1akxe uac1o ncnonLsye1cn, ecnn any1pn He1opa napo nepepa1L
nonyua1enn coo6uennn a kauec1ae apryHen1a ppyro qynkunn:
void aClass::aMessage(bClass b, int x)
{
// nepezais camoro cen s xauecise aprymenia
b->doSomething(this, x);
}
B nsmke C++ Hoxno onycka1L ncnonLsoaanne this a kauec1ae nonyua1enn. Bny1pn
1ena He1opa amsoa ppyroro He1opa 6es nanoro ykasannn nonyua1enn
nn1epnpe1npye1cn kak coo6uenne pnn 1ekyuero nonyua1enn. TeH caHmH He1op color
Hoxe1 6m1L (n o6muno 6ype1) sanncan cnepykunH o6pasoH:
converted to PDF by BoJIoc
colors Card::color()
{ switch ( suit() )
{ case heart:
case diamond:
return red;
}
return black;
}
4.1.3. Cuu1akcuc nepecmnku coo6euu a Java
Cnn1akcnc pnn nepecmnkn coo6uenn a nsmke Java nou1n npen1nuen
ncnonLsyeHoHy a C++. Epnnc1aennoe saHe1noe o1nnune coc1on1 a 1oH, u1o
nceaponepeHennan this a nsmke C++ o6osnauae1 ykasa1enL na o6Lek1, a a nsmke
Java nanne1cn co6c1aenno o6Lek1oH (nockonLky a nsmke Java ne1 ykasa1ene!).
4.1.4. Cuu1akcuc nepecmnku coo6euu a
Smalltalk
Cnn1akcnc nsmka Smalltalk o1nnuen o1 1oro, ko1opm ncnonLsye1cn a nsmkax C++
nnn Object Pascal npn nepecmnke coo6uenn. Ho-npexneHy nepaan uac1L
ampaxennn onncmaae1 nonyua1enn o6Lek1, ko1opoHy npepnasnauae1cn
coo6uenne. B kauec1ae paspenn1enn npnHenne1cn npo6en, a ne 1ouka.
Kak n a nsmke C++, cenek1op ponxen coo1ae1c1aoaa1L opnoHy ns He1opoa,
onpepenennmx pnn knacca nonyua1enn. Opnako a o1nnune o1 C++ Smalltalk
nanne1cn nsmkoH nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx. D1o
osnauae1: npoaepka 1oro u1o knacc nonyua1enn nonnHae1 cenek1op coo6uennn,
amnonnne1cn ao apeHn pa6o1m nporpaHHm, a ne na a1ane koHnnnnunn.
Ecnn npen1nqnka1op aCard a1o nepeHennan knacca Card, 1o cnepykun onepa1op
npnkasmaae1 nepeaepny1Lcn coo1ae1c1aykue kap1e:
aCard flip
Kak Hm o1He1nnn a rnaae 3, apryHen1m He1opa ampennk1cn knkueamHn
cenek1opaHn. 3a kaxpmH knkueamH cnoaoH-cenek1opoH cnepye1 paoe1oune, a
sa1eH apryHen1. Cnepykuee ampaxenne npnkasmaae1 nepeHenno aCard
napncoaa1L ce6n a okne a 1ouke c kooppnna1aHn 25 n 37:
aCard drawOn: win at: 25 and: 37
B nsmke Smalltalk paxe 6nnapnme onepa1opm, apope + nnn *, nn1epnpe1npyk1cn
kak coo6uennn, npnueH nonyua1eneH nanne1cn neam unen ampaxennn, a npaam
nepepae1cn kak apryHen1
1
. Kak n pnn knkueamx cnoa nnn ynapnmx coo6uenn
(coo6uenn 6es apryHen1a), knaccm anpaae napenn1L 6nnapnme coo6uennn kaknH
yropno cHmcnoH. Ec1L npnopn1e1m: ynapnme coo6uennn nHek1 nanamcun
npnopn1e1, sa1eH cnepyk1 6nnapnme coo6uennn n nakoneu knkueame cnoaa.
B nsmke Smalltalk nceaponepeHennan self any1pn He1opa o6osnauae1 nonyua1enn
coo6uennn. D1o snauenne uac1o ncnonLsye1cn kak nonyua1enL ppyrnx coo6uenn,
u1o noppasyHeaae1, u1o nonyua1enL xenae1 nocna1L coo6uenne caHoHy ce6e.
HanpnHep, onncannm panee He1op color Hoxe1 6m1L nepenncan cnepykunH
o6pasoH, u1o6m ns6exa1L npnHoro o6pauennn k nepeHenno akseHnnnpa suit:

1
Hsmk C++ o6ecneunaae1 ananornunyk neperpysky 6nnapnmx onepa1opoa
(1aknx, kak +, -, < nnn onepa1op npncaanaannn =). D1a 1eHa amxopn1 sa
paHkn panno knnrn, xo1n Hm n ynoHnneH o neperpyske onepa1opa
npncaanaannn a rnaae 12.
converted to PDF by BoJIoc
color
" sepnyis usei xapi- "
(self suit = #diamond) ifTrue: [ #red ].
(self suit = #club) ifTrue: [ #black ].
(self suit = #spade) ifTrue: [ #black ].
(self suit = #heart) ifTrue: [ #red ].
4.1.5. Cuu1akcuc nepecmnku coo6euu a nsmke
Objective-C
Cnn1akcnc n 1epHnnonornn nepecmnkn coo6uenn a nsmke Objective-C nanoHnnae1
Smalltalk. HHek1cn knkueame cnoaa n ynapnme coo6uennn, no a o1nnune o1 nsmka
Smalltalk knaccm a Objective-C ne Hory1 nepeonpepenn1L 6nnapnme onepa1opm.
Hepecmnka coo6uenn a nsmke Objective-C ocyuec1anne1cn 1onLko any1pn amsoaoa
coo6uenn. Takne amsoam a1o ampaxennn, saknkuennme a kaappa1nme cko6kn [
]. HanpnHep, ecnn npen1nqnka1op aCard npepc1aanne1 co6o akseHnnnp knacca
Card, 1o cnepykuee ampaxenne npnkasmaae1 kap1e nepeaepny1Lcn (o6pa1n1e
annHanne na 1ouky c sann1o a konue):
[ aCard flip ];
Hopo6no nsmky Smalltalk, Objective-C ncnonLsye1 pnnaHnueckne 1nnm pannmx. D1o
osnauae1: npoaepka 1oro, u1o nonyua1enL cnoco6en nonn1L coo6uenne,
amnonnne1cn ao apeHn pa6o1m nporpaHHm, a ne na a1ane koHnnnnunn. Ecnn
nonyua1enL ne pacnosnan coo6uennn, renepnpye1cn coo6uenne o6 oun6ke.
Cnn1akcnc nsmka Smalltalk paspeuae1 ncnonLsoaa1L coo6uennn c apryHen1aHn.
HanpnHep, cnepykuan koHanpa npnkasmaae1 kap1e aCard napncoaa1L ce6n a
sapannoH okne a 1ouke c kooppnna1aHn 25 n 36:
[ aCard drawOn: win at: 25 and: 36 ];
Kaappa1nme cko6kn nyxnm 1onLko npn amsoae coo6uennn. Ecnn nepecmnka
coo6uennn 1pe6ye1 aosapauaeHoro snauennn, 1o onepa1op npncaanaannn ponxen
pacnonara1Lcn sa npepenaHn kaappa1nmx cko6ok. Tak, nnxecnepykuee ampaxenne
npncaanaae1 nepeHenno newCard konnk kap1m aCard:
newCard = [ aCard copy ];
Hepecmnka coo6uennn Hoxe1 ncnonLsoaa1Lcn aespe, rpe paspeuenm o6munme
ampaxennn nsmka C. HanpnHep, a cnepykueH onepa1ope nsmka C npoaepne1cn,
nexn1 nn kap1a aCard kap1nnko aaepx:
if ( [ aCard faceUp ] ) ...
Hopo6no nsmky Object Pascal, any1pn He1opoa Objective-C ncnonLsye1cn
npen1nqnka1op self pnn ccmnkn na nonyua1enn coo6uennn. Opnako a o1nnune o1
ppyrnx nsmkoa self nanne1cn nc1nnno nepeHenno, ko1opan Hoxe1 6m1L
Hopnqnunpoaana nonLsoaa1eneH. Mm yanpnH a paspene 4.3.3, nocanuennoH
He1opaH-qa6pnkaH, u1o 1akan Hopnqnkaunn 6maae1 nonesna.
4.2. Cnoco6m cospauun u uuuuanusauu
Hepep 1eH kak pe1anLno paccHo1pe1L HexannsH cospannn n nnnunannsaunn a
kaxpoH ns nsyuaeHmx nsmkoa nporpaHHnpoaannn, Hm paccHo1pnH neko1opme
cony1c1aykune HoHen1m. 3nan o pasnnunmx cnoco6ax, un1a1enL 6ype1 nyuue
nopro1oanen k aocnpnn1nk caoc1a, ko1opme npncyun (nnn ne npncyun) 1oHy nnn
nnoHy nsmky nporpaHHnpoaannn. Mm 6ypeH paccHa1pnaa1L ampenenne naHn1n
(uepes c1ek nnn uepes kyuy (heap)), ocao6oxpenne naHn1n, pa6o1y c ykasa1ennHn
n cospanne o6Lek1oa c nensHenneHmH coc1onnneH. 4.2.1. C1ek npo1na kyun
converted to PDF by BoJIoc
Bonpoc o ampenennn naHn1n uepes c1ek nnn uepes kyuy cansan c 1eH, kak
ampenne1cn n ocao6oxpae1cn naHn1L nop nepeHennme n kakne nanme uarn ponxnm
npn a1oH npepnpnnnHa1Lcn nporpaHHnc1oH. Pasnnuak1 aa1oHa1nueckne n
pnnaHnueckne nepeHennme. Cyuec1aennan pasnnua Hexpy nnHn coc1on1 a 1oH, u1o
naHn1L pnn aa1oHa1nuecknx nepeHennmx cospae1cn npn axope a npouepypy nnn
6nok, ynpaannkun a1nHn nepeHennmHn. Hpn amxope ns 6noka naHn1L (onn1L xe
aa1oHa1nueckn) ocao6oxpae1cn. Mnorne nsmkn nporpaHHnpoaannn ncnonLsyk1
1epHnn c1a1nueckn (static) pnn o6osnauennn nepeHennmx, aa1oHa1nueckn
pasHeuaeHmx a c1eke. Mm 6ypeH npnpepxnaa1Lcn 1epHnna aa1oHa1nueckn, no1oHy
u1o on 6onee copepxa1enen n no1oHy u1o static a1o knkueaoe cnoao, ko1opoe
o6osnauae1 neu1o ppyroe a nsmkax C n C++. B HoHen1, korpa cospak1cn
aa1oHa1nueckne nepeHennme, nponcxopn1 cansmaanne nHenn n onpepenennoro
yuac1ka naHn1n, n a1a cansL ne Hoxe1 6m1L nsHenena ao apeHn cyuec1aoaannn
nepeHenno.
PaccHo1pnH 1enepL pnnaHnueckne nepeHennme. Bo Hnornx 1papnunonnmx nsmkax
nporpaHHnpoaannn (Pascal) pnnaHnueckne nepeHennme cospak1cn cnc1eHno
npouepypo new(x), ko1opan ncnonLsye1 a kauec1ae apryHen1a nepeHennyk-
ykasa1enL. HpnHep:
type
shape : record
form : (triangle, square);
side : integer;
end;
var
aShape : shape;
begin
new (aShape);
...
end.
3pecL ampenne1cn noam yuac1ok naHn1n, n a kauec1ae no6ounoro aqqek1a
snauenne nepeHenno-apryHen1a nsHenne1cn 1ak, u1o 1enepL ona ykasmaae1 na
noam yuac1ok naHn1n. TeH caHmH npoueccm ampenennn naHn1n n npnanskn k
nHenn asanHocansanm.
B ppyrnx nsmkax (1aknx, kak C) ampenenne naHn1n n npnanska k nHenn ne cansanm
ppyr c ppyroH. HanpnHep, a C ampenenne naHn1n ocyuec1anne1cn cnc1eHno
6n6nno1euno qynkune malloc, ko1opo a kauec1ae apryHen1a nepepae1cn o6LeH
ampenneHo naHn1n. Oynkunn malloc aosapauae1 appec 6noka naHn1n, ko1opm
sa1eH npncaanaae1cn nepeHenno-ykasa1enk c noHouLk o6munoro onepa1opa
npncaanaannn. Cnepykun npnHep nnnkc1pnpye1 a1o1 npouecc:
struct shape
{
enum {triangle, square} form;
int side;
};
shape *aShape;
...
aShape = (struct shape *)
malloc(sizeof(struct shape));
...
Cyuec1aennan pasnnua Hexpy ampenenneH naHn1n uepes c1ek n uepes kyuy
coc1on1 a 1oH, u1o naHn1L pnn aa1oHa1nuecknx nepeHennmx (ocnoaannmx na
HexannsHe c1eka) o1aopn1cn 6es kakoro-nn6o nanoro ykasannn co c1oponm
nonLsoaa1enn, a pnn pnnaHnuecko nepeHenno naHn1L ampenne1cn no
cneunanLnoHy sanpocy. Cnoco6m ocao6oxpennn naHn1n 1akxe pasnnuak1cn.
HporpaHHnc1y nou1n nnkorpa ne 1pe6ye1cn sa6o1n1Lcn o6 ynnu1oxennn
converted to PDF by BoJIoc
aa1oHa1nuecknx nepeHennmx, a 1o apeHn kak on ponxen ynpaann1L ocao6oxpenneH
naHn1n npn ampenennn ee uepes kyuy. D1a 1eHa nanne1cn npepHe1oH cnepykuero
paspena.
4.2.2. Bocc1auoaneuue naHn1u
Korpa ncnonLsye1cn ampenenne naHn1n uepes kyuy, neo6xopnHo o6ecneun1L
cneunanLnme cpepc1aa pnn aosapa1a naHn1n, ko1opan 6onLue ne nyxna. B o6ueH
cnyuae nsmkn nporpaHHnpoaannn pas6naak1cn na pae 6onLune ka1eropnn. HackanL,
C n C++ 1pe6yk1, u1o6m nonLsoaa1enL caH o1cnexnaan, kakne pannme eHy 6onLue
ne nyxnm, n a nanoH anpe ocao6oxpan a1y naHn1L c noHouLk nopnporpaHH ns
cnc1eHno 6n6nno1ekn. K npnHepy, a nsmke HackanL 1akan nopnporpaHHa
nasmaae1cn dispose, a a nsmke C free.
[pyrne nsmkn (Java, Smalltalk) Hory1 aa1oHa1nueckn o1cnepn1L cn1yaunk, korpa k
o6Lek1y 6onLue ne1 poc1yna (1eH caHmH on nsonnpoaan o1 nocnepykunx
amuncnenn). Takne o6Lek1m aa1oHa1nueckn co6npak1cn n ynnu1oxak1cn, a
ampenennan pnn nnx naHn1L noHeuae1cn kak cao6opnan. D1o1 npouecc nasmaae1cn
c6opko Hycopa. [nn 1akoro aocc1anoanennn naHn1n nHee1cn neckonLko xopouo
nsaec1nmx anropn1Hoa. Hx onncanne amxopn1 sa npepenm panno knnrn. Xopoun
o6sop anropn1Hoa c6opkn Hycopa pae1cn KoanoH [Cohen 1981].
Kak n a cnyuae c pnnaHnueckn nsHenneHmHn 1nnaHn pannmx (ko1opme 6ypy1
paccHa1pnaa1Lcn a rnaae 10), apryHen1m sa n npo1na c6opkn Hycopa ocnoaanm na
npo1naonoc1aanennn aqqek1nanoc1n n rn6koc1n nporpaHHnoro o6ecneuennn.
Aa1oHa1nsaunn c6opkn Hycopa Hoxe1 6m1L poporo (c 1oukn spennn koHnLk1epnmx
pecypcoa), n ona 1pe6ye1 o1 ncnonnn1enLno cnc1eHm ynpaanennn naHn1Lk. C
ppyro c1oponm, a cnc1eHax, a ko1opmx ynpaanenne pnnaHnuecko o6nac1Lk
naHn1n nopyuae1cn nporpaHHnc1y, 1nnnunmHn nannk1cn cnepykune oun6kn:
Honm1ka ncnonLsoaa1L naHn1L, ko1opan eue ne ampenena.
Bmpenennan naHn1L 6onLue nnkorpa ne ocao6oxpae1cn (npo6neHa,
nsaec1nan kak y1euka naHn1n).
Honm1ka ncnonLsoaa1L naHn1L, ko1opan yxe 6mna ocao6oxpena.
HaHn1L ocao6oxpae1cn payHn nesaancnHmHn npouepypaHn, u1o npnaopn1 k
ocao6oxpennk opnoro n 1oro xe yuac1ka paaxpm.
1o6m ns6exa1L a1nx npo6neH, sauac1yk poc1a1ouno o6ecneun1L, u1o6m kaxpm
o6Lek1 c pnnaHnueckn ampenneHo naHn1Lk nHen ue1ko o6osnauennoro anapenLua.
Bnapeneu naHn1n rapan1npye1 npaannLnoe ncnonLsoaanne naHn1n n ee
ocao6oxpenne, korpa ona 6onLue ne nyxna. B 6onLunx nporpaHHax, kak n a
peanLno xnsnn, cnopm sa npaao 6m1L co6c1aennnkoH paspenneHoro pecypca Hory1
cospa1L npo6neHm.
4.2.3. Ykasa1enu
Ykasa1enn aqqek1nanoe cpepc1ao pa6o1m c pnnaHnuecko nnqopHaune, n no
a1o npnunne onn ncnonLsyk1cn npak1nueckn ao acex o6Lek1no-opnen1npoaannmx
nsmkax. Bonpoc: Ykasa1enn a1o nonesnan a6c1pakunn nnn ne1? ak1nano
pncky1npye1cn a coo6uec1ae nporpaHHnc1oa. D1o nauno caoe o1paxenne a 1oH
qak1e, u1o ne ace o6Lek1no-opnen1npoaannme nsmkn noppepxnaak1 ykasa1enn.
B neko1opmx nsmkax (Java, Smalltalk) o6Lek1m npepc1aannk1cn any1pennnH
o6pasoH kak ykasa1enn, no npn a1oH onn ne ncnonLsyk1cn a a1oH kauec1ae
nporpaHHnc1aHn. B ppyrnx nsmkax (C++, Object Pascal) npnxopn1cn nano pasnnua1L
nepeHennme, copepxaune snauennn, n nepeHennme-ykasa1enn. Kak Hm yanpnH a
converted to PDF by BoJIoc
yue6nmx npnHepax na nsmke C++, ko1opme 6ypy1 npepc1aanenm nnxe, a
peannsaunn o6Lek1no-opnen1npoaannmx caoc1a nsmka ykasa1enn sapec1aoaanm a
nanoH anpe. B nsmke Objective-C snauennn, o6Lnanennme kak id, na caHoH pene
nannk1cn ykasa1ennHn, no a1o a snaun1enLno c1enenn ckpm1o o1 nonLsoaa1enn.
Korpa a Objective-C o6Lek1m onncmaak1cn kak nano npnnapnexaune 1oHy nnn
nnoHy knaccy, nporpaHHnc1y neo6xopnHo pasnnua1L o6Lek1m n nanme ykasa1enn na
nnx.
4.2.4. Cospauue ueusHeuneHoro skseHnnnpa
o61ek1a
B rnaae 3 Hm onncann knacc Card n o1He1nnn opno caoc1ao, xena1enLnoe pnn
a6c1pakunn nrpanLnmx kap1, a nHenno, u1o6m snauennn Hac1n n panra
(poc1onnc1aa) kap1m sapaaanncL 6m nnuL opnaxpm n 6onLue ne HennnncL.
HepeHennme, nopo6nme nonnH pannmx suit n rank (onn ne Hennk1 caonx snauenn
ao apeHn amnonnennn nporpaHHm), nasmaak1cn nepeHennmHn c opnokpa1nmH
npncaanaanneH (single-assignment variables) nnn xe nensHenneHmHn nepeHennmHn
(immutable variables). O6Lek1, y ko1oporo ace nepeHennme akseHnnnpa nannk1cn
nensHenneHmHn, a caok ouepepL nasmaae1cn nensHenneHmH o6Lek1oH.
HensHenneHme nepeHennme cnepye1 o1nnua1L o1 konc1an1, xo1n a snaun1enLno
c1enenn pasnnua coc1on1 1onLko ao apeHenn cansmaannn n o6nac1n anpnHoc1n.
Konc1an1a a 6onLunnc1ae nsmkoa nporpaHHnpoaannn (nanpnHep, a C n HackanL)
ponxna 6m1L nsaec1na na HoHen1 koHnnnnpoaannn, nHe1L rno6anLnyk o6nac1L
anpnHoc1n n oc1aaa1Lcn nensHenno. HensHenneHo nepeHenno Hoxno npncaon1L
snauenne, no 1onLko epnnoxpm; 1aknH o6pasoH, snauenne oc1ae1cn
neonpepenennmH anno1L po HoHen1a amnonnennn nporpaHHm (korpa cospae1cn
o6Lek1, copepxaun a1o snauenne).
Koneuno, nensHenneHme o6Lek1m Hory1 konc1pynpoaa1Lcn npoc1o no cornauennk.
HanpnHep, Hm HoxeH npepycHo1pe1L coo6uenne, sapakuee Hac1L n panr nrpanLno
kap1m, n sa1eH npoc1o nonara1Lcn na 1o, u1o nonLsoaa1enL 6ype1 ncnonLsoaa1L a1o
cpepc1ao nnuL opnaxpm npn cospannn o6Lek1a, a ne pnn nocnepykuero nsHenennn
coc1onnnn o6Lek1a. Bonee npepycHo1pn1enLnme o6Lek1no-opnen1npoaannme
paspa6o1unkn npepnoun1ak1 ne nonara1Lcn na po6pyk aonk caonx no1pe6n1ene n
ncnonLsyk1 HexannsHm nsmka, ko1opme rapan1npyk1 1o, u1o nyxno. Hsmkn
nporpaHHnpoaannn, ko1opme Hm paccHa1pnaaeH, o1nnuak1cn no c1enenn, a kako
onn o6ecneunaak1 nopo6nyk ycnyry. 4.3. MexannsHm cospannn n nnnunannsaunn
Bmue Hm onncann neko1opme 1onkoc1n, aosnnkakune npn cospannn n
nnnunannsaunn o6Lek1oa a nsmkax nporpaHHnpoaannn. B nocnepykunx paspenax
Hm akpa1ue onnueH cnn1akcnc, ncnonLsyeHm a kaxpoH konkpe1noH nsmke.
4.3.1. Cospauue u uuuuanusaun a C++
Hsmk C++ cnepye1 C (a 1akxe Pascal n ppyrnH anrononopo6nmH nsmkaH),
noppepxnaan n aa1oHa1nueckne, n pnnaHnueckne nepeHennme. Aa1oHa1nuecko
nepeHenno naHn1L nasnauae1cn npn axope a 6nok, a ko1opoH naxopn1cn ee
o6Lnanenne, a npn nepepaue ynpaanennn sa npepenm 6noka naHn1L ocao6oxpae1cn.
Opno nsHenenne no cpaanennk c nsmkoH C: o6Lnanenne ne o6nsa1enLno
pasHeuae1cn a nauane 6noka. Epnnc1aennoe 1pe6oaanne coc1on1 a 1oH, u1o
o6Lnanenne ponxno nonann1Lcn po nepaoro ncnonLsoaannn nepeHenno. TeH caHmH
ono Hoxe1 nepepanra1Lcn nenocpepc1aenno k 1ouke, a ko1opo ncnonLsye1cn
nepeHennan.
Henanan nnnunannsaunn o6ecneunaae1cn a nsmke C++ sa cue1 ncnonLsoaannn
konc1pyk1opoa. Kak 6mno o1Heueno a rnaae 3, konc1pyk1op a1o He1op, ko1opm
nHee1 1o xe caHoe nHn, u1o n knacc o6Lek1a. Xo1n onpepenenne konc1pyk1opa a1o
converted to PDF by BoJIoc
uac1L onpepenennn knacca, na caHoH pene konc1pyk1op sapec1aye1cn a npouecce
nnnunannsaunn. Hoa1oHy Hm o6cyxpaeH konc1pyk1opm specL. B uac1noc1n, He1op-
konc1pyk1op aa1oHa1nueckn n nenano amsmaae1cn kaxpm pas, korpa cospae1cn
o6Lek1, npnnapnexaun k coo1ae1c1aykueHy knaccy. O6muno a1o nponcxopn1 npn
o6Lnanennn nepeHenno, no 1akxe n a 1oH cnyuae, korpa o6Lek1 cospae1cn c
noHouLk onepa1opa new nnn korpa no kaknH-1o npnunnaH npnHennk1cn apeHennme
nepeHennme.
HanpnHep, paccHo1pnH cnepykuee onncanne knacca, ko1opoe Hoxe1 6m1L
ncnonLsoaano kak uac1L a6c1pak1noro 1nna pannmx koHnnekcnoe uncno:
class Complex
{
public:
// pasnwun-e xoncipyxiop-
Complex();
Complex(double);
Complex(double,double);
// onepauww naz uwcnamw
...
private:
// nonn zann-x
double realPart;
double imaginaryPart;
};
3pecL sapak1cn 1pn konc1pyk1opa knacca. Kako konc1pyk1op 6ype1 amsaan,
saancn1 o1 apryHen1oa, ncnonLsyeHmx npn cospannn nepeHenno. O6Lnanenne 6es
apryHen1oa
Complex numberOne;
npnaopn1 k amsoay nepaoro konc1pyk1opa. Ero 1eno Hoxe1 amrnnpe1L cnepykunH
o6pasoH:
Complex::Complex()
{
// wnwuwanwswposais nonn nynnmw
realPart=0.0;
imaginaryPart=0.0;
}
3aHe1L1e, u1o 1eno konc1pyk1opa o1nnuae1cn o1 nopHanLnoro He1opa eue n 1eH, u1o
o1cy1c1aye1 nanm 1nn aosapauaeHoro pesynL1a1a. MexannsH konc1pyk1opoa
penae1cn snaun1enLno 6onee HounmH, korpa on koH6nnnpye1cn co cnoco6noc1Lk
nsmka C++ neperpyxa1L nHena qynkun (qynkunn nasmaae1cn neperpyxenno,
ecnn nHek1cn pae nnn 6onee qynkunn c opnnH nHeneH). B nsmke C++
paycHmcnennoc1L npn amsoae neperpyxennmx qynkun paspeuae1cn sa cue1
pasnnun a cnncke apryHen1oa.
D1a oco6ennoc1L konc1pyk1opoa uac1o ncnonLsye1cn, u1o6m o6ecneun1L neckonLko
cnoco6oa nnnunannsaunn. HanpnHep, Hm HoxeH nnnunannsnpoaa1L koHnnekcnoe
uncno, sapaaan 1onLko aeuec1aennyk uac1L, no nnorpa neo6xopnHo sapa1L n
aeuec1aennyk, n HnnHyk uac1n. Onncanne knacca, npnaepennoe amue,
o6ecneunaae1 o6e a1n aosHoxnoc1n. Bo1 kak onn ncnonLsyk1cn:
Complex pi = 3.14159265359;
Complex e (2.7182818285);
Complex i (0.0, 1.0);
Hepame pae nnnunannsaunn amsmaak1 konc1pyk1op, ko1opm nHee1 1onLko opnn
apryHen1. Onncanne 1akoro konc1pyk1opa nHee1 anp:
converted to PDF by BoJIoc
Complex::Complex(double rp)
{
// sazais semecisennyx uacis
realPart = rp;
// sazais nynesyx mnwmyx uacis
imaginaryPart = 0.0;
}
Konc1pyk1opm, ko1opme 1pe6yk1 payx n 6onee apryHen1oa, sanncmaak1cn a 1o xe
qopHe. ApryHen1oH nanne1cn cnncok snauenn, sapaaaeHm npn cospannn
nepeHenno.
Teno konc1pyk1opa uac1o npepc1aanne1 co6o npoc1o nocnepoaa1enLnoc1L
onepa1opoa npncaanaannn. D1n onepa1opm Hory1 6m1L saHenenm nnnunannsa1opaHn
a saronoake qynkunn, nopo6no 1oHy kak a1o cpenano a nnxecnepykueH aapnan1e
konc1pyk1opa. Kaxpm nnnunannsa1op npepc1aanne1 co6o npoc1o nHn nepeHenno
akseHnnnpa; a kpyrnmx cko6kax c1on1 cnncok snauenn, ko1opm ncnonLsye1cn pnn
nnnunannsaunn nepeHenno.
Complex::Complex(double rp) :
realPart(rp), imaginaryPart(0.0)
{ /* zansnenman wnwuwanwsauwn npocio ne ny+na */ }
HepeHennme, onncannme any1pn 6noka, cospak1cn aa1oHa1nueckn npn axope a 6nok
n ynnu1oxak1cn npn amxope ns nero. [nnaHnueckne nepeHennme cospak1cn c
noHouLk knkueaoro cnoaa new, sa ko1opmH cnepye1 nHn knacca o6Lek1a n
apryHen1m, ko1opme ncnonLsyk1cn konc1pyk1opoH. Cnepykun npnHep cospae1
noaoe koHnnekcnoe uncno:
Complex *c;
c = new Complex(3.14159265359, -1.0);
Kpyrnme cko6kn ne nannk1cn o6nsa1enLnmHn, ecnn nnkakne apryHen1m ne
nepepak1cn:
Complex *d = new Complex;
Maccna snauenn Hoxe1 6m1L cospan, ecnn ykasa1L ero pasHep a kaappa1nmx
cko6kax. On amuncnne1cn ao apeHn pa6o1m nporpaHHm n Hoxe1 6m1L nponsaonLnmH
uenouncnennmH ampaxenneH. 3nauennn, cospannme 1aknH cnoco6oH,
nnnunannsnpyk1cn c noHouLk konc1pyk1opa no yHonuannk (1o ec1L
konc1pyk1opa, ne nHekuero apryHen1oa):
Complex *carray = new Complex[27];
HaHn1L pnn pnnaHnueckn pasHeuennmx snauenn ponxna 6m1L nanmH o6pasoH
ocao6oxpena nporpaHHnc1oH c noHouLk onepa1opa delete (nnn delete[] a cnyuae
Haccnaa o6Lek1oa).
3nauennn a nsmke C++ Hory1 6m1L o6Lnanenm nensHenneHmHn c noHouLk
knkueaoro cnoaa const. Takne pannme c1anoan1cn konc1an1aHn nx ne
paspeuae1cn nsHenn1L. HepeHennme akseHnnnpa, onncannme kak konc1an1m,
o6cnyxnaak1cn nnnunannsa1opaHn, nockonLky konc1an1a ne Hoxe1 ac1peua1Lcn a
neao uac1n onepa1opa npncaanaannn.
B nsmke C++ Hory1 onpepenn1Lcn qynkunn, ko1opme aa1oHa1nueckn amsmaak1cn
npn ocao6oxpennn naHn1n, ampenenno nop o6Lek1. Onn nasmaak1cn
pec1pyk1opaHn. [nn aa1oHa1nuecknx nepeHennmx naHn1L ocao6oxpae1cn npn
amxope ns npouepypm, a ko1opo onncana nepeHennan. [nn pnnaHnueckn
pasHeuaeHmx nepeHennmx naHn1L aosapauae1cn c noHouLk onepa1opa delete.
Oynkunn-pec1pyk1op nonyuae1 nHn knacca c npepuec1aykunH snakoH 1nnLpa
(~). Ona ne nHee1 apryHen1oa n pepko amsmaae1cn a nanoH anpe.
converted to PDF by BoJIoc
Cnepykun npoc1o, no ne1pnananLnm npnHep nnnkc1pnpye1 ncnonLsoaanne
konc1pyk1opoa n pec1pyk1opoa. Knacc Trace onpepenne1 npoc1oe cpepc1ao, ko1opoe
Hoxe1 ncnonLsoaa1Lcn pnn 1paccnpoakn amsoaoa npouepyp n qynkun. Konc1pyk1op
knacca nonyuae1 apryHen1 (c1poky), npen1nqnunpykuyk 1ouky 1paccnpoakn, n
neua1ae1 coo6uenne, korpa pnn coo1ae1c1aykuero o6Lek1a Trace ampenne1cn
naHn1L (u1o nponcxopn1 npn axope a npouepypy c ee onncanneH). B1opoe
coo6uenne neua1ae1cn, korpa naHn1L ocao6oxpae1cn, u1o nponcxopn1 npn amxope
ns npouepypm.
class Trace
{
public:
// xoncipyxiop w zecipyxiop
Trace (*char);
~Trace ();
private:
char *text;
};
Trace::Trace(char *t) : text(t)
{
printf("entering %s\n", text);
}
Trace::~Trace()
{
printf("exiting %s\n", text);
}
[nn o1napkn nporpaHHm paspa6o1unk acero nnuL cospae1 a kaxpo npouepype,
nopnexaue 1paccnpoake, onncanne nyc1o nepeHenno 1nna Trace c
coo1ae1c1aykue c1poko nnnunannsaunn. PaccHo1pnH cnepykuyk napy
nopnporpaHH:
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");
...
};
...
}
B amxopnoH nnc1nnre o6Lek1m 1nna Trace nokaxy1 nopnpok amnonnennn
nporpaHHm. Tnnnunm amxopno nnc1nnr Hoxe1 amrnnpe1L kak:
entering procedure A
entering procedure B
entering Large case in procedure B
...
exiting Large case in procedure B
converted to PDF by BoJIoc
exiting procedure B
exiting procedure A
4.3.2. Cospauue u uuuuanusaun a nsmke Java
Ocnoanoe pasnnune Hexpy Java n C++ (a o1nouennn aonpocoa, paccHa1pnaaeHmx a
a1o rnaae) coc1on1 a 1oH, u1o Java ncnonLsye1 aa1oHa1nueckyk c6opky Hycopa,
ocao6oxpan 1eH caHmH nporpaHHnc1a o1 neo6xopnHoc1n sannHa1Lcn ynpaanenneH
naHn1Lk. Bce snauennn aa1oHa1nueckn ocao6oxpak1cn, korpa onn c1anoan1cn
nepoc1ynnmHn pnn cpepm ncnonnennn nporpaHHm.
Bce nepeHennme 1nna o6Lek1 a nsmke Java nepaonauanLno nonyuak1 snauenne
null. O6Lek1m cospak1cn onepa1opoH new. B o1nnune o1 C++ a nsmke Java kpyrnme
cko6kn ponxnm ncnonLsoaa1Lcn a onepa1ope paxe 1orpa, korpa ne 1pe6ye1cn
nnkaknx apryHen1oa:
// coszais nniepxy nwx
Card aCard = new Card (Card.spade, 5);
// coszais xapiy no ymonuanwx
Card bCard = new Card ();
Honn1ne konc1pyk1opa a nsmke Java ananornuno C++ c 1eH ncknkuenneH, u1o
konc1pyk1opm a Java ne noppepxnaak1 nnnunannsa1opoa. B o1nnune o1 C++
konc1pyk1op a nsmke Java Hoxe1 amsmaa1L ppyrne konc1pyk1opm 1oro xe o6Lek1a,
u1o sauac1yk nosaonne1 ncknkun1L ns neckonLknx konc1pyk1opoa o6une
onepa1opm. Konc1pyk1op npn a1oH ponxen amsmaa1Lcn c noHouLk knkueaoro cnoaa
this:
class NewClass
{ NewClass(int i)
{
// s-nonnwis wnwuwanwsauwx 1-ro iwna
...
}
NewClass(int i, int j)
{ // npe+ze scero s-ssais neps-n xoncipyxiop
this(i);
// npozon+wis wnwuwanwsauwx
...
}
}
[ec1pyk1opm a nsmke Java o1nnuak1cn o1 C++. Honn1ne pec1pyk1opa a Java
npepc1aaneno qynkune c nHeneH finalize, y ko1opo ne1 nn apryHen1oa, nn
aosapauaeHoro pesynL1a1a. Oynkunn finalize aa1oHa1nueckn amsmaae1cn cnc1eHo,
korpa o6napyxeno, u1o o6Lek1 6onLue ne ncnonLsye1cn; sa1eH naHn1L, sann1an
o6Lek1oH, noHeuae1cn kak cao6opnan. HporpaHHnc1 ne snae1, korpa 6ype1 (ecnn
aoo6ue 6ype1) amsaan He1op finalize. Hoa1oHy ne cnepye1 nonara1Lcn na a1n He1opm
c 1oukn spennn koppek1noc1n pa6o1m nporpaHHm, no nyxno paccHa1pnaa1L nx kak
cpepc1ao on1nHnsaunn.
Heo6munmH caoc1aoH nsmka Java nanne1cn ncnonLsoaanne 1ekc1oao c1pokn kak
apryHen1a onepa1opa new pnn onpepenennn a npouecce pa6o1m nporpaHHm 1nna
o6Lek1a, ko1opm ponxen 6m1L pasHeuen. B 6onee o6ueH cnyuae c1poka nanne1cn
ampaxenneH, ko1opoe c1pon1cn ao apeHn amnonnennn. [nn nnnunannsaunn anoaL
cospaaaeHoro o6Lek1a 6ype1 ncnonLsoaan konc1pyk1op 6es apryHen1oa.
// nocipowis oxsemnnnp xnacca Complex
a = new ("Comp" + "lex");
Kak Hm anpenn a rnaae 3, nepeHennme, ko1opmH nenLsn noa1opno npncaon1L
snauenne, cospak1cn c knkueamH cnoaoH final. Opnako onn ne nannk1cn no-
converted to PDF by BoJIoc
nac1onueHy nensHennmHn, nockonLky nnu1o ne npenn1c1aye1 nporpaHHnc1y
nepecna1L 1akoHy o6Lek1y coo6uenne, ko1opoe noannne1 na ero any1pennee
coc1onnne. TeH caHmH snauennn 1nna final ne akanaanen1nm snauennnH 1nna const a
nsmke C++, ko1opme nannk1cn rapan1npoaanno nensHennmHn.
final aCard = new Card(Card.spade, 3);
aCard.flip(); // wsmenwis cocionnwe xapi-
4.3.3. Cospauue u uuuuanusaun a Objective-C
Hsmk Objective-C koH6nnnpye1 cnn1akcnc nsmka Smalltalk n onncann nsmka C.
O6muno o6Lek1m onncmaak1cn kak akseHnnnpm 1nna id, 1ak kak 6onee noppo6nan
nnqopHaunn o 1nne pannmx Hoxe1 6m1L nensaec1na anno1L po amnonnennn
nporpaHHm. Oak1nueckoe pasHeuenne o6Lek1a amnonnne1cn (kak n a nsmke
Smalltalk) nepecmnko coo6uennn new o6Lek1y knacca. (3aHe1L1e, u1o id onpepenen
uepes koHanpy typedef n u1o na caHoH pene nepeHennan 1nna id a1o ykasa1enL na
co6c1aenno o6Lek1.)
HepeHennme, onncmaaeHme kak id, acerpa nannk1cn pnnaHnuecknHn. Kak Hm
yanpnH a rnaae 7, a nsmke Objective-C paspeuae1cn 1akxe onncmaa1L nepeHennme c
ncnonLsoaanneH nanoro nHenn knacca. Takne nepeHennme nannk1cn
aa1oHa1nuecknHn, n naHn1L nop nnx ampenne1cn ns c1eka npn axope a npouepypy,
any1pn ko1opo onn onncanm. Coo1ae1c1aenno a1a naHn1L ocao6oxpae1cn npn
amxope ns npouepypm.
Bapnaunn npouecca nnnunannsaunn a nsmke Objective-C o6ecneunaak1cn
qa6pnunmHn He1opaHn. Hopo6no He1opaH knacca a nsmke Smalltalk qa6pnunme
He1opm onpepennk1 qynkunonanLnoc1L konkpe1noro knacca. (D1nH onn o1nnuak1cn
o1 o6munmx He1opoa, ko1opme onpepennk1 qynkunonanLnoc1L akseHnnnpa knacca.
Opnn ns nan6onee cnoxnmx acnek1oa a o6Lek1no-opnen1npoaannoH
nporpaHHnpoaannn pasnnuenne a1pn6y1oa akseHnnnpoa n knaccoa. Mm aepneHcn
k a1oHy aonpocy a cnepykunx rnaaax.)
Ecnn onpepenennk He1opa npepuec1aye1 cnHaon + (nnkc), 1o a1o qa6pnunm
He1op. Me1op, nepep ko1opmH c1on1 snak - (Hnnyc), a1o akseHnnnpnm He1op.
Cnepykun npnHep nnnkc1pnpye1 onpepenenne qa6pnunoro He1opa, ko1opm
cospae1 n nnnunannsnpye1 akseHnnnpm knacca Card.
@implementation Card
{
+ suit: (int) s rank: (int) r
{ self = [Card new ];
suit = s; rank = r;
return self;
}
}
@end
3aHe1L1e, u1o coo6uenne new ncnonLsye1cn pnn 1oro, u1o6m cospa1L noam
akseHnnnp knacca, kak n a nsmke Smalltalk. KoHnnnn1op Objective-C ne ampae1
npepynpexpakunx coo6uenn, ecnn nepeHennme akseHnnnpa ncnonLsyk1cn any1pn
qa6pnunmx He1opoa. Ecnn ac1peuak1cn 1akne ccmnkn, 1o npepnonarae1cn (xo1n a1o
npepnonoxenne n ne npoaepne1cn), u1o snauenne nepeHenno self o1nocn1cn k
npaannLnoHy akseHnnnpy knacca. HockonLky o6muno any1pn qa6pnunmx He1opoa
npen1nqnka1op self ccmnae1cn co6c1aenno na knacc, a ne na akseHnnnp knacca, 1o
nonLsoaa1enL ponxen nepamH penoH nsHenn1L snauenne self nepep poc1ynoH k
nonnH akseHnnnpa. (To1 qak1, u1o 1nn nepeHenno self ne npoaepne1cn na
npaannLnoc1L nepep 1eH, kak ncnonLsyk1cn nepeHennme akseHnnnpa, Hoxe1 6m1L
nc1ounnkoH 1pypno ynoanHmx oun6ok a nporpaHHax na nsmke Objective-C.) B
npepuec1aykueH He1ope 1onLko nocne 1oro, kak nepeHennan self 6mna nsHenena,
ccmnkn na nonn akseHnnnpa suit n rank c1ann o1nocn1Lcn k konkpe1nmH nuekaH.
converted to PDF by BoJIoc
Xo1n o6Lek1m pasHeuak1cn a nsmke Objective-C pnnaHnueckn, cnc1eHa ne
ocyuec1anne1 aa1oHa1nueckoe ynpaanenne naHn1Lk. HonLsoaa1enL c noHouLk
coo6uennn free ponxen npepynpepn1L cnc1eHy, u1o ampenennan o6Lek1y naHn1L
6onLue ne ncnonLsye1cn. Coo6uenne free onpepeneno pnn knacca Object, n noa1oHy
pacnosnae1cn aceHn o6Lek1aHn:
[ aCard free ];
Hsmk Objective-C ne o6ecneunaae1 npnHoro cnoco6a sapannn nensHenneHmx nnn
noc1onnnmx snauenn.
4.3.4. Cospauue u uuuuanusaun a Object Pascal
B nsmke Object Pascal ace o6Lek1m nannk1cn pnnaHnuecknHn n ponxnm cospaaa1Lcn
a nanoH anpe c noHouLk cnc1eHno qynkunn new. Ee apryHen1oH nanne1cn
npen1nqnka1op o6Lek1a. Ananornuno pnn ocao6oxpennn naHn1n, sannHaeHo
o6Lek1oH, ncnonLsye1cn cnc1eHnan nopnporpaHHa dispose, ko1opan amsmaae1cn,
korpa o6Lek1 6onLue ne nyxen. Ynpaanenne naHn1Lk nocpepc1aoH nopnporpaHH
new n dispose o6nsannoc1L nonLsoaa1enn. Hsmk nporpaHHnpoaannn o6ecneunaae1
1onLko HnnnHanLnyk noppepxky. Ecnn ne1 poc1a1ouno naHn1n pnn o6ecneuennn
sanpoca na pasHeuenne o6Lek1a, 1o aosnnkae1 oun6ka amnonnennn. Ecnn penae1cn
nonm1ka ncnonLsoaa1L o6Lek1, ko1opm ne 6mn pasHeuen napnexaunH cnoco6oH,
1o oun6ka amnonnennn nnorpa aosnnkae1, a nnorpa ne1.
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.
3aHe1L1e, u1o npn pa6o1e c o6Lek1aHn ne 1pe6ye1cn onepa1op pasmHenoaannn,
necHo1pn na popc1ao o6Lek1oa n ykasa1ene. [pyrnHn cnoaaHn, ccmnkn na
o6Lek1m ne 1o xe caHoe, u1o nanme ykasa1enn na snauennn (xo1n o6Lek1m n
nannk1cn pnnaHnuecknHn nepeHennmHn).
Hsmk Object Pascal qnpHm Apple o1nnuae1cn o1 ppyrnx o6Lek1no-opnen1npoaannmx
nsmkoa 1eH, u1o ne noppepxnaae1 nenano nnnunannsaunn o6Lek1oa. Kak 1onLko
o6Lek1 cospan (uepes qynkunk new), o6muno nanmH o6pasoH amsmaae1cn
nnnunannsnpykun He1op. D1o nnnkc1pnpye1 npnaepennm amue npnHep.
Hoppepxka saun1m pannmx a nsmke Apple Object Pascal nanne1cn cna6o.
HanpnHep, ne1 cnoco6a npepo1apa1n1L nanm poc1yn k nonnH rp n ip akseHnnnpa
knacca Complex, a 1akxe ne1 rapan1nn 1oro, u1o coo6uenne initial ne amsmaae1cn
6onee opnoro pasa.
converted to PDF by BoJIoc
Hsmk Object Pascal a aepcnn Delphi 6nnxe k C++. Mm anpenn, kak nonn a
onpepenennn knacca Hory1 6m1L cnpn1anm o1 nonLsoaa1enn pnpek1nao private.
Hsmk Delphi Pascal 1akxe noppepxnaae1 konc1pyk1opm. Kak Hm anpenn a
paspene 3.5.1, onn onncmaak1cn a onpepenennn knacca c noHouLk knkueaoro
cnoaa constructor. 3a1eH o6Lek1m cospak1cn ny1eH ncnonLsoaannn He1opa-
konc1pyk1opa a anpe coo6uennn, nocmnaeHoro co6c1aenno knaccy:
aCard := Card.Create(spade, 5);
C noHouLk amsoaa qynkunn-konc1pyk1opa ampenne1cn naHn1L nop noaoe snauenne,
ko1opoe 6ype1 aa1oHa1nueckn nponnnunannsnpoaano. B o1nnune o1 nsmka C++
Delphi Pascal ne paspeuae1 neperpyxa1L qynkunn c opnnakoamHn nHenaHn, no sa1o
nosaonne1 ncnonLsoaa1L neckonLko pasnnunmx konc1pyk1opoa.
Hsmk Delphi Pascal 1akxe noppepxnaae1 pec1pyk1opm. Oynkunn-pec1pyk1op (o6muno
nasmaaeHan Destroy) onncmaae1cn c noHouLk knkueaoro cnoaa destructor. Korpa
ocao6oxpae1cn pnnaHnueckn pasHeuennm o6Lek1, He1op free npoaepne1
npen1nqnka1op self na coanapenne c nil n pnn nenyc1oro o6Lek1a amsmaae1
qynkunk-pec1pyk1op.
type
TClass = class (TObject)
constructor Create(arg : integer);
procedure doTask(value : integer);
destructor Destroy;
end;
destructor TClass.Destroy;
begin
(* nexoiop-e zenciswn *)
end;
Xo1n nsmk Delphi Pascal ne noppepxnaae1 nensHenneHme nnn noc1onnnme nonn
pannmx a nanoH anpe, 1akne snauennn Hory1 6m1L cHopennpoaanm sa cue1
konc1pykunn, nasmaaeHo property (caoc1ao). Hone property onncmaae1cn n
o6pa6a1maae1cn nopo6no nonnH pannmH (poc1yn k snauennk ocyuec1anne1cn no
nHenn, a sanncL uepes koHanpy npncaanaannn). Opnako cnn1akcnc ckpmaae1
nc1nnnm HexannsH poc1yna. Han6onee uac1o n npncaanaanne, n poc1yn k nHenn
ocyuec1annk1cn uepes cneunanLnyk qynkunk, xo1n nnorpa none property
ncnonLsye1cn npoc1o kak nceaponnH pnn ppyroro nonn pannmx. HpnHep none
property npnaepen nnxe. Ecnn onn copepxa1 nnuL knkueayk koHanpy read, 1o
nHek1 c1a1yc 1onLko pnn u1ennn, a ecnn knkueayk koHanpy write 1onLko pnn
sanncn. (3aHe1L1e, u1o snauennnH 1onLko pnn u1ennn paspeuae1cn npncaanaa1L
u1o-nn6o any1pn konc1pyk1opa nnn ppyrnx He1opoa, nHekunx poc1yn k o6nac1n
private knacca.)
type
TnewClass = class (TObject)
property readOnly : Integer
read internalValue;
property realValue : Real
read internalReal
write checkArgument;
private
internalValue : Integer;
internalReal : Real;
procedure CheckArgument (arg : Real);
end;

converted to PDF by BoJIoc
4.3.5. Cospauue u uuuuanusaun a nsmke
Smalltalk
HepeHennme a nsmke Smalltalk nHek1 pnnaHnueckn 1nn pannmx. DkseHnnnp knacca
cospae1cn ny1eH nepecmnkn coo6uennn new o6Lek1y-knaccy, cansannoHy c knaccoH.
Honn1ne o6Lek1a-knacca 6ype1 o6cyxpa1Lcn 6onee noppo6no a rnaae 18. [nn
1ekyunx uene poc1a1ouno ckasa1L, u1o o6Lek1-knacc a1o o6Lek1, ko1opm
nnkancynnpye1 nnqopHaunk o knacce, aknkuan cnoco6 cospannn noamx
akseHnnnpoa.
3nauenne, aosapauaeHoe He1opoH new o6Lek1a-knacca, cyuec1aye1 caHo no ce6e,
xo1n o6muno ono neHepnenno npncaanaae1cn npen1nqnka1opy uepes onepa1op
npncaanaannn, nn6o xe nepepae1cn a kauec1ae apryHen1a. HanpnHep, cnepykuan
koHanpa cospae1 noam akseHnnnp nauero knacca Card:
aCard := Card new.
HonLsoaa1enL Smalltalk ne npepnpnnnHae1 nanmx pec1an pnn ocao6oxpennn
naHn1n. 3nauennn, k ko1opmH 6onLue ne1 poc1yna, aa1oHa1nueckn ocao6oxpak1cn
cnc1eHo. Ecnn paxe nocne c6opkn Hycopa okasmaae1cn neaosHoxnmH
ypoane1aopn1L sanpoc na ampenenne naHn1n, 1o aosnnkae1 oun6ka amnonnennn.
Hsmk Smalltalk o6ecneunaae1 neckonLko HexannsHoa nnnunannsaunn o6Lek1a.
Knacc-He1opm a1o He1opm, accounnpoaannme c konkpe1nmH knacc-o6Lek1oH. Mm
6ypeH paccHa1pnaa1L knacc-o6Lek1m n knacc-He1opm 6onee noppo6no a rnaae 18.
Ceuac poc1a1ouno sna1L, u1o knacc-He1opm Hory1 6m1L ncnonLsoaanm 1onLko kak
coo6uennn pnn knacc-o6Lek1oa. TeH caHmH onn saHennk1 coo6uenne new.
3auac1yk a1n He1opm amsmaak1 new pnn cospannn noaoro o6Lek1a, a sa1eH
amnonnnk1 panLneuyk nnnunannsaunk. HockonLky knacc-He1opm ne Hory1
nanpnHyk o6paua1Lcn k nonnH o6Lek1a, onn nocmnak1 coo6uennn akseHnnnpy,
u1o6m amnonnn1L nnnunannsaunk.
Hnxe npnaepen knacc-He1op suit:rank:. On cospae1 noam akseHnnnp (o6Lek1)
knacca Card n sa1eH amsmaae1 He1op suit:rank: (npepnonoxn1enLno onpepenennm
kak knacc-He1op knacca Card) pnn npncaanaannn snauenn nepeHennmH
akseHnnnpa.
suit: s rank: r S newCard S
" cnepsa coszaem xapiy "
newCard := Card new.
" saiem wnwuwanwswpyem ee "
newCard suit: s rank: r.
" naxoneu, sosspamaem ee "
newCard
1o6m cospa1L noam akseHnnnp knacca Card nanpnHep, ue1aepky 6y6e,
nonLsoaa1enL aaopn1 cnepykuyk koHanpy:
aCard := Card suit: #diamond rank: 4.
Caoc1ao, npnanekakuee annHanne a npnaepennoH He1ope, a1o ncnonLsoaanne
nokanLno nepeHenno (nasmaaeHo apeHenno nepeHenno a nsmke Smalltalk).
HporpaHHnc1 Hoxe1 onncmaa1L apeHennme nepeHennme, npoc1o nepeuncnnn nx
nHena any1pn aep1nkanLnmx uep1ouek Hexpy saronoakoH He1opa n ero 1enoH. D1o
cnpaaepnnao kak pnn knacc-He1opoa, 1ak n pnn o6munmx He1opoa. O6nac1L
anpnHoc1n (cyuec1aoaannn) apeHennmx nepeHennmx aknkuae1 a ce6n 1onLko He1op,
a ko1opoH onn onncanm.
Hsmk Smalltalk ne o6ecneunaae1 npnHoro HexannsHa nnnunannsaunn nensHenneHmx
none. ac1o He1opm, nopo6nme suit:rank:, nHek1 noHe1ky private. D1o
npepnonarae1, u1o 1akne He1opm ne ponxnm amsmaa1Lcn nonLsoaa1ennHn
converted to PDF by BoJIoc
nanpnHyk. Opnako 1akoe orpannuenne amnonnne1cn 1onLko a cnny cornauennn, a
ne amnyxpae1cn co6c1aenno nsmkoH nporpaHHnpoaannn.
[pyran 1exnnka nnnunannsaunn o6Lek1oa a nsmke Smalltalk a1o kackap
coo6uenn. Ero npnHennk1, ecnn neckonLko coo6uenn ponxnm 6m1L nocnanm
opnoHy n 1oHy xe nonyua1enk (kak a1o uac1o 6maae1 npn nnnunannsaunn). [nn
kackapnpoaannn coo6uenn acnep sa nonyua1eneH sanncmaak1 cnncok coo6uenn,
paspenneHmx 1oukaHn c sann1o. HanpnHep, cnepykuee coo6uenne cospae1 noaoe
Hnoxec1ao Set n nnnunannsnpye1 ero snauennnHn 1, 2, 3. PesynL1a1, ko1opm
npncaanaae1cn nepeHenno aSet, a1o noaoe nponnnunannsnpoaannoe Hnoxec1ao.
HcnonLsoaanne kackapoa uac1o nosaonne1 o1kasa1Lcn o1 apeHennmx nepeHennmx:
aSet := Set new add: 1; add: 2; add: 3.
Ynpaxueuun
1. Hannun1e He1op copy pnn knacca Card ns rnaam 3. Me1op ponxen
aosapaua1L noam akseHnnnp knacca Card c nonnHn Hac1n n panra,
nnnunannsnpoaannmHn 1aknHn xe snauennnHn, u1o n y nonyua1enn
coo6uennn copy.
2. Kak 6m am paspa6o1ann nporpaHHnoe cpepc1ao o6napyxennn
necankunonnpoaannoro poc1yna pnn nsmka nporpaHHnpoaannn, ne
o6ecneunaakuero npnHo noppepxkn nensHenneHmx nepeHennmx
akseHnnnpa? (Hopckaska: cnpnuL1e coo1ae1c1aykune pnpek1nam a
koHHen1apnn. HporpaHHnoe cpepc1ao ponxno 6ype1 nx anannsnpoaa1L n
ncnonLsoaa1L.)
3. Mm anpenn paa c1nnn amsoaa He1opoa. Hopxop, ncnonLsyeHm a C++,
ananornuen 1papnunonnoHy amsoay qynkunn. C1nnL, npnnn1m a nsmkax
Smalltalk n Objective-C, paspenne1 apryHen1m knkueamHn cnoaaHn. 1o no
aaueHy Hnennk un1ae1cn nerue? 1o 6onee nnqopHa1nano? Kako nopxop
nyuue saunuen o1 oun6ok? O6ocny1e aauy 1ouky spennn.
4. Kak 6m am paspa6o1ann nporpaHHnoe cpepc1ao pnn pe1ek1npoaannn
onncannmx a paspene 4.2.2 npo6neH, cansannmx c ampenenneH n
ocao6oxpenneH naHn1n?
5. A. AnnenL [Appel 1987] nac1anaae1, u1o npn onpepenennmx o6c1on1enLc1aax
ampenenne naHn1n ns kyun Hoxe1 6m1L 6onee aqqek1nanmH, ueH
ncnonLsoaanne c1eka. Hpou1n1e ero c1a1Lk n cyHHnpy1e knkueame
nonoxennn Annenn. HackonLko aepon1no, u1o cn1yaunn, pnn ko1opmx a1o
y1aepxpenne cnpaaepnnao, ac1pe1n1cn na npak1nke?
6. Hannun1e kopo1koe (paa-1pn a6saua) acce sa nnn npo1na aa1oHa1nuecko
c6opkn Hycopa.
Fnaaa 5

Yve6um npuHep: sapava o aocuHu qepsnx
D1a rnaaa npepc1aanne1 co6o nepam ns neckonLknx npnHepoa (nnn napapnrH, a
nepaonauanLnoH snauennn a1oro cnoaa) nporpaHH, paspa6o1annmx c noHouLk o6Lek1no-
opnen1npoaannmx He1opoa. HporpaHHm poc1a1ouno Hanm, noa1oHy Hm cHoxeH npnaec1n nx
aepcnn pnn kaxporo o6cyxpaeHoro a knnre nsmka. Hocnepykune npnHepm 6ypy1
npepc1aanenm 1onLko na opnoH nsmke.
converted to PDF by BoJIoc
Hocne onncannn sapaun Hm o6cypnH, ueH o6Lek1no-opnen1npoaannoe peuenne o1nnuae1cn
o1 ppyrnx. Inaaa sakanunaae1cn nporpaHHnmH kopoH na pasnnunmx nsmkax.
5.1. 3apava o aocuHu qepsnx
B uaxHa1ax qepsL Hoxe1 6n1L nk6yk qnrypy, c1onuyk a opnoH c nnH pnpy, c1on6ue nnn
pnaronann. 3apaua o aocLHn qepsnx nanne1cn knaccnuecko nornuecko npo6neHo.
Heo6xopnHo pacc1aan1L na uaxHa1no pocke 8 qepse 1ak, u1o6m nn opnn ns nnx ne 6nn
ppyroro. Peuenne npnaepeno na pnc. 5.1, no ono ne epnnc1aennoe. 3apaua o aocLHn qepsnx
uac1o ncnonLsye1cn pnn nnnkc1paunn peuennn nornuecknx sapau n 1exnnkn amuncnenn c
aosapa1oH (cH., nanpnHep, [Griswold 1983, Budd 1987, Berztiss 1990]).
eH o6Lek1no-opnen1npoaannoe peuenne sapaun o aocLHn qepsnx o1nnuae1cn o1 kopa na
o6munoH pnpek1nanoH nsmke nporpaHHnpoaannn? B 1papnunonnoH peuennn pnn onncannn
nosnun qnryp ncnonLsoaanacL 6m kakan-nn6ypL c1pyk1ypa pannmx. HporpaHHa peuana 6m
sapauy, cnc1eHa1nueckn nepe6npan snauennn a a1nx c1pyk1ypax n npoaepnn kaxpyk
nosnunk: ne ypoane1aopne1 nn ona ycnoank?
Moxno npnaec1n sa6aanyk n noyun1enLnyk He1aqopy o pasnnue Hexpy 1papnunonnmH n
o6Lek1no-opnen1npoaannmH peuennnHn. Tpapnunonnan nporpaHHa nopo6na uenoaeky,
naxopnueHycn nap pocko n nepepanrakueHy 6esxnsnennme qnrypm. B o6Lek1no-
opnen1npoaannoH nopxope, nanpo1na, Hm napenneH qnrypm xnsnLk, u1o6m onn peunnn
npo6neHy caHoc1on1enLno. To ec1L aHec1o opnoro cyuec1aa, ynpaannkuero npoueccoH, Hm
paspenneH o1ae1c1aennoc1L sa naxoxpenne peuennn cpepn Hnornx asanHopec1aykunx
aren1oa. D1o nponcxopn1 1ak, kak ecnn 6m uaxHa1nme qnrypm 6mnn opyueanennmHn
cyuec1aaHn, asanHopec1aykunHn Hexpy co6o, ko1opmH nopyueno na1n peuenne.
TaknH o6pasoH, cyunoc1L nauero o6Lek1no-opnen1npoaannoro nopxopa coc1on1 a cospannn
o6Lek1oa, npepc1aannkunx qepse, n napenennn nx cnoco6noc1nHn na1n peuenne. C
1oukn spennn nporpaHHnpoaannn kak nHn1aunn, npnaepenno a rnaae 1, Hm cospaeH Hnp,
onpepennn a neH noaepenne ero o6Lek1oa. Korpa coc1onnne Hnpa c1a6nnnsnpye1cn,
peuenne 6ype1 napeno.

Puc. 5.1. Opno ns peuenn sapaun o aocLHn qepsnx
5.1.1. Cospauue o61ek1oa, pemamux caHux ce6n
Kak onpepenn1L noaepenne kaxporo qepsn, u1o6m rpynna qepse, pa6o1akunx coaHec1no,
Horna npn1n k peuennk? Hepaoe na6nkpenne coc1on1 a 1oH, u1o a nk6oH cnyuae nnkakne
paa qepsn ne Hory1 sannHa1L opnn c1on6eu n, cnepoaa1enLno, ace c1on6um sann1m.
Hoa1oHy Hm HoxeH cpasy npncaon1L kaxpoHy qepsk onpepenennm c1on6eu n caec1n
sapauy k 6onee npoc1o na1n coo1ae1c1aykune c1pokn.
converted to PDF by BoJIoc
1o6m npn1n k peuennk, qepsn ponxnm asanHopec1aoaa1L ppyr c ppyroH. Honna a1o, Hm
HoxeH cpena1L cnepykuee aaxnoe na6nkpenne, ko1opoe ouenL ynpoc1n1 nauy sapauy, a
nHenno: kaxpm qepsL ponxen sna1L 1onLko o caoeH cocepe cneaa. TaknH o6pasoH, pannme
o qepse 6ypy1 coc1on1L ns 1pex snauenn: c1on6eu, ko1opm oc1ae1cn nensHennmH; c1poka,
Hennkuancn a xope peuennn; n cocep cneaa.
OnpepennH npneHneHoe peuenne pnn c1on6ua N kak 1akyk konqnrypaunk c1on6uoa c 1 no
N, a ko1opo nn opnn qepsL ns a1nx c1on6uoa ne 6Le1 ppyroro. KaxpoHy qepsk 6ype1
nopyueno na1n npneHneHoe peuenne pnn ce6n n caonx cocepe cneaa. Mm nonyunH
peuenne ace sapaun, korpa caHm npaam qepsL o1mue1 npneHneHoe peuenne. Onncanne
knacca Queen (OepsL) na CRC-kap1ouke, aknkuan pannme o kaxpoH npepc1aan1ene
(nanoHnnH, u1o a1a nnqopHaunn copepxn1cn na o6opo1e), npnaepeno na pnc. 5.2.

Puc. 5.2. [ae c1oponm CRC-kap1oukn pnn knacca Queen
5.2. Hcnonusoaauue reuepa1opoa
Kak n a ppyrnx nopo6nmx sapauax, peuenne npo6neHm aocLHn qepse coc1on1 ns payx
asanHocansannmx uaroa: renepaunn aosHoxnmx uac1nunmx peuenn n o1cenaanne
peuenn, ne ypoane1aopnkunx panLneunH 1ec1aH. Tako c1nnL anannsa npo6neH nnorpa
nasmaak1 renepaunn n 1ec1npoaanne [Hanson 1981], [Bertiss 1990].
Cnauana paccHo1pnH uar o1cenaannn kak 6onee npoc1o. [nn npoaepkn no1enunanLnoro
peuennn cnc1eHe poc1a1ouno, u1o6m qepsL asnn kooppnna1nyk napy (c1poka-c1on6eu) n
ampan nornueckoe snauenne, ykasmaakuee, 6ype1 nn a1o1 qepsL (nnn kako-nn6ypL ppyro
cneaa) 6n1L a1o none (cH. npnaepennm nnxe anropn1H na nceapokope). Hpouepypa
canAttack ncnonLsye1 1o1 qak1, u1o npn panxennn no pnaronann cHeuenne no c1pokaH
paano cHeuennk no c1on6uaH.
function queen.canAttack(testRow, testColumn) -> boolean
/* nposepxa na iy +e cipoxy */
converted to PDF by BoJIoc
if row = testRow then
return true
/* nposepxa zwaronanw */
columnDifference := testColumn column
if (row + columnDifference = testRow) or
(row columnDifference = testRow)
then return true
/* m- ne mo+em wis, a cocezw? */
return neighbor.canAttack(testRow, testColumn)
end
5.2.1. Huuuanusaun
Mm paspennH sapauy na uac1n. Me1op initialize yc1anaannaae1 neo6xopnHme nauanLnme
ycnoann, a pannoH cnyuae npoc1o sapae1 pannme. [anee o6muno cpasy xe cnepye1 amsoa
He1opa findSolution, naxopnuero peuenne pnn pannoro c1on6ua. HockonLky peuenne uac1o
6ype1 neypoane1aopn1enLnmH pnn nocnepykunx qepse, coo6uenne advance
(npopanny1Lcn) ncnonLsye1cn pnn nepexopa k cnepykueHy peuennk.
OepsL ns c1on6ua N nnnunannsnpye1cn npncaanaanneH noHepa c1on6ua n onpepenenneH
cocepnero qepsn (ns c1on6ua N1). Ha a1oH ypoane anannsa Hm oc1aanH neonpepenennmHn
pec1ann caHoro neaoro qepsn, ne nHekuero cocepa. HpepnonaraeH, u1o qepsn-cocepn yxe
nnnunannsnpoaanm, a1o snaun1, u1o onn naunn asanHno ypoane1aopnkuee peuenne. OepsL
ns 1ekyuero c1on6ua npoc1o c1aan1 ce6n a nepayk c1poky. Hnxe npnaepen anropn1H na
nceapokope.
function queen.initialize(col, neigh) -> boolean
/* wnwuwanwsauwn snauenwn cionua w coceza */
column := col
neighbor := neigh
/* nauwnaem co cipoxw 1 */
row := 1
end
5.2.2. Haxoxpeuue pemeuun
1o6m na1n peuenne, qepsL npoc1o cnpaunaae1 caonx cocepe, Hory1 nn onn ero
a1akoaa1L. Ecnn pa, 1o qepsL npopanrae1cn. Ecnn panLneuee nepepanxenne sanpeueno,
aosapauae1cn snauenne neypaua. Ecnn cocepn coo6uak1, u1o onn a1akoaa1L ne Hory1, 1o
(nokanLnoe) peuenne napeno.
function queen.findSolution -> boolean
/* nposepxa noswuww */
while neighbor.canAttack (row, column) do
if not self.advance then
return false
/* namnw pemenwe */
return true
end
Kak Hm ykasmaann a rnaae 4, nceaponepeHennan self o6osnauae1 nonyua1enn nocnepnero
coo6uennn. B pannoH cnyuae Hm xo1nH, u1o6m qepsL, ko1opoHy nopyueno na1n peuenne,
nocnan coo6uenne advance caHoHy ce6e.
5.2.3. Hpopauxeuue ua cnepymym nosuum
Hpouepypa advance pacnapae1cn na paa cnyuan. Ecnn Hm eue ne poc1nrnn konua, qepsL
npoc1o yaennunaae1 noHep c1pokn na 1. Ecnn qepsL yxe nonpo6oaan ace nosnunn n ne
nauen peuennn, 1o ne oc1ae1cn nnuero, kpoHe kak nonpocn1L y cocepa noaoro peuennn n
naua1L co c1pokn 1.
converted to PDF by BoJIoc
function queen.advance -> boolean
/* npoyem cnezyxmyx cipoxy */
if row < 8 then
begin
row := row + 1
return self.findSolution
end
/* ne mo+em zswraiscn zansme */
/* czswnem coceza x cnezyxmemy pemenwx */
if not neighbor.advance then
return false
/* naunem cnosa c 1-n cipoxw */
row := 1
return self.findSolution
end
TenepL oc1anocL 1onLko naneua1a1L peuenne. D1o penae1cn ouenL npoc1o c noHouLk
He1opa print, ko1opm pekypcnano noa1opne1cn pnn acex cocepe.
procedure print
neighbor.print
write row, column
end
5.3. 3apava o aocuHu qepsnx ua pasnuvumx nsmkax
nporpaHHupoaauun
B a1oH naparpaqe Hm npnaepeH peuenne sapaun o aocLHn qepsnx pnn kaxporo
paccHa1pnaaeHoro nsmka nporpaHHnpoaannn. Cpaann1e 1ekc1m nporpaHH. O6pa1n1e
annHanne na 1o, kak caoc1aennme nsmky uep1m npnanocn1 xn1poyHnme nsHenennn a
peuenne sapaun. B uac1noc1n, npoanannsnpy1e peuennn na nsmkax Smalltalk n Objective-
C, ncnonLsykune cneunanLnm kapaynLnm knacc, n cpaann1e nx c kopoH na Object
Pascal, C++ nnn Java, kaxpm ns ko1opmx ncnonLsye1 nyneao ykasa1enL pnn caHoro
neaoro qepsn n noc1onnno amnyxpen npoaepn1L snauenne ykasa1ene.
5.3.1. 3apava o aocuHu qepsnx: Object Pascal
Onpepenenne knacca pnn sapaun o aocLHn qepsnx na nsmke Object Pascal aepcnn Apple
npnaepeno nnxe. Tonko n aaxno uep1o nanne1cn 1o, u1o a1o onpepenenne pekypcnano
o6Lek1m 1nna Queen copepxa1 none pannmx 1nna Queen. D1o poc1a1ouno ncno nokasmaae1,
u1o o6Lnanenne n ampenenne naHn1n ne o6nsa1enLno cansanm. B npo1nanoH cnyuae pnn
kaxporo o6Lek1a Queen no1pe6oaanocL 6m 6eckoneuno Hnoro naHn1n. Mm cpaannH a1o c
kopoH pnn C++ n pe1anLno paccHo1pnH cansL Hexpy o6LnanenneH n ampenenneH naHn1n a
rnaae 12.
type
Queen = object
(* nonn zann-x *)
row : integer;
column : integer;
neighbor : Queen;
(* wnwuwanwsauwn *)
procedure initialize (col : integer; ngh : Queen);
(* onepauww *)
function canAttack (testRow, testColumn : integer) :
boolean;
function findSolution : boolean;
function advance : boolean;
procedure print;
converted to PDF by BoJIoc
end;
[nn nsmka Delphi onpepenenne knacca 6ype1 o1nnua1Lcn coaceH neHnoro (ono nokasano
nnxe). Bapnan1 nsmka pnn qnpHm Borland nosaonne1 pas6n1L onpepenenne knacca na
o1kpm1yk (public) n sakpm1yk (private) uac1n, a 1akxe po6aan1L qynkunk-konc1pyk1op,
ncnonLsyeHyk aHec1o nporpaHHm 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;
Hceapokop, npnaepennm a npepmpyunx naparpaqax, poc1a1ouno 6nnsok k aapnan1y na
nsmke Pascal c payHn ocnoanmHn o1nnunnHn. Hepaoe: o1cy1c1ane a nsmke Pascal onepa1opa
return. B1opoe: neo6xopnHoc1L cnauana npoaepn1L, ec1L nn y qepsn cocep, nepep nocmnko
coo6uennn a1oHy cocepy. Oynkunn findSolution n advance, npnaepennme nnxe,
peHonc1pnpyk1 a1n pasnnunn. (3aHe1nH, u1o nsmk Delphi Pascal o1nnuae1cn o1 c1anpap1noro
Pascal 1eH, u1o ponyckae1 6mc1poe amnonnenne pnpek1na and (n) n or (nnn), kak n C++.
TaknH o6pasoH, kop na nsmke Delphi Hoxe1 a opnoH ampaxennn o6Lepnnn1L npoaepky
cyuec1aoaannn nenyneaoro cocepa n nocmnky coo6uennn a1oHy cocepy.)
function Queen.findSolution : boolean;
var
done : boolean;
begin
done := false;
findsolution := true;
(* nposepxa noswuww *)
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;
(* npoyem cnezyxmyx cipoxy *)
if row < 8 then
begin
row := row + 1;
advance := self.findSolution;
end
else begin
(* ne mo+em zswraiscn zansme *)
(* czswnyis coceza x cnezyxmemy pemenwx *)
if neighbor <> nil then
if not neighbor.advance then
converted to PDF by BoJIoc
advance := false
else begin
row := 1;
advance := self.findSolution;
end;
end;
end;
Ocnoanan nporpaHHa o1aopn1 naHn1L pnn kaxporo ns aocLHn qepse n nnnunannsnpye1 nx,
onpepennn c1on6eu n cocepa. HockonLky ao apeHn nnnunannsaunn 6ype1 napeno nepaoe
(nokanLnoe) peuenne, qepsn ponxnm 6ypy1 naneua1a1L a1o peuenne. Kop na nsmke Apple
Object Pascal, amnonnnkun a1y sapauy, nokasan nnxe. 3pecL neighbor n i apeHennme
nepeHennme, ncnonLsyeHme ao apeHn nnnunannsaunn, a lastQueen nocnepnn cospannm
qepsL.
begin
neighbor := nil;
for i := 1 to 8 do
begin
(* coszais w wnwuwanwswposais nosoro epsn *)
new (lastQueen);
lastQueen.initial (i, neighbor);
if not lastQueen.findSolution then
writeln(no solution);
(* cam-n nos-n epss cocez npez-zymero *)
neighbor := lastQueen;
end;
(* neuais pemenwn *)
lastQueen.print;
end;
end.
Hpepoc1aannn nanme konc1pyk1opm, o6Lepnnnkune cospanne noamx o6Lek1oa n nx
nnnunannsaunk, nsmk Delphi nosaonne1 nnkanpnpoaa1L opny ns apeHennmx nepeHennmx.
Ocnoanan nporpaHHa na Delphi 6ype1 1akoaa:
begin
lastQueen := nil;
for i := 1 to 8 do
begin
(* coszais w wnwuwanwswposais nosoro epsn *)
lastQueen := Queen.create(i, lastQueen);
lastQueen.findSolution;
end;
// neuais pemenwn
lastQueen.print;
end;
5.3.2. 3apava o aocuHu qepsnx: C++
Han6onee cyuec1aenno pasnnue Hexpy npnaepennmH panee onncanneH anropn1Ha na
nceapokope n 1nnnunmH kopoH pnn C++ nanne1cn nenocpepc1aennoe ncnonLsoaanne
ykasa1ene. Hnxe npnaepeno onncanne knacca Queen. Kaxpm npepc1aan1enL knacca
copepxn1 a ce6e ykasa1enL na ppyroro qepsn. 3aHe1L1e, u1o a o1nnune o1 Object Pascal a
nsmke C++ a1o snauenne ponxno 6m1L o6Lnaneno kak ykasa1enL na o6Lek1, a ne kak caH
o6Lek1.
class Queen
{
public:
converted to PDF by BoJIoc
// xoncipyxiop
Queen (int, Queen *);
// nowcx w neuais pemenwn
bool findSolution();
bool advance();
void print();
private:
// nonn zann-x
int row;
const int column;
Queen *neighbor;
// snyipennwn meioz
bool canAttack (int, int);
};
Kak n a cnyuae nporpaHHm na nsmke Delphi Pascal, Hm peannsoaann He1op initialize a
konc1pyk1ope. Kopo1ko onnueH ero.
Ec1L 1pn nonn pannmx. Uenouncnennoe none column o6Lnaneno kak const. D1o onpepenne1
none kak nensHenneHoe. Tpe1Le none pannmx nHee1 1nn ykasa1enn; ono nn6o copepxn1
nyneaoe snauenne (1o ec1L nn na u1o ne ykasmaae1), nn6o ccmnae1cn na ppyroro qepsn.
HockonLky nnnunannsaunn ocyuec1anne1cn konc1pyk1opoH, ocnoanan nporpaHHa Hoxe1
npoc1o cospa1L aoceHL o6Lek1oa-qepse n no1oH naneua1a1L peuenne. HepeHennan
lastQueen ykasmaae1 na nocnepnero cospannoro qepsn. Bnauane a1o snauenne paano null-
ykasa1enk nn na u1o ne ykasmaae1. 3a1eH a unkne konc1pynpyk1cn aoceHL qepse,
nnnunannsnpyeHmx snauennnHn c1on6ua n npepmpyuero qepsn. Korpa unkn sakonun1cn,
caHm neam qepsL 6ype1 copepxa1L nyneao ykasa1enL a none neighbor, a ace
oc1anLnme ykasa1enn na caonx cocepe; lastQueen ykasmaae1 na caHoro npaaoro qepsn.
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();
}
Mm onnueH 1onLko He1opm, nnnkc1pnpykune aaxnme HoHen1m. Honnoe peuenne Hoxno
nocHo1pe1L a Hpnnoxennn A.
Konc1pyk1op ponxen ncnonLsoaa1L a saronoake nnnunannsnpykuyk konc1pykunk pnn
npncaanaannn snauennn konc1an1e column, 1ak kak sanpeuae1cn npncaanaa1L u1o-nn6o
nonnH, o6LnanennmH kak const. Hnnunannsnpykuan konc1pykunn ncnonLsye1cn n pnn nonn
neighbor, xo1n Hm ne o6Lnannnn a1o none kak konc1an1y.
Queen::Queen(int col, Queen *ngh)
: column(col), neighbor(ngh)
{
row = 1;
}
HockonLky snauenne nepeHenno neighbor Hoxe1 6m1L nn6o ccmnko na qepsn, nn6o
nyneamH, neo6xopnHa npoaepka, npexpe ueH coo6uenne 6ype1 nocnano cocepy. D1o
nokasano a He1ope findSolution. HcnonLsoaanne ykopouennoro amuncnennn nornuecknx
ampaxenn n aosHoxnoc1L amxopa ns cepepnnm npouepypm ynpouak1 kop no cpaanennk c
aepcne na Object Pascal a oc1anLnoH xe onn ouenL noxoxn.
converted to PDF by BoJIoc
bool Queen::findSolution()
{
while (neighbor && neighbor->canAttack(row, column))
if (! advance())
return false;
return true;
}
Me1op advance nopo6nmH xe o6pasoH ponxen npoaepn1L nannune cocepa nepep 1eH, kak
npopanra1L ero k cnepykueHy peuennk. Hocmnan coo6uenne ce6e, kak a1o penae1cn a
pekypcnanoH findSolution, ne o6nsa1enLno ykasmaa1L nonyua1enn.
bool Queen::advance()
{
if (row < 8)
{
row++;
return findSolution();
}
if (neighbor && ! neighbor->advance())
return false;
row = 1;
return findSolution();
}
5.3.3. 3apava o aocuHu qepsnx: Java
Peuenne na nsmke Java ao HnoroH nanoHnnae1 kop na C++. Opnako a Java 1eno He1opa
nnue1cn npnHo na Hec1e, n ykasannn public n private noHeuak1cn a onpepenenne knacca.
Hnxe npnaopn1cn onpepenenne knacca Queen; neko1opme He1opm onyuenm.
class Queen
{
// nonn zann-x
private int row;
private int column;
private Queen neighbor;
// xoncipyxiop
Queen (int c, Queen n)
{
// wnwuwanwswposais nonn zann-x
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)
{
converted to PDF by BoJIoc
. . .
}
public void paint (Graphics g)
{
. . .
}
}
B o1nnune o1 nsmka C++ a Java cansL co cnepykunH qepseH onpepenne1cn kak o6Lek1 1nna
Queen, a ne kak ykasa1enL na Queen. Hepep nocmnko coo6uennn qepsk, onpepenneHoHy
nepeHenno neighbor, amnonnne1cn nanan npoaepka na nenyneaoe snauenne.
HockonLky nsmk Java npepoc1aanne1 6ora1oe Hnoxec1ao rpaqnuecknx nopnporpaHH,
peuenne 6ype1 o1nnua1Lcn o1 npounx 1eH, u1o okonua1enLnan pacc1anoaka qepse 6ype1
napncoaana na akpane. Me1op paint pncye1 qepsn, no1oH nso6paxae1 cocepe.
class Queen
{
. . .
public void paint (Graphics g)
{
// x, y nes-n sepxnwn yron
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);
// saiem pwcyem coceza
if (neighbor != null)
neighbor.paint(g);
}
}
Ipaqnueckne nporpaHHm pncyk1 HanenLkyk kopony, amrnnpnuyk ao1 1ak:

B nsmke Java ne1 nn rno6anLnmx nepeHennmx, nn 6esknaccoamx qynkun. Kak Hm
onnueH 6onee pe1anLno a rnaae 8, nporpaHHa naunnae1cn c onpepenennn nopknacca
cnc1eHnoro knacca Applet n nepeonpepenennn neko1opmx He1opoa. B uac1noc1n, He1op init
ncnonLsye1cn pnn nnnunannsaunn npnnoxennn, a He1op paint pnn nepepncoakn akpana.
Mm 1akxe onpepennH He1op mouseDown, amsmaaeHm npn naxa1nn knonkn Hmun, u1o6m
sac1aan1L nporpaHHy nepexopn1L k cnepykueHy peuennk. HasoaeH knacc nauero
npnnoxennn QueenSolver n onpepennH ero 1ak:
public class QueenSolver extends Applet
{ private Queen lastQueen;
public void init()
{ lastQueen = null;
converted to PDF by BoJIoc
for (int i = 1; i <= 8; i++)
{ lastQueen = new Queen(i, lastQueen);
lastQueen.findSolution();
}
}
public void paint(Graphics g)
{ // pwcyem zocxy
for (int i = 0; i <= 8; i++)
{
g.drawLine(50 * i, 0, 50 * i, 400);
g.drawLine(0, 50 * i, 400, 50 * i);
}
// pwcyem epsen
lastQueen.paint(g);
}
public boolean mouseDown(java.awt.Event evt, int x,
int y)
{
// naniw w naneuaiais cnezyxmee pemenwe
lastQueen.advance();
repaint();
return true;
}
}
3aHe1L1e, u1o knacc npnnoxennn ponxen 6m1L o6Lnanen kak public, u1o6m 6m1L poc1ynnmH
a ocnoano nporpaHHe.
5.3.4. 3apava o aocuHu qepsnx: Objective-C
Onncanne nn1epqeca nauero knacca Queen 1akoao:
@interface Queen : Object
{ /* nonn zann-x */
int row;
int column;
id neighbor;
}
/* meioz- */
- (void) initialize: (int) c neighbor: ngh;
- (int) advance;
- (void) print;
- (int) canAttack: (int) testRow column:
(int) testColumn;
- (int) findSolution;
@end
Kaxpm qepsL nHee1 1pn nonn pannmx: c1poky, c1on6eu n qepsn-cocepa. Hocnepnee
o6Lnaneno c 1nnoH id. D1o ykasmaae1, u1o snauenneH nepeHenno 6ype1 o6Lek1, xo1n n ne
o6nsa1enLno qepsL.
B pec1an1enLnoc1n Hm HoxeH ncnonLsoaa1L 6ec1nnoayk cyunoc1L nepeHennmx a nsmke
Objective-C ce6e ao 6naro. HpnHennH 1exnnky, ko1opan neaosHoxna nnn, no kpane Hepe,
ne 1ak npoc1a a nsmkax co c1pornHn orpannuennnHn na 1nn (1aknHn, kak C++ nnn Object
Pascal). BcnoHnnH, u1o caHm neam qepsL ne nHee1 cocepa. B peuennn na nsmke C++
npnsnakoH a1oro cnyxnno nyneaoe snauenne nepeHenno, copepxaue ykasa1enL na
cocepa, pnn caHoro neaoro qepsn. B pannoH peuennn Hm aHec1o a1oro cospaeH noam knacc
kapaynLnoe (sentinel) snauenne. CaHm neam qepsL 6ype1 ykasmaa1L na a1o kapaynLnoe
snauenne, 1eH caHmH o6ecneunaan pec1an1enLnoro cocepa kaxpoHy qepsk.
converted to PDF by BoJIoc
KapaynLnme aennunnm uac1o ncnonLsyk1cn kak Hapkepm konua; nx Hoxno o6napyxn1L a
anropn1Hax, pa6o1akunx co cnnckaHn, 1aknHn kak nau cnncok qepse. Pasnnune Hexpy
o6Lek1no-opnen1npoaannmH kapaynoH n 6onee 1papnunonnmHn npoaepkaHn coc1on1 a
1oH, u1o nepam Hoxe1 6m1L ak1nanmH y nero ec1L noaepenne, 1o ec1L on Hoxe1
o1aeua1L na sanpocm.
Kakoao ponxno 6m1L noaepenne kapaynLnmx aennunn? BcnoHnnH, u1o ccmnkn na cocepa a
naueH anropn1He ncnonLsyk1cn paonko. Bo-nepamx, u1o6m y6epn1Lcn, u1o nn1epecykuan
nac nosnunn ne a1akoaana; kapaynLnan aennunna na 1ako sanpoc acerpa ponxna o1aeua1L
o1pnua1enLno, nockonLky ona ne Hoxe1 nnuero 6n1L. B1opoe ncnonLsoaanne ccmnok na
cocepa a pekypcnanoH amsoae npouepypm print. B a1oH cnyuae kapaynLnan aennunna
npoc1o nnuero ne penae1, 1ak kak ne nHee1 nnkako nnqopHaunn o peuennn.
O6Lepnnnn aHec1e ace a1n paccyxpennn, npnxopnH k cnepykue peannsaunn nauero
kapaynLnoro qepsn:
@implementation SentinelQueen : Object
- (int) advance
{
/* nwuero ne zenaem */
return 1;
}
- (int) findSolution
{
/* nwuero ne zenaem */
return 1;
}
- (void) print
{
/* nwuero ne zenaem */
}
- (int) canAttack: (int) testRow column:
(int) testColumn;
{
/* ne mo+em aiaxosais */
return 0;
}
@end
B nonnoH peuennn ec1L uac1L, peannsykuan SentinelQueen, no k ne ne1 nn1epqeca. D1o
ponyc1nHo, xo1n koHnnnn1op ampac1 npepynpexpenne, nockonLky 1akan cn1yaunn
neckonLko neo6muna.
HcnonLsoaanne kapaynLnoro o6Lek1a nosaonne1 He1opaH knacca Queen npoc1o nocmna1L
coo6uennn cocepy 6es npepaapn1enLnoro nsyuennn, nanne1cn nn pannm qepsL caHmH
neamH. HanpnHep, a He1ope canAttack:
- (int) canAttack: (int) testRow column:
(int) testColumn
{ int columnDifference;
/* mo+em wis iy +e cipoxy */
if (row == testRow)
return 1;
columnDifference = testColumn column;
if ((row + columnDifference == testRow) ||
(row columnDifference == testRow))
return 1;
return [ neighbor canAttack:testRow column:
testColumn ];}
converted to PDF by BoJIoc
Bny1pn He1opa nocmnka coo6uennn caHoHy ce6e ocyuec1anne1cn c noHouLk
nceaponepeHenno self:
- (void) initialize: (int) c neighbor: ngh
{
/* ycianoswis nonn xonciani- */
column = c;
neighbor = ngh;
row = 1;
}
- (int) findSolution
{
/* uwxn, noxa ne nanzem pemenwe */
while ([neighbor canAttack: row and: column ])
if (! [self advance])
return 0; /* sosspamaem false */
return 1; /* sosspamaem true */
}
Oc1anLnme He1opm ananornunm n specL ne paccHa1pnaak1cn.
5.3.5. 3apava o aocuHu qepsnx: Smalltalk
Peuenne sapaun o aocLHn qepsnx na nsmke Smalltalk ao Hnornx o1nouennnx noxoxe na
Objective-C. Kak n nocnepnn, nsmk Smalltalk yun1maae1 1o1 qak1, u1o caHm neam qepsL
ne nHee1 cocepa, aaopn cneunanLnm kapaynLnm (sentinel) knacc. Ero epnnc1aennan uenL
a 1oH, u1o6m npepoc1aan1L nonyua1enn coo6uenn, nocnannmx caHmH neamH qepseH.
KapaynLnan aennunna nanne1cn epnnc1aennmH npepc1aan1eneH knacca SentinelQueen
(nopknacc knacca Queen), ko1opm peannsye1 cnepykune 1pn He1opa:
advance
" xapaynsn-n epss ne aiaxyei "
false
canAttack: row column: column
" xapaynsn-n epss ne mo+ei aiaxosais "
false
result
" sosspamaem nycion cnwcox s xauecise pesynsiaia "
List new
Epnnc1aennoe pasnnune Hexpy aepcnnHn na Objective-C n Smalltalk a 1oH, u1o nporpaHHa
na nsmke Smalltalk aosapauae1 pesynL1a1 kak cnncok aennunn, a ne amaopn1 nx na neua1L.
Texnnka amaopa na neua1L poaonLno xn1po yc1poena a Smalltalk n pasnnuae1cn o1
peannsaunn k peannsaunn. Bosapauan cnncok, Hm HoxeH o1penn1L a1n pasnnunn o1
ocnoanmx He1opoa.
Knacc Queen nanne1cn nopknaccoH knacca Object. Hpepc1aan1enn knacca Queen copepxa1
1pn any1pennne nepeHennme: row (c1poka), column (c1on6eu) n neighbor (cocep).
Hnnunannsaunn ocyuec1anne1cn a He1ope setColumn : neighbor:.
setColumn: aNumber neighbor: aQueen
" wnwuwanwsauwn nonen zann-x "
column := aNumber.
neighbor := aQueen.
row := 1.
Me1op canAttack o1nnuae1cn o1 ananornunoro He1opa pnn nsmka Objective-C 1onLko
cnn1akcncoH:
canAttack: testRow column: testColumn |
converted to PDF by BoJIoc
columnDifference |
columnDifference := testColumn column.
((row = testRow) or:
[ row + columnDifference = testRow]) or:
[ row columnDifference = testRow])
ifTrue: [ true ].
neighbor canAttack: testRow column: testColumn
BHec1o 1oro u1o6m npoaepn1L ycnoane na o1pnuanne, nsmk Smalltalk npepoc1aanne1 nanm
onepa1op ifFalse, ncnonLsyeHm a He1ope advance:
advance
" cnauana nonpoyem cnezyxmyx cipoxy "
(row < 8)
ifTrue: [ row := row + 1. self findSolution].
" ne mo+em zswraiscn zansme, czswraem coceza "
(neighbor advance)
ifFalse: [ false ].
" naunem co cipoxw 1 "
row := 1.
self findSolution
Unkn while a nsmke Smalltalk ponxen ncnonLsoaa1L 6nok npn npoaepke ycnoann, kak a
cnepykueH npnHepe:
findSolution
[ neighbor canAttack: row column: column ]
whileTrue: [ self advance ifFalse: [ false ] ].
true
[nn nonyuennn cnncka okonua1enLnmx nosnun ncnonLsye1cn pekypcnanm He1op.
BcnoHnnH, u1o kapaynLnan aennunna cospae1 nyc1o cnncok a o1ae1 na coo6uenne result:
result
neighbor result; addLast: row
Peuenne 6ype1 nonyueno c noHouLk amsoaa cnepykuero He1opa, ne nannkuerocn uac1Lk
knacca Queen n o1nocnuerocn k neko1opoHy ppyroHy knaccy, nanpnHep, 1akoHy kak Object:
solvePuzzle | lastQueen |
lastQueen := SentinelQueen new.
1 to: 8 do: [:i | lastQueen := (Queen new)
setColumn: i neighbor: lastQueen.
lastQueen findSolution ].
lastQueen result
Peuenne sapaun o aocLHn qepsnx, noc1poennoe 6es npnHenennn kapaynLno aennunnm,
onncano a Hoe 6onee panne knnre no nsmky Smalltalk [Budd 1987].
Ynpaxueuun
1. HsHenn1e nporpaHHm 1ak, u1o6m onn ampaaann ace aosHoxnme peuennn, a ne
1onLko opno. CkonLko cyuec1aye1 peuenn sapaun o aocLHn qepsnx? CkonLko ns
nnx nannk1cn noaopo1aHn ppyrnx? Kak Hoxno o16pocn1L noaopo1m?
2. Kak am Hoxe1e o6Lncnn1L, u1o kapaynLnm knacc a nsmkax ObjectiveC n Smalltalk
ne npepoc1aanne1 caok aepcnk He1opa findSolution, necHo1pn na 1o u1o coo6uenne
findSolution nocmnae1cn cocepy a He1ope advance?
3. HpepnonoxnH, Hm o6o6unH sapauy o aocLHn qepsnx kak npo6neHy N qepse.
3apaua: kak pacnonoxn1L N qepse na uaxHa1no pocke N r N? Kak nsHenn1cn
nporpaHHm? Hcno, u1o cyuec1ayk1 N, pnn ko1opmx ne1 peuenn (nanpnHep, N=2
converted to PDF by BoJIoc
nnn N=3). 1o 6ype1 a a1oH cnyuae c naunHn nporpaHHaHn? Kak Hoxno
oprannsoaa1L 6onee ocHmcnennm amaop o1ae1a?
4. HcnonLsyn rpaqnueckne aosHoxnoc1n aaue cnc1eHm, nsHenn1e opny ns nporpaHH
1ak, u1o6m ona pnnaHnueckn nso6paxana na uaxHa1no pocke nosnunn kaxporo
qepsn no Hepe caoe pa6o1m. Kakne uac1n kopa ponxnm sna1L o6 yc1poc1ae
amaopa?
Fnaaa 6

Yve6um npuHep: urpa Bununpp
Bo a1opoH npnHepe Hm noc1ponH npoc1yk nHn1aunk 6nnLnppnoro c1ona. HporpaHHa
nanncana na nsmke Object Pascal pnn Macintosh
1
. Kak n a cnyuae c aoceHLk qepsnHn,
paspa6o1ka penae1 ynop na cospanne aa1onoHnmx aren1oa, asanHopec1aykunx Hexpy
co6o pnn poc1nxennn xenaeHoro pesynL1a1a.
6.1. 3neHeu1m 6ununppa
[nn nonLsoaa1enn 6nnLnppnm c1on npepc1aanne1 co6o okno, copepxauee npnHoyronLnnk
c nysaHn no yrnaH, 15 uepnmx uapoa n 1 6enm uap. Haxa1neH knonkn Hmun
nonLsoaa1enL nHn1npye1 ypap kneH no uapy, coo6uan eHy neko1opyk aneprnk. Lap
panxe1cn a c1opony, npo1naononoxnyk ykasa1enk Hmun. Honyuna aneprnk, uap naunnae1
ka1n1Lcn, o1ckaknaan o1 c1enok, ypapnn ppyrne uapm, n, nakoneu, nonapae1 a nysy. Korpa
opnn uap c1anknaae1cn c ppyrnH, uac1L aneprnn nepaoro nepepae1cn a1opoHy n a
pesynL1a1e nanpaanenne panxennn o6onx uapoa Henne1cn.


1
HporpaHHa BnnLnpp 6mna paspa6o1ana na koHnLk1epe PowerPC Macintosh c
ncnonLsoaanneH koHnnnn1opa CodeWarrior Pascal aepcnn 1.1. B aepcnn pnn
PowerPC panxenne c1onL 6mc1po, u1o n peunn ac1aan1L a npouepypy Ball.update
saHepnnkun unkn, neckonLko pas amsmaakun He1op draw. Hrpa,
peannsoaannan nporpaHHo ns a1o rnaam, ne coo1ae1c1aye1 nnkako nac1onue
converted to PDF by BoJIoc
nrpe. D1o ne nyn, a1o ne 6nnLnpp, a1o npoc1o panxenne uapoa no c1ony co
c1enkaHn n nysaHn.
6.2. Fpaquveckue o61ek1m
Ocnoay nHn1aunn coc1aannk1 1pn cnncka rpaqnuecknx o6Lek1oa, npepc1aannkunx c1enkn,
nysm n uapm. Kaxpm rpaqnueckn o6Lek1 aknkuae1 a ce6n none ccmnkn n none,
onncmaakuee Hec1ononoxenne o6Lek1a na akpane
1
.
Mm aaenn ynpouakuee npepnonoxenne, u1o ace rpaqnueckne o6Lek1m sannHak1
npnHoyronLnyk o6nac1L. D1o, koneuno, coaepuenno neaepno pnn kpyrnmx o6Lek1oa
nanopo6ne uapa. Bonee peannc1nuno anL1epna1nao 6mno 6m nannca1L npouepypy,
onpepennkuyk, nepeceknncL nn paa uapa, na ocnoae nx pec1an1enLno reoHe1pnn. Ho
cnoxnoc1L npouepypm 1onLko o1anekna 6m nac o1 1oro rnaanoro, uero Hm xo1nH po6n1Lcn,
npnaopn a1o1 npnHep, a nHenno nonn1L cnoco6 napenennn o6Lek1oa o1ae1c1aennoc1Lk sa nx
noaepenne. Kaxpm rpaqnueckn o6Lek1 snae1 ne 1onLko kak nso6paxa1L ce6n, no n kak
asanHopec1aoaa1L c ppyrnHn o6Lek1aHn naue Hopenn Hnpa.
6.2.1. Fpaquvecku o61ek1 Wall (c1euka)
HepamH ns naunx 1pex rpaqnuecknx o6Lek1oa nanne1cn c1enka Wall. Ona onpepenne1cn
cnepykunH onncanneH knacca:
Wall = object
(* nonn zann-x *)
link : Wall;
region : Rect;
(* yron oicxoxa mapos *)
convertFactor : real;
(* wnwuwanwswpyxman ynxuwn *)
procedure initialize
(left, top, right, bottom : integer; cf : real);
(* wsopa+enwe cienxw *)
procedure draw;
(* coomenwe cienxe, uio o nee yzapwncn map *)
procedure hitBy (aBall : Ball);
end;

1
Hencno, kypa noHec1n1L nsyuenne a1oro npnHepa. C opno c1oponm, un1a1enk
aaxno kak Hoxno 6mc1pee yanpe1L npnHenenne o6Lek1nmx npnnunnoa; noa1oHy
xena1enLno, u1o6m a1o1 npnHep ac1pe1nncn a knnre nopanLue. C ppyro c1oponm,
a1a nporpaHHa 1onLko amnrpana 6m o1 6onee nsoupenno 1exnnkn, ko1opan
o6cyxpae1cn nnxe. B uac1noc1n, rpaqnueckne o6Lek1m 6mno 6m nyuue
npepc1aann1L a anpe nepapxnn nacnepoaannn, kak onncano a rnaae 7. KpoHe 1oro,
cun1ae1cn nnoxnH c1nneH nporpaHHnpoaannn pasHeuenne ccmnounmx none a
o6nac1n pannmx o6Lek1oa, o6Lepnnennmx a cnncok; nyuue o1penn1L kon1enep n
aneHen1m cnncka. Peuenne a1nx npo6neH ne1pnananLno n copepxn1 cnoxnoc1n.
Mm o6cypnH knaccm kon1enepoa a rnaae 15.
Hone link (ccmnka) cnyxn1 pnn noppepxannn cnncka o6Lek1oa Wall. Hnnunannsnpykun
He1op npoc1o sapae1 Hec1ononoxenne (region) c1enkn n napaHe1p o1ckoka (convert factor):
procedure Wall.initialize
(left, top, right, bottom : integer; cf : real);
begin
(* wnwuwanwsauwn convertFactor *)
convertFactor := cf;
(* ycianoswis onacis znn cien- *)
converted to PDF by BoJIoc
SetRect (region, left, top, right, bottom);
end;
C1enka Hoxe1 6m1L napncoaana npoc1o kak cnnouno npnHoyronLnnk. D1o amnonnne1cn
c1anpap1no npouepypo pnn Macintosh:
procedure Wall.draw;
begin
PaintRect (region);
end;
CaHoe nn1epecnoe nponcxopn1 co c1enko, korpa o nee ypapne1cn uap. Hanpaanenne ero
panxennn nsHenne1cn, ocnoamaancL na snauennn napaHe1pa convertFactor pnn c1enkn.
(HepeHennan convertFactor paana nnn nynk, nnn pi, a saancnHoc1n o1 1oro, ropnson1anLnan
c1enka nnn aep1nkanLnan.) B pesynL1a1e c1onknoaennn uap 6ype1 panra1Lcn a noaoH
nanpaanennn.
procedure Wall.hitBy (aBall : Ball);
begin
(* oiionxnem map oi cienxw *)
aBall.setDirection(convertFactor aBall.direction);
end;
6.2.2. Fpaquvecku o61ek1 Hole (nysa)
Hole (nysa) onpepenne1cn cnepykunH onncanneH knacca:
Hole = object
(* nonn zann-x *)
link : Hole;
region : Rect;
(* wnwuwanwswpyxman ynxuwn *)
procedure initialize (x, y : integer);
(* wsopa+enwe nynxw *)
procedure draw;
(* coomenwe nyse, uio s nee nonan map *)
procedure hitBy (aBall : Ball);
end;
Kak n a cnyuae c1enok, nnnunannsaunn n nso6paxenne nysm a ocnoanoH coc1on1 ns amsoaa
coo1ae1c1aykunx c1anpap1nmx nopnporpaHH:
procedure Hole.initialize (x, y : integer);
begin
(* onpezenwis onacis c uenipom s x, y *)
SetRect(region, x-5, y-5, x+5, y+5);
end;
procedure Hole.draw;
begin
PaintOval (region);
end;
BonLun nn1epec npepc1aanne1 nponcxopnuee npn ypape uapa o nysy. Ec1L paa cnyuan.
Ecnn uap okasancn 6enmH (on npen1nqnunpye1cn rno6anLno nepeHenno CueBall), 1o on
aosapauae1cn nasap a nrpy na onpepenennyk nosnunk. B oc1anLnmx cnyuanx uap
nnuae1cn aneprnn n y6npae1cn co c1ona a cneunanLnyk o6nac1L.
procedure Hole.hitBy (aBall : Ball);
begin
(* ocianoswis map; ypais ero co ciona *)
aBall.energy := 0.0;
aBall.erase;
(* nepezswnyis map *)
converted to PDF by BoJIoc
if aBall = CueBall then
aBall.setCenter(50.100);
else begin
saveRack := saveRack + 1;
aBall.setCenter (10 + saveRack * 15, 250);
end;
(* nepepwcosais map *)
aBall.draw;
end;
6.2.3. Fpaquvecku o61ek1 Ball (map)
HocnepnnH rpaqnuecknH o6Lek1oH nanne1cn uap, onpepenneHm cnepykunH onncanneH
knacca:
Ball = object
(* nonn zann-x znn mapa *)
link : Ball;
region : Rect;
direction : real; (* nanpasnenwe s pazwanax *)
energy : real;
(* wnwuwanwswpyxman ynxuwn *)
procedure initialize (x, y : integer);
(* meioz- *)
procedure draw;
procedure erase;
procedure update;
procedure hitBy (aBall : Ball);
procedure setDirection (newDirection : real);
(* sosspamaxi x, y xoopzwnai- uenipa mapa *)
function x : integer;
function y : integer;
end;
B pononnenne k nonnH ccmnkn (link) n Hec1ononoxennn (region), o6unHn c oc1anLnmHn
o6Lek1aHn, uap nHee1 paa noamx nonn pannmx: direction (nanpaanenne), amuncnennoe a
papnanax, n energy (aneprnn), npepc1aannkuee co6o aeuec1aennoe snauenne. Kak n a
cnyuae nysm, uap nnnunannsnpye1cn apryHen1aHn, onncmaakunHn kooppnna1m ero uen1pa.
HepaonauanLno uap ne nHee1 aneprnn n ero nanpaanenne nyneaoe.
procedure Ball.initialize (x, y : integer);
begin
SetRect (region, x-5, y-5, x+5, y+5);
setDirection (0.0);
energy := 0.0;
end;
Lap nso6paxae1cn nn6o okpyxnoc1Lk, nn6o cnnounmH kpyroH, a saancnHoc1n o1 1oro,
nanne1cn nn on 6enmH nnn ne1.
procedure Ball.draw;
begin
if self = CueBall then
(* pwcyem oxpy+nocis *)
FrameOval (region);
else
(* pwcyem xpyr *)
PaintOval (region);
end;
procedure Ball.erase;
converted to PDF by BoJIoc
begin
EraseRect (region);
end;
Me1op update ncnonLsye1cn pnn nsHenennn nosnunn uapa. Ecnn on nHee1 saHe1nyk
aneprnk, 1o cnerka cpanrae1cn, a sa1eH npoaepne1, ne sapen nn on ppyro o6Lek1.
Ino6anLnan nepeHennan ballMoved yc1anaannaae1cn a true, ecnn kako-nn6o uap na c1one
cpannyncn. Ecnn uap sapen ppyro o6Lek1, uap coo6uae1 o6 a1oH o6Lek1y. Coo6uennn
6maak1 1pex anpoa; onn coo1ae1c1ayk1 ypapaH no nyse, c1enke n ppyrnH uapaH.
Hacnepoaanne, ko1opoe Hm nsyuaeH a rnaae 7, npepoc1aanne1 He1opm o6Lepnnennn a1nx
1pex 1ec1oa a opnn unkn.
procedure Ball.update;
var
hptr : Hole;
wptr : Wall;
bptr : Ball;
dx, dy : integer;
theIntersection : Rect;
begin
if energy > 0.5 then
begin
ballMoved := true;
(* yzanwis map *)
erase;
(* ymensmwis oneprwx *)
energy := energy 0.05;
(* czswnyis map *)
dx := trunc(5.0 * cos(direction));
dy := trunc(5.0 * sin(direction));
offsetRect(region, dx, dy);
(* nepepwcosais map *)
draw;
(* nposepwis, ne nonanw nw s nysy *)
hptr := listOfHoles;
while (hptr <> nil) do
if SectRect (region, hptr.region, theIntersection)
then
begin
hptr.hitBy(self);
hptr := nil;
end
else
hptr := hptr.link;
(* nposepwis, ne yzapwnwcs nw s cienxy *)
wptr := listOfWalls;
while (wptr <> nil) do
if SectRect (region, wptr.region, theIntersection)
then
begin
wptr.hitBy(self);
wptr := nil;
end
else
wptr := wptr.link;
(* nposepwis, ne yzapwnw nw map *)
bptr := listOfBalls;
while (bptr <> nil) do
converted to PDF by BoJIoc
if SectRect (region, bptr.region, theIntersection)
then
begin
bptr.hitBy(self);
bptr := nil;
end
else
bptr := bptr.link;
end;
end;
Korpa opnn uap ypapne1 ppyro, aneprnn nepaoro penn1cn nononaH Hexpy nnHn. Takxe
Hennk1cn nanpaanennn panxennn o6onx uapoa.
procedure Ball.hitBy (aBall : Ball);
var
da : real;
begin
(* ymensmwis oneprwx yzapnxmero mapa nanonoswny *)
aBall.energy := aBall.energy / 2;
(* w zoaswis ee x namemy mapy *)
energy := energy + aBall.energy;
(* ycianoswis name nosoe nanpasnenwe *)
setDirection(hitAngle(self.x aBall.x,
self.y aBall.y);
(* w nanpasnenwe yzapnxmero mapa *)
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;
if (dy < 0) then
na := -na;
hitAngle := na;
end;
6.3. Ocuoauan nporpaHHa
B npepmpyueH naparpaqe onncmaanncL c1a1nueckne xapak1epnc1nkn nporpaHHm.
[nnaHnka naunnae1cn npn naxa1nn knonkn Hmun. Hpn a1oH amsmaae1cn cnepykuan
npouepypa:
procedure mouseButtonDown (x, y : integer);
var
bptr : Ball;
begin
(* npwcsowm enomy mapy nexoiopyx oneprwx *)
CueBall.energy := 20.0;
(* w nanpasnenwe *)
converted to PDF by BoJIoc
CueBall.setDirection(hitAngle (CueBall.x x,
CueBall.y y));
(* wsmenenwn npowcxozni, noxa zsw+eicn xoin - ozwn map *)
ballMoved := true;
while ballMoved do
begin
ballMoved := false;
bptr := listOfBalls;
while bptr <> nil do
begin
bptr.update;
bptr := bptr.link;
end;
end;
end;
Oc1aauancn uac1L nporpaHHm o1nocn1enLno npnHonnnena n ne npepc1aanena specL. BecL
1ekc1 naxopn1cn a Hpnnoxennn B. Ocnoanan uac1L kopa cansana c nnnunannsaune noamx
o6Lek1oa n oprannsaune unkna oxnpannn co6m1nn, 1o ec1L pec1ann nonLsoaa1enn.
Inaanoe nonn1L 1o, kak 6mno peuen1pannsoaano ynpaanenne n kak caHn o6Lek1m 6mnn
napenenm aosHoxnoc1nHn annn1L na xop amnonnennn nporpaHHm. Bce, u1o nponcxopn1 npn
naxa1nn knonkn Hmun, a1o napenenne 6enoro uapa neko1opo aneprne. B panLneueH
HopenL pykoaopc1aye1cn ncknkun1enLno asanHopec1aneH uapoa.
6.4. Hcnonusoaauue uacnepoaauun
B rnaae 1 Hm onncann nacnepoaanne neqopHanLno, a a rnaae 7 o6cypnH, kak ono pa6o1ae1
a kaxpoH ns paccHa1pnaaeHmx naHn nsmkoa. 3pecL Hm noncnnH, kak nacnepoaanne
ncnonLsye1cn pnn ynpouennn nHn1aunn 6nnLnppa. [yHae1cn, u1o un1a1enk nyuue
aepny1Lcn k a1oHy naparpaqy nocne osnakoHnennn c o6unHn nonoxennnHn o nacnepoaannn
a cnepykue rnaae.
HepamH uaroH a ncnonLsoaannn nacnepoaannn a naue nHn1aunn 6nnLnppa nanne1cn
onncanne o6uero knacca rpaqnueckn o6Lek1. On nopopn1 1pex no1oHkoa: uapm, c1enkn
n nysm. Popn1enLckn knacc onpepenne1cn cnepykunH o6pasoH:
GraphicalObject = object
(* nonn zann-x *)
link : GraphicalObject;
region : Rect;
(* wnwuwanwswpyxman ynxuwn *)
procedure setRegion (left, top, right, bottom :
integer);
(* onepauww, s-nonnnem-e rpawuecxwmw o+exiamw *)
procedure draw;
procedure erase;
procedure update;
function intersect (anObj : GraphicalObject) :
boolean;
procedure hitBy (anObj : GraphicalObject);
end;
Hnnunannsnpykuan qynkunn setRegion npoc1o yc1anaannaae1 o6nac1L, sannHaeHyk
o6Lek1oH. Me1opm draw n update nnuero ne penak1, 1ak kak nx qak1nueckoe noaepenne
onpepeneno a pouepnnx knaccax. HporpaHHa erase ounuae1 o6nac1L, sannHaeHyk
o6Lek1oH. intersect aosapauae1 snauenne true, ecnn o6Lek1-apryHen1 nepecekae1cn c
paccHa1pnaaeHmH o6Lek1oH. H nakoneu, He1op hitBy 1akxe nepeonpepenne1cn a pouepnnx
knaccax. Xo1n panrak1cn 1onLko uapm n, cnepoaa1enLno, apryHen1oH a1o qynkunn acerpa
converted to PDF by BoJIoc
6ype1 uap, 1o1 qak1, u1o knacc Ball eue ne onpepenen, osnauae1, u1o Hm ponxnm o6Lnan1L
apryHen1 kak nHekun 6onee o6un 1nn GraphicalObject:
procedure GraphicalObject.setRegion
(left, top, right, bottom : integer);
begin
SetRect(region, left, top, right, bottom);
end;
procedure GraphicalObject.draw;
begin
(* nepeonpezenneicn s zouepnem xnacce *)
end;
procedure GraphicalObject.erase;
begin
EraseRect (region);
end;
procedure GraphicalObject.update;
begin (* nepeonpezenneicn s zouepnem xnacce *)
end;
procedure GraphicalObject.hitBy(anObject :
GraphicalObject);
begin (* nepeonpezenneicn s zouepnem xnacce *)
end;
function GraphicalObject.intersect
(anObject : GraphicalObject) : boolean;
var
theIntersection : Rect;
begin
intersect := SectRect
(region, anObject.region, theIntersection);
end;
TenepL Ball, Wall n Hole o6Lnannk1cn kak nopknaccm o6uero knacca GraphicalObject, n
any1pn nnx nn k ueHy o6Lnann1L pannme nnn qynkunn, ecnn 1onLko onn ne
nepeonpepennk1cn:
Hole = object (GraphicalObject)
(* wnwuwanwsauwn mecionono+enwn nys- *)
procedure initialize (x, y : integer);
(* wsopa+enwe nys- *)
procedure draw; override;
(* coomwis nyse, uio s nee nonan map *)
procedure hitBy (anObject : GraphicalObject);
override;
end;
Hpouepypa hitBy ponxna npeo6pasoaa1L 1nn apryHen1a a Ball. BnaropasyHno npoaepn1L 1nn
po npnaepennn:
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;
converted to PDF by BoJIoc
[enan knacc CueBall nopknaccoH Ball, Hm nnkanpnpyeH ycnoanm onepa1op a nporpaHHe
nso6paxennn uapa.
CueBall = Object (Ball)
procedure draw; override;
end;
procedure Ball.draw;
begin
(* pwcyem xpyr *)
PaintOval (region);
end;
procedure CueBall.draw;
begin
(* pwcyem oxpy+nocis *)
FrameOval (region);
end;
Han6onLuee ynpouenne poc1nrae1cn 1eH, u1o 1enepL Hoxno pepxa1L ace rpaqnueckne
o6Lek1m a opnoH cnncke. HporpaHHa, pncykuan aecL akpan, sanncmaae1cn 1ak:
procedure drawBoard;
var
gptr : GraphicalObject;
begin
SetPort (theWindow);
gptr := listOfObjects;
while gptr <> nil do begin
gptr.draw;
gptr := gptr.link;
end;
end;
Han6onee aaxnmH Hec1oH a1oro kopa nanne1cn amsoa qynkunn draw any1pn unkna.
HecHo1pn na 1o u1o amsoa nanncan opnn, nnorpa 6ype1 amsmaa1Lcn qynkunn knacca Ball, a
a ppyrnx cnyuanx knacca Wall nnn Hole. To1 qak1, u1o opno o6pauenne k qynkunn Hoxe1
npnaec1n k amsoaaH pasnnunmx qynkun, o1nocn1cn k nonn1nk nonnHopqnsHa. Mm
o6cypnH ero a rnaae 14.
ac1L nopnporpaHHm Ball.update, npoaepnkuan, ypapnncn nn panxyuncn uap o6o u1o-
nn6ypL, 1akxe ynpouae1cn ananornunmH o6pasoH. D1o Hoxno yanpe1L a nonnoH ncxopnoH
1ekc1e a Hpnnoxennn B.
Ynpaxueuun
1. HpepnonoxnH, am xo1n1e nponsaopn1L onpepenennoe pec1ane kaxpm pas, korpa
nporpaHHa BnnLnpp amnonnne1 unkn o6pa6o1kn co6m1nn. B kakoH Hec1e nyuue
acero noHec1n1L a1o1 kop?
2. HpepnonoxnH, am xo1n1e cpena1L uapm uae1nmHn. Kakne uac1n nporpaHHm aaH
npnpe1cn nsHenn1L?
3. HpepnonoxnH, am xo1n1e po6aan1L nysm na 6okoamx c1enkax, kak na o6munoH
6nnLnppnoH c1one. Kakne uac1n nporpaHHm aaH npnpe1cn nsHenn1L?
4. HporpaHHa BnnLnpp ncnonLsye1 He1op, npn ko1opoH a unkne npocHa1pnaae1cn
cnncok uapoa n kaxpm uap, nHekun aneprnk, neHnoro cpanrae1cn.
AnL1epna1nanm n 6onee o6Lek1no-opnen1npoaannm nopxop saknkuae1cn a 1oH,
u1o6m nosaonn1L kaxpoHy uapy, noka on nHee1 aneprnk, nsHenn1L caoe coc1onnne
n coc1onnne uapoa, ko1opme on sapeaae1. Torpa pnn sanycka Hopenn 6nnLnppa
converted to PDF by BoJIoc
neo6xopnHo 1onLko npnpa1L panxenne 6enoHy uapy. HsHenn1e nporpaHHy, u1o6m
ncnonLsoaa1L a1o1 nopxop. 1o pae1 6onee peanLnyk HopenL? HoueHy?
Fnaaa 7

Hacnepoaauue
HepamH uaroH npn nsyuennn o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn 6mno
ocosnanne sapaun kak asanHopec1ann nporpaHHnmx koHnonen1. D1o1 oprannsaunonnm
nopxop 6mn rnaanmH npn pas6ope npnHepoa a rnaaax 5 n 6.
CnepykunH uaroH a nsyuennn o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn c1ane1
oprannsaunn knaccoa a anpe nepapxnuecko c1pyk1ypm, ocnoaanno na npnnunne
nacnepoaannn. Hop nacnepoaanneH Hm nonnHaeH aosHoxnoc1L poc1yna npepc1aan1ene
pouepnero knacca (nopknacca) k pannmH n He1opaH popn1enLckoro knacca (napknacca).
7.1. Hu1yu1uauoe onucauue uacnepoaauun
[aaa1e aepneHcn k Ono xosnke uae1ounoro Harasnna ns nepao rnaam. Mm anpaae
oxnpa1L o1 nee anonne onpepenennoro noaepennn ne no1oHy, u1o ona xosnka nHenno
uae1ounoro Harasnna, a no1oHy, u1o ona xosnka Harasnna. HanpnHep, Ono nonpocn1 aac
onna1n1L sakas, a sa1eH pac1 aaH kan1anunk. D1n pec1ann ne nannk1cn ynnkanLnmHn pnn
anapenLua uae1ounoro Harasnna; onn o6une pnn 6ynounnkoa, 6akaneunkoa, npopaauoa
kanuennpcknx 1oaapoa n aa1oHo6nne n 1. p. TaknH o6pasoH, Hm kak 6m cansann
onpepenennoe noaepenne c o6ue ka1eropne xosneaa Harasnnoa Shopkeeper, n
nockonLky xosneaa (n xosnkn) uae1ounmx Harasnnoa (Florist) nannk1cn uac1nmH cnyuaeH
ka1eropnn Shopkeeper, noaepenne pnn pannoro nopknacca onpepenne1cn aa1oHa1nueckn.
B nsmkax nporpaHHnpoaannn nacnepoaanne osnauae1, u1o noaepenne n pannme, cansannme
c pouepnnH knaccoH, acerpa nannk1cn pacunpenneH (1o ec1L 6onLunH Hnoxec1aoH)
caoc1a, cansannmx c popn1enLcknHn knaccaHn. Hopknacc nHee1 ace caoc1aa
popn1enLckoro knacca n, kpoHe 1oro, pononnn1enLnme caoc1aa. C ppyro c1oponm,
nockonLky pouepnn knacc nanne1cn 6onee cneunannsnpoaanno (nnn orpannuenno)
qopHo popn1enLckoro knacca, on 1akxe, a onpepenennoH cHmcne, 6ype1 cyxenneH
popn1enLckoro knacca. D1o pnanek1nueckoe npo1naopeune Hexpy nacnepoaanneH kak
pacunpenneH n nacnepoaanneH kak cyxenneH nanne1cn nc1ounnkoH 6onLuo cnnm,
npncyue panno 1exnnke, n a 1o xe apeHn amsmaae1 neko1opyk ny1annuy. Mm a1o yanpnH
a cnepykunx paspenax npn npak1nueckoH nsyuennn nacnepoaannn.
Hacnepoaanne acerpa 1pansn1nano, 1ak u1o knacc Hoxe1 nacnepoaa1L uep1m napknaccoa,
o1c1onunx o1 nero na neckonLko ypoane. HanpnHep, ecnn co6akn Dog nannk1cn
nopknaccoH knacca Hnekonn1akunx Mammal, a Hnekonn1akune Mammal nannk1cn
nopknaccoH knacca xnao1nmx Animal, 1o knacc co6ak Dog nacnepye1 caoc1aa n
Hnekonn1akunx Mammal, n acex xnao1nmx Animal.
YcnoxnnkunH o6c1on1enLc1aoH a naueH nn1yn1nanoH onncannn nacnepoaannn nanne1cn
1o1 qak1, u1o nopknaccm Hory1 nepeonpepenn1L noaepenne, ynacnepoaannoe o1
popn1enLckoro knacca. HanpnHep, knacc y1konocoa Platypus nepeonpepenne1 npouepypy
pasHnoxennn, ynacnepoaannyk o1 knacca Hnekonn1akunx Mammal, nockonLky y1konocm
o1knapmaak1 nua. B a1o rnaae Hm kopo1ko kocneHcn HexannsHa nepeonpepenennn. K
6onee pe1anLnoHy o6cyxpennk ceHan1nkn nacnepoaannn Hm aepneHcn a rnaae 11.

converted to PDF by BoJIoc
7.2. Hopknacc, nop1un u npuuun nopc1auoaku
PaccHo1pnH cansL Hexpy 1nnoH pannmx, cansannmH c popn1enLcknH knaccoH, n 1nnoH
pannmx, cansannmH c pouepnnH knaccoH. Moxno y1aepxpa1L cnepykuee:
Hpepc1aan1enn nopknacca ponxnm anape1L aceHn o6nac1nHn pannmx popn1enLckoro
knacca.
Hpepc1aan1enn nopknacca ponxnm o6ecneunaa1L amnonnenne, no kpane Hepe
uepes nacnepoaanne (ecnn ne1 nanoro nepeonpepenennn), acex qynkunonanLnmx
o6nsannoc1e popn1enLckoro knacca. D1o ne npo1naopeun1 1oHy, u1o y noaoro
knacca Hory1 nonan1Lcn pononnn1enLnme o6nsannoc1n.
Hpepc1aan1enL pouepnero knacca Hoxe1 nHn1npoaa1L noaepenne popn1enLckoro
knacca n ponxen 6m1L neo1nnunH o1 npepc1aan1enn popn1enLckoro knacca a
cxopnmx cn1yaunnx.
Hnxe a a1o rnaae Hm, pas6npan pasnnunme aapnan1m nacnepoaannn, yanpnH u1o a1n
y1aepxpennn ne acerpa aepnm. TeH ne Henee onn pak1 xopouee onncanne
npeannsnpoaannoro nopxopa k nacnepoaannk. Hoa1oHy qopHannsyeH 1ako nopxop a anpe
npnnunna nopc1anoakn.
Hpnnunn nopc1anoakn y1aepxpae1, u1o ecnn ec1L paa knacca A n B 1akne, u1o knacc B
nanne1cn nopknaccoH knacca A (aosHoxno, o1c1on a nepapxnn na neckonLko c1ynene), 1o
Hm ponxnm nHe1L aosHoxnoc1L nopc1aan1L npepc1aan1enn knacca B aHec1o npepc1aan1enn
knacca A a nk6o cn1yaunn, npnueH 6es anpnHoro aqqek1a.
Kak Hm yanpnH a rnaae 10, 1epHnn nop1nn uac1o npnHenne1cn k 1ako cansn knacc
nopknacc, pnn ko1opo amnonnen npnnunn nopc1anoakn, a o1nnune o1 o6uero cnyuan, a
ko1opoH a1o1 npnnunn ne acerpa ypoane1aopne1cn.
Mm anpenn npnHenenne npnnunna nopc1anoakn a rnaae 6. B paspene 6.4 onncmaanacL
cnepykuan npouepypa:
procedure drawBoard;
var
gptr : GraphicalObject;
begin
SetPort (theWindow);
(* napwcosais sce rpawuecxwe o+exi- *)
gptr := listOfObjects;
while gptr <> nil do
begin
gprt.draw;
gptr := gptr.link;
end;
end;
Ino6anLnan nepeHennan listOfObjects o1nocn1cn k cnncky rpaqnuecknx o6Lek1oa opnoro ns
1pex 1nnoa. HepeHennan gptr o6Lnanena npoc1o kak rpaqnueckn o6Lek1, xo1n ao apeHn
amnonnennn 1ena unkna ona npnnnHae1 snauennn, ko1opme qak1nueckn npepc1aannk1
co6o o6Lek1m nopoxpennmx nopknaccoa. B opnoH cnyuae gptr copepxn1 uap, a ppyroH
nysy, a a 1pe1LeH c1enky. Hpn o6pauennn k qynkunn draw acerpa amsmaae1cn
nopxopnun He1op pnn 1ekyuero snauennn gptr, o1nnunm o1 He1opa o6Lnanennoro knacca
GraphicalObject. [nn 1oro u1o6m a1a npouepypa pa6o1ana aepno, neo6xopnHo, u1o6m
qynkunonanLnme aosHoxnoc1n kaxporo ns nopknaccoa coo1ae1c1aoaann oxnpaeHmH
qynkunonanLnmH o6nsannoc1nH, onpepenennmH pnn popn1enLckoro knacca; 1o ec1L
nopknaccm ponxnm 6m1L 1akxe n nop1nnaHn.
converted to PDF by BoJIoc
7.2.1. Hop1unm u c1poru kou1ponu 1unoa pauumx
Hsmkn nporpaHHnpoaannn co c1a1nuecknHn 1nnaHn pannmx (1akne, kak C++ n Object
Pascal) penak1 6onee cnnLnm ynop na npnnunn nopc1anoakn, ueH a1o nHee1 Hec1o a
nsmkax c pnnaHnuecknHn 1nnaHn pannmx (Smalltalk n Objective-C). Hpnunna a1oro a 1oH,
u1o nsmkn co c1a1nuecknHn 1nnaHn pannmx cknonnm xapak1epnsoaa1L o6Lek1m uepes
npnnncannme nH knaccm, 1orpa kak nsmkn c pnnaHnuecknHn 1nnaHn pannmx uepes nx
noaepenne. HanpnHep, nonnHopqnan qynkunn (qynkunn, ko1opan Hoxe1 npnnnHa1L a
kauec1ae apryHen1oa o6Lek1m pasnnunmx knaccoa) a nsmke co c1a1nuecknHn 1nnaHn
pannmx Hoxe1 o6ecneun1L ponxnm ypoaenL qynkunonanLnmx aosHoxnoc1e, 1onLko
no1pe6oaaa, u1o6m ace apryHen1m 6mnn nopknaccaHn nyxnoro knacca. HockonLky a nsmke c
pnnaHnuecknHn 1nnaHn pannmx apryHen1m aoace ne nHek1 1nna, nopo6noe 1pe6oaanne
npoc1o osnauano 6m, u1o apryHen1 ponxen yHe1L o1aeua1L na onpepenennm na6op
coo6uenn. [anLneuee o6cyxpenne c1a1nuecknx n pnnaHnuecknx 1nnoa pannmx Hm
npoaepeH a rnaae 10, a pe1anLnoe nsyuenne nonnHopqnsHa a rnaae 14.
7.3. 4opHm uacnepoaauun
Hacnepoaanne npnHenne1cn na ypnanenne no-pasnoHy. B a1oH paspene Hm onnueH
nan6onee uac1o ncnonLsyeHme aapnan1m. 3aHe1nH, u1o a cnepykueH cnncke npnaepenm
a6c1pak1nme ka1eropnn o6uero xapak1epa, n on ne npe1enpye1 na nonno1y. Bonee 1oro,
nnorpa nponcxopn1 1ak, u1o pasnnunme He1opm knacca peannsyk1 nacnepoaanne no-
pasnoHy.
B cnepykunx paspenax
1
o6pa1n1e oco6oe annHanne na 1o, korpa nacnepoaanne
o6ecneunaae1 nopoxpenne nop1nnoa, a korpa ne1.
7.3.1. Hopoxpeuue nopknaccoa pnn cneuanusauu
(nopoxpeuue nop1unoa)
Haaepnoe nan6onee uac1o nopoxpenne nopknaccoa n nacnepoaanne ncnonLsyk1cn pnn
cneunannsaunn. Hpn nopoxpennn nopknacca pnn cneunannsaunn noam knacc nanne1cn
cneunannsnpoaanno qopHo popn1enLckoro knacca, no ypoane1aopne1 cneunqnkaunnH
popn1enn ao acex cyuec1aennmx HoHen1ax. TaknH o6pasoH, pnn a1o qopHm nonnoc1Lk
amnonnne1cn npnnunn nopc1anoakn. BHec1e co cnepykue ka1eropne (nacnepoaanne pnn
cneunqnkaunn) cneunannsaunn nanne1cn nan6onee npeanLno qopHo nacnepoaannn, k
ko1opo ponxna c1peHn1Lcn xopouan nporpaHHa.
Bo1 npnHep nopoxpennn nopknaccoa co cneunannsaune. Knacc okon Window
npepoc1aanne1 o6une onepaunn c oknaHn (cpanr, nsHenenne pasHepoa, caep1maanne n 1.
p.). Cneunannsnpoaannm nopknacc 1ekc1oamx okon TextEditWindow nacnepye1 onepaunn c
oknaHn n pononnn1enLno o6ecneunaae1 cpepc1aa, nosaonnkune okny o1o6paxa1L
1ekc1oayk nnqopHaunk, a nonLsoaa1enk ee pepak1npoaa1L. HockonLky knacc
TextEditWindow ypoane1aopne1 aceH caoc1aaH, oxnpaeHmH o1 okon a o6ueH anpe (n,
cnepoaa1enLno, nanne1cn nop1nnoH knacca Window a pononnenne k 1oHy, u1o on nanne1cn
ero nopknaccoH), Hm pacnosnaeH 1akyk cn1yaunk kak nopoxpenne nopknaccoa pnn
cneunannsaunn.
7.3.2. Hopoxpeuue nopknacca pnn cneuqukauu
Cnepykun aapnan1 ncnonLsoaannn nacnepoaannn coc1on1 a 1oH, u1o6m rapan1npoaa1L
noppepxky knaccaHn onpepenennoro o6uero nn1epqeca, 1o ec1L peannsaunk nHn opnnx n
1ex xe He1opoa. Popn1enLckn knacc Hoxe1 6m1L koH6nnaune peannsoaannmx onepaun n
onepaun, ocyuec1anenne ko1opmx poaepeno pouepnnH knaccaH. ac1o ne1 nnkaknx
o1nnun a nn1epqece Hexpy popn1enLcknH n pouepnnH knaccaHn nocnepnn npoc1o
o6ecneunaae1 amnonnenne onncannoro, no ne peannsoaannoro a popn1enLckoH knacce
noaepennn.
converted to PDF by BoJIoc
Oak1nueckn a1o cneunanLnm cnyua nopoxpennn cneunannsnpykuero nopknacca sa
ncknkuenneH 1oro, u1o nopknaccm nannk1cn ne ycoaepuenc1aoaanneH

1
Onncannme specL ka1eropnn asn1m ns [Halbert 1987], xo1n n po6aann neckonLko
co6c1aennmx. HpnHep pepak1npyeHoro okna asn1 ns [Meyer 1988a].
cyuec1aykuero 1nna, a, ckopee, peannsaune nenonno, a6c1pak1no cneunqnkaunn. B
1aknx cnyuanx popn1enLckn knacc nnorpa nasmaak1 a6c1pak1no cneunqnunpoaannmH
knaccoH.
B npnHepe, nHn1npykueH nrpy 6nnLnpp, a rnaae 6 knacc rpaqnuecknx o6Lek1oa
GraphicalObject nanne1cn a6c1pak1nmH knaccoH, nockonLky on ne peannsye1 He1opm
o1o6paxennn o6Lek1oa n nx peakunk na conpnkocnoaenne c uapoH. Hocnepykune knaccm
Ball, Wall n Hole ncnonLsyk1 nopoxpenne nopknaccoa pnn cneunqnkaunn, o6ecneunaan
peanLnoe aonnouenne a1nx He1opoa.
B o6ueH cnyuae nopoxpenne nopknaccoa pnn cneunqnkaunn pacnosnae1cn no 1oHy, u1o
qak1nueckoe noaepenne ne onpepeneno a popn1enLckoH knacce ono 1onLko onncano n
6ype1 peannsoaano a pouepneH knacce.
7.3.3. Hopoxpeuue nopknacca c enum
kouc1pyupoaauun
ac1o knacc nacnepye1 nou1n ace qynkunonanLnoe noaepenne popn1enLckoro knacca,
nsHennn 1onLko nHena He1opoa nnn onpepenennmH o6pasoH Hopnqnunpyn apryHen1m. D1o
Hoxe1 nponcxopn1L paxe a 1oH cnyuae, korpa noaoHy n popn1enLckoHy knaccaH ne ypae1cn
coxpann1L Hexpy co6o o1nouenne 6m1L akseHnnnpoH (is-a relationship).
HanpnHep, nepapxnueckan c1pyk1ypa knaccoa a nsmke Smalltalk peannsye1 o6o6uenne
Haccnaa, nasmaaeHoe Dictionary (cnoaapL). CnoaapL npepc1aanne1 co6o na6op nap knku
snauenne; knkun Hory1 6m1L nponsaonLnmHn. Ta6nnuy cnHaonoa, ko1opyk Hoxno 6mno 6m
ncnonLsoaa1L a koHnnnn1ope, pasyHno npepc1aan1L cnoaapeH, a ko1opoH nnpekcaHn cnyxa1
cnHaonnueckne nHena, a snauennn nHek1 qnkcnpoaannm qopHa1, onpepenennm pnn
o1penLnmx sannce a1o 1a6nnum. Cnepoaa1enLno, knacc SymbolTable (1a6nnua cnHaonoa)
ponxen 6m1L nopoxpen ns knacca Dictionary aaepenneH noamx He1opoa, cneunqnunmx pnn
1a6nnum cnHaonoa. [pyrnH npnHepoH Hoxe1 cnyxn1L noc1poenne coaokynnoc1n
a6c1pak1nmx pannmx na ocnoae 6asoaoro knacca, o6ecneunaakuero He1opm pa6o1m co
cnnckaHn. B o6onx cnyuanx pouepnn knacc ne nanne1cn 6onee cneunannsnpoaanno
qopHo popn1enLckoro knacca, 1ak kak y nac n a Hmcnnx ne 6ype1 nopc1aann1L
npepc1aan1ene pouepnero knacca 1ypa, rpe ncnonLsyk1cn npepc1aan1enn popn1enLckoro
knacca.
Tnnnunm npnHep nopoxpennn c uenLk konc1pynpoaannn na6nkpae1cn npn cospannn
knacca, sanncmaakuero snauennn a paonunm qan, nanpnHep, a cnc1eHax xpanennn
nnqopHaunn. Popn1enLckn knacc, kak npaanno, o6ecneunaae1 sanncL 1onLko
nec1pyk1ypnpoaannmx paonunmx pannmx. Hopknacc c1pon1cn pnn kaxpo c1pyk1ypm,
1pe6ykue coxpanennn. On peannsye1 npouepypy xpanennn pnn onpepenennoro 1nna
pannmx, ko1opan ncnonLsye1 He1opm popn1enLckoro knacca pnn nenocpepc1aenno sanncn
1
.

1
D1o1 npnHep nnnkc1pnpye1 pacnnmaua1oc1L ka1eropn. Ecnn pouepnn knacc
peannsye1 npouepypy xpanennn, ncnonLsyn ppyroe nHn He1opa, Hm roaopnH, u1o
a1o nacnepoaanne pnn konc1pynpoaannn. Ecnn xe pouepnn knacc nonLsye1cn 1eH
xe nHeneH, u1o n popn1enLckn knacc, Hm Hornn 6m ckasa1L, u1o a1o
nacnepoaanne pnn cneunqnkaunn.
class Storable
{
converted to PDF by BoJIoc
void writeByte(unsigned char);
}
class StoreMyStruct : public Storable
{
void writeStruct (MyStruct &aStruct);
}
B nsmkax co c1a1nuecknHn 1nnaHn pannmx koco cHo1pn1 na nopoxpenne nopknaccoa pnn
konc1pynpoaannn, nockonLky ono uac1o nanpnHyk napyuae1 npnnunn nopc1anoakn
(nonannk1cn nopknaccm, ne nannkunecn nop1nnaHn). C ppyro c1oponm, 6ypyun 6mc1pmH n
nerknH cnoco6oH noc1poennn noamx a6c1pakun, ono unpoko ncnonLsye1cn a nsmkax c
pnnaHnuecknHn 1nnaHn pannmx. B 6n6nno1eke c1anpap1nmx nporpaHH nsmka Smalltalk
Hoxno na1n Hnoxec1ao npnHepoa nopoxpennn nopknaccoa c uenLk konc1pynpoaannn.
Mm nsyunH konkpe1nm aapnan1 nopoxpennn c uenLk konc1pynpoaannn a rnaae 8. TaH xe
Hm ysnaeH, u1o nsmk nporpaHHnpoaannn C++ npepoc1aanne1 nn1epecnm HexannsH:
sakpm1oe nacnepoaanne, ko1opm nosaonne1 nopoxpa1L nopknaccm pnn konc1pynpoaannn
6es napyuennn npnnunna nopc1anoakn.
7.3.4. Hopoxpeuue nopknacca pnn o6o6euun
HcnonLsoaanne nacnepoaannn npn nopoxpennn nopknaccoa pnn o6o6uennn a onpepenennoH
cHmcne nanne1cn npo1naononoxnoc1Lk nopoxpennk pnn cneunannsaunn. B a1oH cnyuae
nopknacc pacunpne1 popn1enLckn knacc pnn cospannn o6Lek1a 6onee o6uero 1nna.
Hopoxpenne nopknacca pnn o6o6uennn uac1o npnHenne1cn, korpa noc1poenne nponcxopn1
na ocnoae cyuec1aykunx knaccoa, ko1opme Hm ne xo1nH nnn ne HoxeH nsHenn1L.
PaccHo1pnH cnc1eHy rpaqnueckoro o1o6paxennn, a ko1opo 6mn onpepenen knacc okon
Window pnn uepno-6enoro qona. Mm xo1nH cospa1L 1nn uae1nmx rpaqnuecknx okon
ColoredWindow. Uae1 qona 6ype1 o1nnua1Lcn o1 6enoro sa cue1 po6aanennn noaoro nonn,
copepxauero uae1. Hpnpe1cn 1akxe nepeonpepenn1L nacnepyeHyk npouepypy nso6paxennn
okna, a ko1opo nponcxopn1 qonoaan sannaka.
Hopoxpenne nopknacca pnn o6o6uennn uac1o ncnonLsye1cn a cnyuae, korpa o6un npoek1
ocnoamaae1cn a nepayk ouepepL na snauennnx pannmx n HenLue na qynkunonanLnoH
noaepennn. D1o anpno na npnHepe c uae1nmH oknoH, 1ak kak ono copepxn1 nonn pannmx,
neo6nsa1enLnme a cnyuae uepno-6enoro okna.
Kak npaanno, cnepye1 ns6era1L nopoxpennn nopknacca pnn o6o6uennn, nonLsyncL
nepeaepny1o nepapxne 1nnoa n nopoxpenneH pnn cneunannsaunn. Opnako a1o ne acerpa
aosHoxno.
7.3.5. Hopoxpeuue nopknacca pnn pacmupeuun
B 1o apeHn kak nopoxpenne nopknacca pnn o6o6uennn Hopnqnunpye1 nnn pacunpne1
cyuec1aykune qynkunonanLnme aosHoxnoc1n o6Lek1a, nopoxpenne pnn pacunpennn
po6aanne1 coaepuenno noame caoc1aa. Ero Hoxno o1nnun1L no 1oHy, u1o nopoxpenne pnn
o6o6uennn o6nsano nepeonpepenn1L no kpane Hepe opnn He1op popn1enn, a
qynkunonanLnme aosHoxnoc1n nopknacca npnansanm k popn1enLcknH. Pacunpenne npoc1o
po6aanne1 noame He1opm k popn1enLcknH, n qynkunonanLnme aosHoxnoc1n nopknacca
Henee kpenko npnansanm k cyuec1aykunH He1opaH popn1enn.
HpnHepoH nopoxpennn nopknacca pnn pacunpennn nanne1cn Hnoxec1ao 1ekc1oamx c1pok
StringSet, nacnepykuee caoc1aa o6uero knacca Hnoxec1a Set n npepnasnauennoe pnn
xpanennn c1pokoamx aennunn. Tako knacc Hor 6m npepoc1aann1L pononnn1enLnme He1opm
pnn c1pokoamx onepaun, nanpnHep na1n no npeqnkcy, ko1opm aosapauan 6m
nopHnoxec1ao acex aneHen1oa Hnoxec1aa, naunnakunxcn c onpepenenno nopc1pokn.
Takne onepaunn nHek1 cHmcn pnn nopknacca, no ne pnn popn1enLckoro knacca.
converted to PDF by BoJIoc
HockonLky qynkunonanLnme aosHoxnoc1n popn1enn oc1ak1cn ne1pony1mHn n poc1ynnmHn,
nopoxpenne nopknacca pnn pacunpennn ne npo1naopeun1 npnnunny nopc1anoakn n,
cnepoaa1enLno, 1akne nopknaccm acerpa 6ypy1 nop1nnaHn.
7.3.6. Hopoxpeuue nopknacca pnn orpauuveuun
Hopoxpenne pnn orpannuennn nponcxopn1 a cnyuae, korpa aosHoxnoc1n nopknacca 6onee
orpannuenm, ueH a popn1enLckoH knacce. Tak xe, kak n npn o6o6uennn, nopoxpenne pnn
orpannuennn uaue acero aosnnkae1, korpa nporpaHHnc1 c1pon1 knacc na ocnoae
cyuec1aykue nepapxnn, ko1opan ne ponxna nnn ne Hoxe1 6m1L nsHenena.
[onyc1nH, cyuec1aykuan 6n6nno1eka knaccoa npepoc1aanne1 c1pyk1ypy pannmx Deque
(double-ended-queue, ouepepL c payHn konuaHn). DneHen1m Hory1 po6aann1Lcn nnn
ypann1Lcn c nk6oro konua c1pyk1ypm 1nna Deque, no nporpaHHnc1 xenae1 cospa1L knacc
Stack, aaopn 1pe6oaanne po6aanennn nnn ypanennn aneHen1oa 1onLko c opnoro konua
c1eka.
TaknH xe o6pasoH, kak n npn nopoxpennn pnn konc1pynpoaannn, nporpaHHnc1 Hoxe1
noc1pon1L knacc Stack kak nopknacc knacca Deque n Hopnqnunpoaa1L nexena1enLnme
He1opm 1ak, u1o6m onn ampaaann coo6uenne o6 oun6ke a cnyuae npnHenennn. Takne
He1opm nepeonpepennk1 cyuec1aykune n orpannunaak1 nx aosHoxnoc1n, u1o
xapak1epnsye1 nopoxpenne nopknacca pnn orpannuennn. (Hepeonpepenenne, c noHouLk
ko1oporo nopknacc nsHenne1 cHmcn He1opa popn1enLckoro knacca, o6cyxpae1cn a
cnepykue rnaae.)
HockonLky nopoxpenne nopknacca pnn orpannuennn nanne1cn nanmH napyuenneH npnnunna
nopc1anoakn n nockonLky ono c1pon1 nopknaccm, ne nannkunecn nop1nnaHn, ero cnepye1
ns6era1L, rpe 1onLko aosHoxno.
7.3.7. Hopoxpeuue nopknacca pnn aapuupoaauun
Hopoxpenne nopknacca pnn aapLnpoaannn npnHenne1cn, korpa paa knacca nHek1 cxopnyk
peannsaunk, no ne nHek1 nnkako anpnHo nepapxnuecko cansn Hexpy a6c1pak1nmHn
nonn1nnHn, nHn npepc1aanneHmHn. HanpnHep, nporpaHHnm kop pnn ynpaanennn Hmuko
Hoxe1 6m1L nou1n npen1nunmH 1oHy, u1o 1pe6ye1cn pnn ynpaanennn rpaqnuecknH
nnanue1oH. Teope1nueckn, opnako, ne1 nnkaknx npnunn, pnn 1oro u1o6m knacc Mouse,
ynpaannkun Hannnynn1opoH HmuL, 6mn nopknaccoH knacca Tablet, kon1ponnpykuero
rpaqnueckn nnanue1, nnn nao6opo1. B 1akoH cnyuae a kauec1ae popn1enn nponsaonLno
am6npae1cn opnn ns nnx, npn a1oH ppyro nacnepye1 o6uyk nporpaHHnyk uac1L kopa n
nepeonpepenne1 kop, saancnun o1 yc1poc1aa.
O6muno, opnako, nyuue anL1epna1nao nanne1cn ampenenne o6uero kopa a a6c1pak1nm
knacc, ckaxeH PointingDevice, n nopoxpenne o6onx knaccoa o1 a1oro o6uero npepka. Kak n
a cnyuae nopoxpennn nopknacca pnn o6o6uennn, 1ako ny1L Hoxe1 6m1L nepoc1ynen npn
popa6o1ke yxe cyuec1aykuero knacca.
7.3.8. Hopoxpeuue nopknacca pnn koH6uuupoaauun
O6munmH nanne1cn xenanne nHe1L nopknacc, npepc1aannkun co6o koH6nnaunk payx
nnn 6onee popn1enLcknx knaccoa. Accnc1en1 yun1enn, nanpnHep, nHee1 xapak1epnme
oco6ennoc1n kak yun1enn, 1ak n yuauerocn n, cnepoaa1enLno, Hoxe1 aec1n ce6n paonko.
Cnoco6noc1L nacnepoaa1L o1 payx nnn 6onee popn1enLcknx knaccoa nsaec1na kak
Hnoxec1aennoe nacnepoaanne; ono poc1a1ouno cnoxno n koaapno, n Hm nocan1nH a1oHy
aonpocy uenyk rnaay.

converted to PDF by BoJIoc
7.3.9. Kpa1koe nepevucneuue qopH uacnepoaauun
Mm HoxeH nopaec1n n1or nsyuennk pasnnunmx qopH nacnepoaannn a anpe cnepykue
1a6nnum:
Cneuanusaun. [ouepnn knacc nanne1cn 6onee konkpe1nmH, uac1nmH nnn
cneunannsnpoaannmH cnyuaeH popn1enLckoro knacca. [pyrnHn cnoaaHn, pouepnn knacc
nanne1cn nop1nnoH popn1enLckoro knacca.
Cneuqukaun. Popn1enLckn knacc onncmaae1 noaepenne, ko1opoe peannsye1cn a
pouepneH knacce, no oc1aaneno nepeannsoaannmH a popn1enLckoH.
Kouc1pyupoaauue. [ouepnn knacc ncnonLsye1 He1opm, npepoc1aanneHme popn1enLcknH
knaccoH, no ne nanne1cn nop1nnoH popn1enLckoro knacca (peannsaunn He1opoa napyuae1
npnnunn nopc1anoakn).
O6o6euue. [ouepnn knacc Hopnqnunpye1 nnn nepeonpepenne1 neko1opme He1opm
popn1enLckoro knacca c uenLk nonyuennn o6Lek1a 6onee o6ue ka1eropnn.
Pacmupeuue. [ouepnn knacc po6aanne1 noame qynkunonanLnme aosHoxnoc1n k
popn1enLckoHy knaccy, no ne Henne1 nacnepyeHoe noaepenne.
Orpauuveuue. [ouepnn knacc orpannunaae1 ncnonLsoaanne neko1opmx He1opoa
popn1enLckoro knacca.
Bapuupoaauue. [ouepnn n popn1enLckn knaccm nannk1cn aapnaunnHn na opny 1eHy, n
cansL knaccnopknacc nponsaonLna.
KoH6uuupoaauue. [ouepnn knacc nacnepye1 uep1m 6onee ueH opnoro popn1enLckoro
knacca. D1o Hnoxec1aennoe nacnepoaanne; ono 6ype1 paccHo1peno a opno ns
cnepykunx rnaa.
7.4. Hacnepoaauue a pasnuvumx nsmkax
nporpaHHupoaauun
B cnepykunx paspenax Hm noppo6no onnueH cnn1akcnc, ncnonLsyeHm pnn onncannn
nacnepoaannn a kaxpoH ns paccHa1pnaaeHmx nsmkoa nporpaHHnpoaannn. O6pa1n1e
annHanne na pasnnuy Hexpy 1eHn nsmkaHn, ko1opme 1pe6yk1, u1o6m ace knaccm 6mnn
nopoxpennnHn o6uero popn1enLckoro knacca (o6muno nasmaaeHoro Object, kak a nsmkax
Smalltalk n Objective-C), n 1eHn, ko1opme ponyckak1 pasnnunme nesaancnHme nepapxnn
knaccoa.
HpenHyuec1ao nacnepoaannn c epnnmH npepkoH a 1oH, u1o qynkunonanLnme aosHoxnoc1n
nocnepnero (knacca Object) nacnepyk1cn aceHn o6Lek1aHn. TaknH o6pasoH rapan1npye1cn,
u1o kaxpm o6Lek1 o6napae1 o6unH HnnnHanLnmH ypoaneH qynkunonanLnoc1n. Mnnyc a
1oH, u1o epnnan nepapxnn sauennne1 ace knaccm ppyr c ppyroH.
B cnyuae neckonLknx nesaancnHmx nepapxn nacnepoaannn npnnoxennk ne npnpe1cn
1aun1L sa co6o 6onLuyk 6n6nno1eky knaccoa, ns ko1opo nnuL neHnorne 6ypy1
ncnonLsoaa1Lcn a kaxpo konkpe1no nporpaHHe. Koneuno, a1o osnauae1, u1o ne1
qynkunonanLnoc1n, ko1opo rapan1npoaanno o6napak1 ace o6Lek1m.
Pasnnunme asrnnpm na o6Lek1m o1uac1n nannk1cn eue opnnH pasnnuneH Hexpy nsmkaHn c
pnnaHnuecknHn n c1a1nuecknHn 1nnaHn pannmx (Hm aepneHcn k a1o npo6neHe a rnaae 12).
B nsmkax nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx o6Lek1m a ocnoanoH
xapak1epnsyk1cn 1eHn coo6uennnHn, ko1opme onn nonnHak1. Ecnn paa o6Lek1a nonnHak1
opno n 1o xe Hnoxec1ao coo6uenn n pearnpyk1 na nnx cxopnmH o6pasoH, onn c
npak1nuecko 1oukn spennn nepasnnunHm, neasnpan na nx popc1aennme cansn. B 1aknx
converted to PDF by BoJIoc
cnyuanx pasyHno, u1o6m ace o6Lek1m nacnepoaann 6onLuyk uac1L caoero noaepennn o1
o6uero 6asoaoro knacca.
7.4.1. Hacnepoaauue a nsmke Object Pascal
B nsmke nporpaHHnpoaannn Object Pascal qnpHm Apple nacnepoaanne o1 popn1enLckoro
knacca ykasmaae1cn noHeuenneH ero nHenn a kpyrnme cko6kn nocne knkueaoro cnoaa
object. HpepnonoxnH, nanpnHep, u1o a naue nHn1aunn 6nnLnppa Hm peunnn nopopn1L
knaccm Ball, Wall n Hole o1 o6uero knacca GraphicalObject. Mm cpenaeH a1o 1ak, kak
nokasano a nnc1nnre 7.1.
Ruc1uur 7.1. HpnHep nacnepoaannn a nsmke Object Pascal qnpHm Apple
type
GraphicalObject = object
(* nonn zann-x *)
region : Rect;
link : GraphicalObject;
(* onepauww *)
procedure draw;
procedure update;
procedure hitBy(aBall : Ball);
end;
Ball = object(GraphicalObject)
(* nonn zann-x *)
direction : real;
energy : real;
(* wnwuwanwsauwn *)
procedure initialize (x, y : integer);
(* nepeonpezennem-e meioz- *)
procedure draw; override;
procedure update; override;
procedure hitBy(aBall : Ball); override;
(* meioz-, cneuwwun-e znn xnacca *)
procedure erase;
procedure setCenter(newx, newy : integer);
function x : integer;
function y : integer;
end;

Kak nokasano a a1oH npnHepe, pouepnne knaccm Hory1 po6aann1L kak noame nonn pannmx,
1ak n noame He1opm. KpoHe 1oro, onn Hopnqnunpyk1 cyuec1aykuee noaepenne,
nepeonpepennn He1opm c noHouLk knkueaoro cnoaa override, kak cpenano pnn He1opa
draw. ApryHen1m a nepeonpepenennoH He1ope ponxnm coanapa1L no 1nny n uncny c
apryHen1aHn He1opa popn1enLckoro knacca.
Bepcnn nsmka Object Pascal qnpHm Borland (cnc1eHa Delphi) o1nnuae1cn a payx aaxnmx
o1nouennnx. Bo-nepamx, kak Hm anpenn a npepmpyunx rnaaax, aHec1o knkueaoro cnoaa
object ncnonLsye1cn cnoao class, n knaccm acerpa ponxnm amaopn1Lcn opnn ns ppyroro.
Knacc TObject o6un npepok acex o6Lek1oa. Bo-a1opmx, a pononnenne k knkueaoHy
cnoay override pnpek1naa virtual noHeuae1cn nocne onncannn 1ex He1opoa popn1enLckoro
knacca, ko1opme Hory1 6m1L nepeonpepenenm nou1n kak a nsmke C++. HpnHep,
nnnkc1pnpykun a1n nsHenennn, npnaepen a nnc1nnre 7.2. Hponyck knkueaoro cnoaa
override nanne1cn uac1mH nc1ounnkoH oun6ok, nockonLky onncanne oc1ae1cn cnn1akcnueckn
sakonnmH, a ero nn1epnpe1aunn neaepno. D1o Hm o6cypnH noppo6nee a rnaae 10.

converted to PDF by BoJIoc
Ruc1uur 7.2. HpnHep nacnepoaannn a nsmke Delphi Pascal
type
GraphicalObject = class(TObject)
(* nonn zann-x *)
region : Rect;
link : GraphicalObject;
(* onepauww *)
procedure draw; virtual;
procedure update; virtual;
procedure hitBy(aBall : Ball); virtual;
end;
Ball = class(GraphicalObject)
(* nonn zann-x *)
direction : real;
energy : real;
(* wnwuwanwsauwn *)
procedure initialize (x, y : integer);
(* nepeonpezennem-e meioz- *)
procedure draw; override;
procedure update; override;
procedure hitBy(aBall : Ball); override;
(* meioz-, cneuwwun-e znn xnacca *)
procedure erase;
procedure setCenter(newx, newy : integer);
function x : integer;
function y : integer;
end;

Bonee cyuec1aennmH pasnnuneH Hexpy nsmkaHn nporpaHHnpoaannn Delphi Pascal n Apple
Object Pascal nanne1cn aaepenne pnnaHnuecknx He1opoa. Onn ncnonLsyk1 ppyrne
HexannsHm noncka ao apeHn amnonnennn nporpaHHm (6onLue nanoHnnakune Objective-C,
ueH C++; cH. rnaay 21). D1o penae1 pnnaHnueckne He1opm 6onee HepnennmHn, ueH
anp1yanLnme, no onn 1pe6yk1 HenLue naHn1n. Knkueaoe cnoao dynamic aHec1o virtual
nokasmaae1, u1o o6Lnanne1cn pnnaHnueckn He1op. Mnorne He1opm, cansannme c
pec1annHn onepaunonno cnc1eHm no ynpaanennk oknaHn, peannsoaanm kak
pnnaHnueckne. 3nauenne 1epHnna coo6uenne uac1o orpannunaae1cn 1onLko pec1annHn,
cansannmHn c ynpaanenneH oknaHn.
7.4.2. Hacnepoaauue a nsmke Smalltalk
Kak o1Heueno naHn a rnaae 3, nacnepoaanne kak cpepc1ao cospannn noamx knaccoa
o6nsa1enLno a nsmke Smalltalk. Hoam knacc ne Hoxe1 6m1L onpepenen 6es
npepaapn1enLnoro onncannn cyuec1aykuero knacca, ko1opoHy on nacnepye1. Oak1nueckn
noam knacc cospae1cn c noHouLk coo6uennn popn1enLckoHy knaccy.
List subclass: #Set
instanceVariables: #()
classVariables: #()
HHee1cn epnnm npepok, nasmaaeHm Object, o1 ko1oporo a konue konuoa nponcxopn1 ace
oc1anLnme knaccm. Object o6ecneunaae1 ace o6Lek1m o6unHn n coaHec1nHmHn
qynkunonanLnmHn aosHoxnoc1nHn. HpnHepaHn He1opoa a1oro knacca cnyxa1 qynkunn
cpaanennn opnoro o6Lek1a c ppyrnH, neua1n c1pokoamx npepc1aanenn o6Lek1a n 1. p.
Hsmk Smalltalk o6ecneunaae1 1onLko opnnounoe nacnepoaanne, 1o ec1L kaxpm knacc
nacnepye1 1onLko opnoHy popn1enLckoHy knaccy. Hoam He1op Hoxe1 saHec1n1L He1op
popn1enLckoro knacca, npoc1o 6ypyun 1ak xe nasaannmH.
converted to PDF by BoJIoc
7.4.3. Hacnepoaauue a nsmke Objective-C
Kak n a nsmke Smalltalk, pnn Objective-C nacnepoaanne nanne1cn neo1LeHneHo uac1Lk
qopHnpoaannn noaoro knacca. Onncanne nn1epqeca kaxporo noaoro knacca ponxno
onpepenn1L npepka, o1 ko1oporo nponcxopn1 nacnepoaanne. Cnepykun npnHep
nokasmaae1, u1o knacc nrpanLnmx kap1 Card nponcxopn1 o1 ynnaepcanLnoro knacca Object:
@interface Card : Object
{
. . .
}
. . .
@end
Kak n a nsmke Smalltalk, cyuec1aye1 epnnm npepok Object, o1 ko1oporo a koneunoH cue1e
nponcxopn1 ace oc1anLnme knaccm. Object o6ecneunaae1 ace o6Lek1m o6ue n nonno
qynkunonanLnoc1Lk. On uac1o ncnonLsye1cn kak popn1enL pnn noaoro knacca.
Paspeuae1cn 1onLko opnnounoe nacnepoaanne, 1o ec1L knacc ne Hoxe1 nacnepoaa1L o1 payx
nnn 6onee popn1ene. Kak n a nsmke Smalltalk, He1op, nHekun 1o xe nHn, u1o n He1op
popn1enLckoro knacca, nepeonpepenne1 ero.
7.4.4. Hacnepoaauue a nsmke C++
B o1nnune o1 Smalltalk n Objective-C noam knacc a nsmke nporpaHHnpoaannn C++ ne
o6nsan nponcxopn1L o1 yxe cyuec1aykuero knacca. Hacnepoaanne ykasmaae1cn a
saronoake onncannn knacca c noHouLk knkueaoro cnoaa public, sa ko1opmH cnepye1 nHn
popn1enLckoro knacca. Hoam knacc TablePile nponcxopn1 o1 6onee o6uero knacca CardPile,
npepc1aannkuero co6o konopy kap1:
class TablePile : public CardPile
{
. . .
};
Knkueaoe cnoao public Hoxe1 6m1L saHeneno na cnoao private, ykasmaakuee na
nopoxpenne knacca pnn konc1pynpoaannn, 1o ec1L na qopHy nacnepoaannn, ne cospakuyk
nop1nna. Tako npnHep 6ype1 paccHo1pen a rnaae 11.
Kak 6mno o1Heueno a npepmpyue rnaae, npenHyuec1ao o6Lek1no-opnen1npoaannmx
nsmkoa a 1oH, u1o onn c1peHn1cn o6Lepnnn1L cospanne noao nepeHenno n ee
nnnunannsaunk. Hacnepoaanne neckonLko ycnoxnne1 a1o1 npouecc, nockonLky
popn1enLckn n noam knaccm Hory1 nHe1L pasnm nnnunannsnpykun kop. [nn
o6ecneuennn nacnepoaannn konc1pyk1op pouepnero knacca ponxen nano amsaa1L
konc1pyk1opa popn1enLckoro knacca. [enae1cn a1o c noHouLk nnnunannsnpykuero
npepnoxennn a konc1pyk1ope pouepnero knacca:
TablePile::TablePile (int x, int y, int c)
: CardPile(x,y) // wnwuwanwsauwn pozwienn
{
// ieneps wnwuwanwswpyem zouepnwn xnacc
. . .
}
Hsmk nporpaHHnpoaannn C++ noppepxnaae1 Hnoxec1aennoe nacnepoaanne, 1o ec1L noam
knacc Hoxe1 6m1L onpepenen kak no1oHok payx nnn 6onee popn1ene. Mm nccnepyeH cHmcn
a1oro 6onee noppo6no a cnepykue rnaae.
B npepmpyue rnaae Hm onncann knkueame cnoaa public (o1kpm1m) n private (sakpm1m),
ykasaa, u1o opno onncmaae1 nn1epqec knacca, a ppyroe pe1ann peannsaunn. B onncannn
knaccoa, nonyuaeHmx nacnepoaanneH, Hoxe1 6m1L ncnonLsoaano 1pe1Le knkueaoe cnoao
converted to PDF by BoJIoc
protected (saunuennm). 3aunuennme nonn nannk1cn uac1Lk peannsaunn, no poc1ynnm
nopknaccaH 1ak xe, kak n caHoHy knaccy.
Korpa knkueaoe cnoao virtual npepuec1aye1 onncannk axopnue a knacc qynkunn, ono
osnauae1, u1o a1a qynkunn, aepon1no, 6ype1 nepeonpepenena a nopknacce nnn u1o a1a
qynkunn caHa nepeonpepenne1 qynkunk napknacca. (D1o knkueaoe cnoao nanne1cn
neo6nsa1enLnmH a pouepneH knacce, no ero xena1enLno oc1aann1L.) Opnako ceHan1nka
nepeonpepenennn a nsmke C++ nanne1cn 1onknH HoHen1oH n saancn1 o1 1oro, kak 6mn
onncan nonyua1enL, ko1opoHy npncaanaae1cn o6Lek1. Mm o1noxnH ee o6cyxpenne po
rnaam 11.
7.4.5. Hacnepoaauue a nsmke Java
Hs acex o6cyxpaeHmx a a1o knnre nsmkoa Java npe1 panLue acex a paspenennn nonn1n
nopknacca n nop1nna. Hopknaccm o6Lnannk1cn c noHouLk knkueaoro cnoaa extends, kak a
cnepykueH npnHepe:
class window
{
// . . .
}
class textEditWindow extends window
{
// . . .
}
Hpepnonarae1cn, u1o nopknaccm nannk1cn nop1nnaHn (xo1n, kak n a cnyuae nsmka C++, a1o
npepnonoxenne ne acerpa aepno). D1o osnauae1, u1o npepc1aan1enL nopknacca Hoxe1 6m1L
npncaoen nepeHenno, o6Lnanenno c 1nnoH popn1enLckoro knacca. Me1opm pouepnero
knacca, nHekune 1e xe nHena, u1o n He1opm popn1enn, nepeonpepennk1 nacnepyeHoe
noaepenne. Kak n a nsmke C++, knkueaoe cnoao protected Hoxe1 6m1L ncnonLsoaano pnn
ykasannn He1opoa n pannmx, poc1ynnmx 1onLko any1pn knacca nnn nopknacca, no ne
axopnunx a 6onee o6un nn1epqec.
Bce knaccm nponcxopn1 o1 epnnoro npepka Object. Ecnn popn1enLckn knacc ne ykasan
nano, 1o npepnonarae1cn knacc Object. TaknH o6pasoH, onpepenenne knacca window,
npnaepennoe amue, akanaanen1no cnepykue sanncn:
class window extends Object
{
// . . .
}
AnL1epna1nanan qopHa nopoxpennn nop1nnoa pannmx ocnoaana na nn1epqece. Hn1epqec
onpepenne1 npo1okon onpepenennoro noaepennn, a ne peannsaunk. B a1oH o1nouennn on
nopo6en a6c1pak1noHy popn1enLckoHy knaccy. B cnepykueH npnHepe npnaepen nn1epqec,
onncmaakun o6Lek1m, ko1opme Hory1 un1a1L n nnca1L ao axopno/amxopno no1ok.
public interface Storing
{ void writeOut(Stream s);
void readFrom(Stream s);
}
Hn1epqec onpepenne1 noam 1nn. D1o osnauae1, u1o nepeHennme Hory1 6m1L o6Lnanenm
npoc1o c nHeneH nn1epqeca. A knacc Hoxe1 ykasa1L, u1o on peannsye1 npo1okon,
onpepenennm nn1epqecoH. Hpepc1aan1enn knacca Hory1 npncaanaa1Lcn nepeHennmH,
o6LnanennmH c 1nnoH nn1epqeca, 1ouno 1ak xe, kak npepc1aan1enn pouepnero knacca
Hory1 npncaanaa1Lcn nepeHennmH, o6LnanennmH c 1nnoH popn1enLckoro knacca:
public class BitImage implements Storing
{ void writeOut (Stream s)
{
converted to PDF by BoJIoc
// . . .
};
void readFrom (Stream s)
{
// . . .
};
};
Xo1n nsmk Java noppepxnaae1 1onLko opnnounoe nacnepoaanne (nacnepoaanne
ncknkun1enLno o1 opnoro popn1enLckoro knacca), knacc Hoxe1 ykasmaa1L, u1o on
noppepxnaae1 neckonLko nn1epqecoa (peannsye1 Hnoxec1aennm nn1epqec). Mnorne
npo6neHm, pnn ko1opmx a nsmke C++ npnunocL 6m ncnonLsoaa1L Hnoxec1aennoe
nacnepoaanne, a nsmke nporpaHHnpoaannn Java paspeuak1cn uepes Hnoxec1aennme
nn1epqecm. Hn1epqecaH nosaoneno pacunpn1L ppyrne nn1epqecm, a 1oH uncne n
Hnoxec1aennme, uepes ykasanne knkueaoro cnoaa extend.
B nsmke Java npen nopoxpennn nopknacca pnn cneunqnkaunn qopHannsoaana uepes
Hopnqnka1op abstract. Ecnn knacc o6Lnanen kak abstract, 1o ns nero ponxnm nopoxpa1Lcn
nopknaccm. He paspeuae1cn cospaaa1L npepc1aan1ene a6c1pak1noro knacca, Hoxno 1onLko
nopoxpa1L nopknaccm. Me1opm 1oxe Hory1 6m1L o6Lnanenm kak abstract, n a 1akoH cnyuae
onn ne o6nsanm nHe1L peannsaunk. TaknH o6pasoH, o6Lnanenne knacca kak abstract
o6ecneunaae1, u1o on 6ype1 ncnonLsoaa1Lcn 1onLko kak cneunqnkaunn noaepennn, a ne a
anpe konkpe1nmx o6Lek1oa:
abstract class storable
{
public abstract writeOut();
}
Hao6opo1, Hopnqnka1op final ykasmaae1, u1o ns knacca ne Hory1 nopoxpa1Lcn nopknaccm
nnn u1o He1op, k ko1opoHy o1nocn1cn a1o1 Hopnqnka1op, ne Hoxe1 6m1L nsHenen. TeH
caHmH nonLsoaa1enk rapan1npye1cn, u1o noaepenne knacca 6ype1 1aknH, kaknH
onpepeneno, n ne 6ype1 Hopnqnunpoaano npn nopoxpennn nocnepykunx nopknaccoa:
final class newClass extends oldClass
{
. . .
}
7.5. HpeuHyec1aa uacnepoaauun
B a1oH paspene Hm onnueH neko1opme ns Hnornx aaxnmx npenHyuec1a npaannLnoro
ncnonLsoaannn HexannsHa nacnepoaannn.
7.5.1. Hoa1opuoe ucnonusoaauue nporpaHH
Hpn nacnepoaannn noaepennn o1 ppyroro knacca nporpaHHnm kop ne nyxpae1cn a
nepenncmaannn. D1o kaxe1cn oueanpnmH, no na caHoH pene nHee1 aaxnoe snauenne.
Mnorne nporpaHHnc1m 1pa1n1 Haccy apeHenn na nepepa6o1ky kopa, yxe nanncannoro ne
opnn pas po a1oro, nanpnHep, npn noncke no o6pasuy a c1poke nnn npn aknkuennn
noaoro aneHen1a a 1a6nnuy. C aaepenneH o6Lek1no-opnen1npoaanno 1exnnkn, a1n
qynkunn Hory1 6m1L nanncanm opnaxpm n no1oH noa1opno ncnonLsoaa1Lcn.
[pyroe npenHyuec1ao noa1opno ncnonLsyeHoro kopa a ero noamuenno napexnoc1n (ueH
a 6onLueH uncne cn1yaun ncnonLsye1cn kop, 1eH 6onLue aosHoxnoc1e o6napyxennn
oun6ok) n nnsko c1onHoc1n, 1ak kak ona penn1cn na acex nonLsoaa1ene kopa.


converted to PDF by BoJIoc
7.5.2. Hcnonusoaauue o6ero kopa
Hpn npnHenennn o6Lek1no-opnen1npoaanno 1exnnkn ncnonLsoaanne o6uero kopa
nponcxopn1 na neckonLknx ypoannx. Bo-nepamx, knnen1m Hory1 nonLsoaa1Lcn opnnHn n
1eHn xe knaccaHn (Bpep Kokc [Cox 1986] nasmaae1 nx software-IC, 1o ec1L nporpaHHnmHn
nn1erpanLnmHn cxeHaHn, no ananornn c annapa1nmHn nn1erpanLnmHn cxeHaHn). Hnan
qopHa ncnonLsoaannn o6uero kopa aosnnkae1 a cnyuae, korpa paa nnn 6onee knacca,
paspa6o1annmx 1eH xe caHmH nporpaHHnc1oH pnn neko1oporo npoek1a, nacnepyk1 o1
epnnoro popn1enLckoro knacca. HanpnHep, Hnoxec1ao Set n Haccna Array Hory1
paccHa1pnaa1Lcn kak pasnoanpnoc1n coaokynnoc1n pannmx Collection. B a1oH cnyuae paa
nnn 6onee 1nnoa o6Lek1oa coaHec1no ncnonLsyk1 nacnepyeHm kop. On nnue1cn epnnoxpm
n axopn1 a nporpaHHy 1onLko a opnoH Hec1e.
7.5.3. Cornacoaauue uu1epqeca
Korpa paa knacca nacnepyk1 opnoHy n 1oHy xe npepky, Hm HoxeH 6m1L yaepenm, u1o
nacnepyeHoe noaepenne 6ype1 opnnakoamH ao acex cnyuanx. TaknH o6pasoH, nerko
rapan1npoaa1L, u1o o6Lek1m, cxoxne no nn1epqecy, 6ypy1 n qak1nueckn cxopnmHn. B
npo1nanoH cnyuae nonLsoaa1enL nonyun1 nou1n opnnakoame o6Lek1m, noaepenne ko1opmx
coaepuenno pasnoe.
7.5.4. HporpaHHume koHnoueu1m
B rnaae 1 Hm o1Heuann, u1o nacnepoaanne npepoc1aanne1 nporpaHHnc1aH aosHoxnoc1L
cospaaa1L noa1opno (Hnorokpa1no) ncnonLsyeHme nporpaHHnme koHnonen1m. UenL:
o6ecneun1L pasan1ne noamx npnnoxenn c HnnnHanLnmH nanncanneH noaoro kopa. Yxe
ceuac poc1ynnm neckonLko koHHepuecknx 6n6nno1ek 1akoro 1nna, n a 6ypyueH Hm HoxeH
oxnpa1L nonanennn Hnornx noamx cneunannsnpoaannmx cnc1eH.
7.5.5. Bmc1poe Hake1upoaauue
Korpa nporpaHHnoe o6ecneuenne konc1pynpye1cn a ocnoanoH ns noa1opno ncnonLsyeHmx
koHnonen1, 6onLuan uac1L apeHenn, 1pe6yeHoro na paspa6o1ky, Hoxe1 6m1L nocanuena
nonnHannk noamx neo6munmx uac1e cnc1eHm. TaknH o6pasoH nporpaHHnme koHnnekcm
Hory1 cospaaa1Lcn 6mc1pee n npoue, npnaopn k c1nnk nporpaHHnpoaannn, nsaec1noHy kak
6mc1poe Hake1npoaanne nnn nccnepoaa1enLckoe nporpaHHnpoaanne. Cospae1cn cnc1eHa-
npo1o1nn (Hake1), nonLsoaa1enn akcnepnHen1npyk1 c ne, no1oH na ocnoae a1nx onm1oa
cospae1cn a1opan cnc1eHa, c ne npoaopn1cn akcnepnHen1m n 1. p. Takoe
nporpaHHnpoaanne oco6enno amropno a cn1yaunn, korpa uenn n 1pe6oaannn k cnc1eHe a
nauane paspa6o1kn npepc1aanenm aecLHa pacnnmaua1o.
7.5.6. HonuHopqusH u c1pyk1ypa
HporpaHHnoe o6ecneuenne 1papnunonno cospaaanocL cnnsy aaepx, xo1n n Horno
paspa6a1maa1Lcn caepxy anns. To ec1L cnauana nncanncL nporpaHHm nnxnero ypoann, na
nx ocnoae c1ponnncL 6onee a6c1pak1nme aneHen1m, a sa1eH eue 6onee a6c1pak1nme.
Tako npouecc noxox na c1pon1enLc1ao poHa.
O6muno Ho6nnLnoc1L kopa yHenLuae1cn c yaennuenneH a6c1pakunn, 1o ec1L nporpaHHm
nnxnero ypoann Hory1 6m1L ncnonLsoaanm a neckonLknx pasnnunmx npoek1ax, n paxe,
aosHoxno, a6c1pakunn cnepykuero ypoann Hory1 noa1opno ncnonLsoaa1Lcn, no nporpaHHm
aepxnero ypoann 1ecno cansanm c onpepenennmH npnnoxenneH. KoHnonen1m nnxnero
ypoann Hory1 6m1L nepenecenm a noayk cnc1eHy, n uac1o ec1L cHmcn a nx caHoc1on1enLnoH
cyuec1aoaannn. KoHnonen1m aepxnero ypoann o6muno nHek1 cHmcn (ns-sa nx
qynkunonanLnoc1n nnn saancnHoc1n o1 pannmx), 1onLko korpa onn noc1poenm na
onpepenennmx aneHen1ax nnxnero ypoann.
converted to PDF by BoJIoc
HonnHopqnsH a nsmkax nporpaHHnpoaannn nosaonne1 nporpaHHnc1y cospaaa1L
Hnorokpa1no ncnonLsyeHme koHnonen1m amcokoro ypoann, ko1opme Hoxno nepekpanaa1L
nop pasnnunme npnnoxennn sa cue1 nsHenennn nnxnero ypoann. Mm eue noroaopnH o6
a1oH a nocnepykunx rnaaax.
7.5.7. Mackupoaka uuqopHauu
HporpaHHnc1, ncnonLsykun nporpaHHnyk koHnonen1y, ponxen nonnHa1L 1onLko ee
nasnauenne n nn1epqec. HporpaHHnc1y coaceH ne o6nsa1enLno nHe1L noppo6nyk
nnqopHaunk o 1exnnuecknx cpepc1aax, ncnonLsoaannmx npn peannsaunn koHnonen1m.
TaknH o6pasoH yHenLuae1cn neo6xopnHoc1L any1pennnx canse Hexpy nporpaHHnmHn
cnc1eHaHn. Mm panee ykasmaann na sauennnkuncn xapak1ep 1papnunonnmx
nporpaHHnmx npopyk1oa kak na opny ns npnunn nx cnoxnoc1n.
7.6. Hspepxku uacnepoaauun
Xo1n npenHyuec1aa nacnepoaannn a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn
necoHnennm, nnuero ne pae1cn papoH. Ho a1o npnunne Hm ponxnm paccHo1pe1L nspepxkn
1exnnuecknx cpepc1a o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn a uac1noc1n,
nacnepoaannn.
7.6.1. Ckopoc1u amnonueuun
Pepko nporpaHHnme nnc1pyHen1m o6uero nasnauennn nannk1cn c1onL xe 6mc1pmHn, kak n
akkypa1no, apyunyk paspa6o1annme cneunanLnme cnc1eHm. TaknH o6pasoH,
ynacnepoaannme He1opm, cnoco6nme nHe1L peno c nponsaonLnmHn nopknaccaHn, uac1o
Hepnennee cneunanLnoro kopa.
H ace xe sa6o1m o6 aqqek1nanoc1n uac1o 6maak1 ne k Hec1y
1
. Bo-nepamx, pasnnua ne
cnnukoH aennka. Bo-a1opmx, cnnxenne ckopoc1n amnonnennn Hoxe1 koHnencnpoaa1Lcn
noamuenneH ckopoc1n paspa6o1kn nporpaHHnoro o6ecneuennn. H nakoneu, 6onLunnc1ao
nporpaHHnc1oa na caHoH pene Hano snak1 o 1oH, kak pacnpepenenm apeHennme sa1pa1m a
nx nporpaHHax. Iopaspo nyuue cospa1L pa6o1akuyk cnc1eHy, nponsaec1n saHepm apeHenn,
u1o6m o6napyxn1L, na u1o xe, co6c1aenno, ono 1pa1n1cn, n ynyuun1L a1n uac1n, ueH
sa1pa1n1L yHy apeHenn, sa6o1ncL o6 aqqek1nanoc1n na pannnx c1apnnx npoek1a.
7.6.2. PasHep nporpaHH
HcnonLsoaanne nk6o nporpaHHno 6n6nno1ekn uac1o npnaopn1 k yaennuennk pasHepa
nporpaHH. D1oro ne nponcxopn1 a cneunanLno paspa6o1annmx cnc1eHax. Xo1n 1akne
sa1pa1m Hory1 6m1L cyuec1aennmHn, no Hepe yHenLuennn c1onHoc1n naHn1n pasHep
nporpaHHm nepec1ae1 6m1L kpn1nunmH. Cnnsn1L sa1pa1m na paspa6o1ky n 6mc1po ampa1L
amcokokauec1aennm n cao6opnm o1 oun6ok nporpaHHnm kop snaun1 ceuac ropaspo
6onLue, ueH Hanm pasHep npnnoxennn
2
.
7.6.3. Haknapume pacxopm ua nocmnky coo6euu
Mnoro annHannn o6pauanocL na 1o1 qak1, u1o nocmnka coo6uenn no caoe cy1n 6onee
poporan onepaunn, ueH npoc1o amsoa npouepypm. Opnako, kak n c cyHHapno ckopoc1Lk
amnonnennn, cnnukoH 6onLune sa6o1m o uene nocmnkn coo6uenn uac1o 6maak1
akonoHne na cnnukax. Mexpy npounH, saHepnenne

1
Cnepykuan un1a1a ns c1a1Ln Bnnna BynLqa npepnarae1 ypaunoe saHeuanne no noaopy
aaxnoc1n aqqek1nanoc1n: Bo nHn aqqek1nanoc1n (kak npaanno, aqeHepno) coaepuae1cn
6onLue nporpaHHnmx oun6ok, ueH no kako-nn6o ppyro npnunne, aknkuan nonnyk
1ynoc1L [Wulf 1972].
converted to PDF by BoJIoc
2
C1on1, opnako, npnaec1n n ppyryk 1ouky spennn. Cnepykune un1a1m npnnapnexa1 Anany
Iony6y nporpaHHnc1y, koncynL1an1y n npenopaaa1enk, cneunannsnpykueHycn a o6nac1n
OOH: Pas6yxanne nporpaHH nanne1cn orpoHno npo6neHo. Xec1kn pnck a 350 M6 na
Hoe Haunne Hoxe1 aHec1n1L onepaunonnyk cnc1eHy, yceuennyk aepcnk koHnnnn1opa n
pepak1op, n 6onLue nnuero. B c1apopaanne apeHena n Hor pasHec1n1L aepcnn CP/M pnn 1ex
xe nporpaHH na opno-epnnc1aenno pncke1e a 1,2 M6... H y6expen, u1o 6onLuan uac1L
a1oro pas6yxannn naHn1n nanne1cn pesynL1a1oH ne6pexnoro nporpaHHnpoaannn; Ecnn
1onLko am ne nponnkne1ecL cosnanneH neo6xopnHoc1n pncunnnnnnpoaa1L ce6n, 1o Hoxe1e
sakonun1L rnran1cknH HopyneH ns nenoppakuecn conpoaoxpennk 1apa6apunnm, 1onLko
npn1aopnkuecn koHnLk1epno nporpaHHo. HpnHeu. nepea.
uac1o HnnnHanLno paa nnn 1pn pononnn1enLnmx acceH6nepnmx onepa1opa n o6uee
yaennuenne apeHenn na 10 npouen1oa. PesynL1a1m saHepoa ckopoc1n pasnnunm pnn pasnmx
nsmkoa. Haknapnme pacxopm na nocmnky coo6uenn 6onLue a nsmkax nporpaHHnpoaannn c
pnnaHnuecknHn 1nnaHn pannmx (nanpnHep, Smalltalk) n ropaspo HenLue a nsmkax co
c1a1nuecknHn 1nnaHn (C++, a uac1noc1n). D1n sa1pa1m, kak n ppyrne, ponxnm
paccHa1pnaa1Lcn na qone Hnornx npenHyuec1a o6Lek1no-opnen1npoaanno 1exnnkn.
Heko1opme nsmkn nporpaHHnpoaannn, n oco6enno C++, npepoc1aannk1 nporpaHHnc1aH
neko1opoe konnuec1ao onun, pakunx aosHoxnoc1L yHenLun1L naknapnme pacxopm na
nocmnky coo6uenn. Onn aknkuak1 a ce6n ncknkuenne nonnHopqnsHa ns coo6uenn (npn
ykasannn nHenn knacca a amsoaax qynkun) n noppepxky ac1panaaeHmx (inline) npouepyp.
Hopo6nmH o6pasoH nporpaHHnc1 na nsmke Delphi Pascal Hoxe1 am6pa1L He1opm, onncannme
c noHouLk knkueaoro cnoaa dynamic, ko1opme 6ypy1 ncnonLsoaa1L HexannsH noncka ao
apeHn amnonnennn nnn ncnonLsoaa1L He1opm c knkueamH cnoaoH virtual, ko1opme
npnHennk1 neckonLko 6onee 6mc1pyk 1exnnky. [nnaHnueckne He1opm 6onee Hepnennm npn
nacnepoaannn, no 1pe6yk1 HenLue naHn1n.
7.6.4. Cnoxuoc1u nporpaHH
Xo1n o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne uac1o ampanrae1cn kak cnoco6
paspeuennn npo6neHm cnoxnoc1n nporpaHHnoro o6ecneuennn, neo6pyHannoe
ncnonLsoaanne nacnepoaannn Hoxe1 uac1o npoc1o saHenn1L opny qopHy cnoxnoc1n na
ppyryk. [nn nonnHannn nporpaHHm, ncnonLsykue nacnepoaanne, Hoxe1 no1pe6oaa1Lcn
neckonLko cnoxnmx nepexopoa aaepx n anns a nepapxnueckoH pepeae. D1a npo6neHa
nsaec1na nop nHeneH aaepx-anns, nnn o-o. Mm o6cypnH ee a cnepykue rnaae.
Ynpaxueuun
1. HpepnonoxnH, aaH 1pe6ye1cn nannca1L nporpaHHnm npoek1 na nsmke
nporpaHHnpoaannn, ko1opm ne nanne1cn o6Lek1no-opnen1npoaannmH (nanpnHep,
Pascal nnn C). Kak 6m am nHn1npoaann knaccm n He1opm? Kak 6m am nHn1npoaann
nacnepoaanne? CHoxe1e nn am o6ecneun1L Hnoxec1aennoe nacnepoaanne?
O6ocny1e cao o1ae1.
2. Mm ykasmaann, u1o naknapnme pacxopm, cansannme c nocmnko coo6uenn, o6muno
6onLue, ueH npn 1papnunonnoH amsoae npouepyp. Kak am Hornn 6m nx nsHepn1L?
[nn nsmka nporpaHHnpoaannn, noppepxnaakuero n knaccm, n npouepypm (C++ nnn
Object Pascal), npnpyHa1e akcnepnHen1 pnn onpepenennn qak1nuecknx sa1pa1 na
nocmnky coo6uenn.
3. PaccHo1pn1e 1pn reoHe1pnuecknx nonn1nn: nnnnn (6eckoneuna a o6onx
nanpaanennnx), nyu (nauano a qnkcnpoaanno 1ouke, 6eckoneuen a opnoH
nanpaanennn), cerHen1 (o1pesok npnHo c qnkcnpoaannmHn konuaHn). Kak 6m am
noc1ponnn knaccm, npepc1aannkune a1n 1pn nonn1nn, a anpe nepapxnn
nacnepoaannn? Bype1 nn aaue peuenne ppyrnH, ecnn am o6pa1n1e oco6oe annHanne
na npepc1aanenne pannmx (na noaepenne)? Oxapak1epnsy1e 1nn nacnepoaannn,
ko1opm am ncnonLsoaann. O6Lncnn1e aaue peuenne.
converted to PDF by BoJIoc
4. HoueHy ncnonLsoaannm a cnepykueH paccyxpennn npnHep ne nanne1cn aepno
nnnkc1paune nacnepoaannn?
BnpnHo, nan6onee aaxnmH nonn1neH a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn
nanne1cn nacnepoaanne. O6Lek1m Hory1 nacnepoaa1L caoc1aa ppyrnx o6Lek1oa, 1eH caHmH
nnkanpnpye1cn neo6xopnHoc1L nanncannn kakoro-nn6o kopa! HpepnonoxnH, nanpnHep, u1o
nporpaHHa ponxna o6pa6a1maa1L koHnnekcnme uncna, coc1onune ns aeuec1aenno n
HnnHo uac1e. [nn koHnnekcnmx uncen aeuec1aennan n HnnHan uac1n aepy1 ce6n kak
aeuec1aennme aennunnm, noa1oHy ace onepaunn (+, , /, *, sqrt, sin, cos n 1. p.) Hory1
6m1L nacnepoaanm o1 knacca Real aHec1o 1oro, u1o6m nnca1L noam kop. D1o, necoHnenno,
okaxe1 6onLuoe annnnne na npopyk1nanoc1L pa6o1m nporpaHHnc1a.
Fnaaa 8



Yve6um npuHep:
Hacunuc
HporpaHHa pnn packnapmaannn kap1ounoro nacLnnca nponnnkc1pnpye1 ack HouL
nacnepoaannn n nepeonpepenennn. B rnaaax 3 n 4 ac1peuanncL qparHen1m a1o
nporpaHHm, a uac1noc1n a6c1pakunn nrpanLno kap1m, npepc1aanennan knaccoH Card.
HsmkoH nporpaHHnpoaannn a1oro yue6noro npnHepa 6ype1 Java.
Ocnoanoe annHanne 6ype1 ypeneno knaccy CardPile, a6c1parnpykueHy c1onky
nrpanLnmx kap1. Tak kak nepeknapmaanne kap1 ns opno c1onkn a ppyryk a1o ocnoanoe
pec1ane nacLnnca, 1o nopknaccm CardPile 6ypy1 6asoamHn c1pyk1ypaHn pannmx npn
peannsaunn nacLnnca. HHee1cn Hnoxec1ao c1onok kap1, n nacnepoaanne akyne c
nepeonpepenenneH nn1encnano ncnonLsye1cn pnn ynpouennn paspa6o1kn a1nx koHnonen1 n
o6ecneuennn nx epnnoo6pasnn.
8.1. Knacc nrpanunmx kap1 Card
B npepmpyunx rnaaax Hm o6cyxpann a6c1pak1nm knacc Card. Hoa1opnH neko1opme
aaxnme HoHen1m.
Kaxpm akseHnnnp knacca Card (nuc1uur 8.1) napenen Hac1Lk n panroH. 1o6m
npepo1apa1n1L nx nsHenenne, nonn pannmx (nepeHennme akseHnnnpa) o6Lnanenm
sakpm1mHn, n cpena1L u1o-nn6o c nnHn Hoxno 1onLko nocpepc1aoH qynkun poc1yna.
3nauennn none Hac1n n panra yc1anaannaak1cn konc1pyk1opoH knacca. KpoHe 1oro,
o1penLnan qynkunn nosaonne1 nonLsoaa1ennH onpepenn1L uae1 kap1m. 3nauennn
uenouncnennmx konc1an1 (onpepenneHmx a nsmke Java c noHouLk cneunqnka1opoa final
static) sapanm pnn uepnoro n kpacnoro uae1oa, a 1akxe pnn Hac1e. Eue opna napa
uenouncnennmx konc1an1 onpepenne1 amco1y n unpnny kap1m.
Ec1L aaxnme npnunnm pnn 1oro, u1o6m o6paua1Lcn k Hac1n n panry 1onLko uepes
qynkunn poc1yna. HpnHo poc1yn k a1nH nonnH cnepye1 sanpe1n1L. Torpa nonn Hac1n n
panra Hory1 6m1L npoun1anm, no ne Hopnqnunpoaanm. (Coo1ae1c1aykuan qynkunn,
ncnonLsyeHan rnaanmH o6pasoH pnn nsHenennn snauenn none pannmx, uac1o nasmaae1cn
Hy1a1opoH (mutator).)
converted to PDF by BoJIoc
Ruc1uur 8.1. Onncanne knacca card
class Card
{
// xoncipyxiop
Card (int sv, int rv)
{
s = sv; r = rv; faceup = false;
}
// zociyn x aipwyiam xapi-
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)
{
...
}
// ciaiwuecxwe nonn zann-x znn useia w maciw
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;

// nonn zann-x
private boolean faceup;
private int r;
private int s;
}

H1ak, ace pec1ann, ko1opme Hoxe1 amnonnn1L kap1a (kpoHe yc1anoakn n aosapa1a
coc1onnnn), a1o nepeaopaunaanne n nokas ce6n. Oynkunn flip() coc1on1 ns opno
c1poukn, ko1opan npoc1o o6pauae1 snauenne, copepxaueecn a nepeHenno akseHnnnpa
faceup, na npo1naononoxnoe. Oynkunn pncoaannn draw() cnoxnee: ona ncnonLsye1
rpaqnueckne cpepc1aa, npepoc1aanneHme c1anpap1no 6n6nno1eko npnnoxenn Java.
Bn6nno1eka npnnoxenn noc1aanne1 1nn pannmx, nasmaaeHm Graphics, ko1opm
o6ecneunaae1 Hnoxec1ao He1opoa pncoaannn nnnn n qnryp, a 1akxe packpaunaanne. B
kauec1ae apryHen1a qynkunn pncoaannn nepepae1cn snauenne 1nna Graphics, a 1akxe
uenouncnennme kooppnna1m, coo1ae1c1aykune aepxneHy neaoHy yrny kap1m.
Ipaqnueckne nso6paxennn kap1 pncynkn ns npoc1mx nnnn, kak nokasano nnxe.
epan n 6y6nm napncoaanm kpacnmH, a nnkn n kpec1n uepnmH. L1pnxoaka py6aukn
amnonnena xen1mH uae1oH. OparHen1 npouepypm pncoaannn nrpanLno kap1m nokasan a
nuc1uure 8.2.
converted to PDF by BoJIoc

Han6onee aaxnan oco6ennoc1L a6c1pakunn nrpanLno kap1m a1o c1nnL, npn ko1opoH
kaxpan kap1a o1ae1c1aenna sa xpanenne a ce6e ace nnqopHaunn n noaepennn, k ne
o1nocnunxcn. Kap1a snae1 n caoe snauenne, n 1o, kak ce6n napncoaa1L. TaknH o6pasoH,
nnqopHaunn nnkancynnpoaana n nsonnpoaana o1 npnnoxennn, ncnonLsykuero nrpanLnme
kap1m. Ecnn, nanpnHep, nporpaHHa nepenecena na noayk nna1qopHy, ncnonLsykuyk
ppyrne rpaqnueckne cpepc1aa, 1o nsHenn1L nyxno 6ype1 1onLko He1op draw any1pn caHoro
knacca.
Ruc1uur 8.2. Hpouepypa pncoaannn nrpanLno kap1m
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"};
// Ouwciwis npnmoyronsnwx, napwcosais rpanwuy
g.clearRect(x, y, width, height);
g.setColor(Color.black);
g.drawRect(x, y, width, height);
// napwcosais ieno xapi-
if (faceUp) // nwueson cioponon ssepx
{ 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 // xapiwnxon snws
{ g.setColor(Color.yellow);
g.drawLine(x+15, y+5, x+15, y+65);
g.drawLine(x+35, y+5, x+35, y+65);
g.drawLine(x+5, y+20, x+45, y+20);
g.drawLine(x+5, y+35, x+45, y+35);
converted to PDF by BoJIoc
g.drawLine(x+5, y+50, x+45, y+50);
}
}
}
8.2. Cansnme cnnckn
Kon1enep c1onka kap1 ncnonLsye1 pnn nx xpanennn HopenL cansnoro cnncka. O1pennn
knacc kon1enepa pannmx o1 ero konkpe1nmx npepc1aan1ene (c1onok nrpanLnmx kap1), Hm
nosaonneH kaxpoHy knaccy ckonuen1pnpoaa1Lcn na orpannuennoH Hnoxec1ae sapau.
D1o nanne1cn npopanxenneH no cpaanennk c rnaao 6 (rpe, kak am noHnn1e, kaxpm
rpaqnueckn o6Lek1 copepxan ykasa1enL na cnepykun rpaqnueckn o6Lek1). B nopxope,
nsnoxennoH a rnaae 6, nnoxo ne 1onLko 1o, u1o none ykasa1enn-canskn ne oco6enno aaxno
pnn o6Lek1a, copepxauerocn a kon1enepe, no n 1o, u1o npn 1akoH cnoco6e o6Lek1 ne
Hoxe1 6m1L aknkuen a paa (nnn 6onee) cnncka opnoapeHenno. Cospaaan o1penLnme knaccm
pnn a6c1pakunn cansnmx cnnckoa, Hm nonyuaeH ropaspo 6onLuyk rn6koc1L a
ncnonLsoaannn kon1enepoa.
B a6c1pakunn cansnoro cnncka sapec1aoaanm paa knacca. Knacc LinkedList a1o
qacap cnncka, 1o ec1L knacc, c ko1opmH asanHopec1aye1 nonLsoaa1enL. B
pec1an1enLnoc1n snauennn xpann1cn a akseHnnnpax knacca List. O6muno nonLsoaa1enL
paxe ne porapmaae1cn o cyuec1aoaannn knacca List. O6a knacca nokasanm a nuc1. 8.3.
Tak kak kon1enep pannmx na ocnoae cansnoro cnncka nanne1cn a6c1pakune o6uero
nasnauennn n nnuero ne snae1 o 1nne o6Lek1a, ko1opm on 6ype1 copepxa1L, 1o 1nn
pannmx, npnnncmaaeHm o6Lek1y-snauennk, a1o knacc acex o6Lek1oa Object.
HepeHennan, o6Lnanennan c 1nnoH pannmx Object (a uac1noc1n, none pannmx value a
knacce Link), nanne1cn nonnHopqno ona Hoxe1 copepxa1L snauenne nk6oro 1nna.
Knacc LinkedList o6ecneunaae1: po6aanenne aneHen1a a cnncok, npoaepky cnncka na
nannune a neH aneHen1oa, poc1yn k nepaoHy aneHen1y cnncka, ypanenne nepaoro aneHen1a
cnncka.
Ruc1uur 8.3. Knaccm Link n 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 ()
{ firstLink = null; }
public void add (Object newValue)
{ firstLink = new Link(newValue, firstLink); }
public boolean empty ()
{ return firstLink == null; }
public Object front ()
{
converted to PDF by BoJIoc
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;
}
B 6onee o6ueH cnyuae Hm xo1enn 6m npepoc1aan1L nonLsoaa1enk naue a6c1pakunn
cansnoro cnncka cnoco6 pnn nepe6opa aennunn, copepxaunxcn a cnncke, 6es
neo6xopnHoc1n nx ypanennn n 6es snannn pe1anLno nnqopHaunn o any1penne c1pyk1ype
cnncka (a pannoH cnyuae 6es caepenn o knacce Link). Kak Hm yanpnH a rnaae 16, 1akne
aosHoxnoc1n uac1o o6ecneunaak1cn paspa6o1unkaHn knacca cnncok uepes poc1yn k
cneunanLno pasnoanpnoc1n o6Lek1oa, nasmaaeHmx n1epa1opaHn. H1epa1op ckpmaae1
pe1ann npepc1aanennn kon1enepa pannmx n o6ecneunaae1 npoc1o nn1epqec pnn poc1yna
k snauennnH a nopnpke ouepepn. H1epa1op pnn cansnoro cnncka nokasan a nuc1uur 8.4. C
ero noHouLk unkn sanncmaae1cn cnepykunH o6pasoH:
ListIterator itr = aList.iterator();
while (! Itr.atEnd() )
{
... do something list itr.current() ...
itr.next();
}
O6pa1n1e annHanne na 1o, kak caH cnncok aosapauae1 n1epa1op a pesynL1a1e amsoaa
He1opa n kak ncnonLsoaanne n1epa1opa nosaonne1 ns6exa1L ynoHnnannn o cansnmx nonnx
cnncka.
Ruc1uur 8.4. Knacc ListIterator
class ListIterator
{
public ListIterator (Link firstLink)
{
currentLink = firstLink;
}
public boolean atEnd ()
{
return currentLink == null;
}
public void next ()
{
if (currentLink != null)
currentLink = currentLink.next();
}
public Object current ()
{
if (currentLink == null)
return null;
return currentLink.value();
}
private Link currentLink:
}
converted to PDF by BoJIoc
8.3. Hpaanna nacunnca
Bepcnn nacLnnca, ko1opyk Hm 6ypeH onncmaa1L, nsaec1na nop nasaanneH Kocmnka
(nnn Klondike). Becuncnennme aapnaunn a1o nrpm penak1 ee, aosHoxno, nan6onee
pacnpoc1panenno aepcne nacLnnca, 1ak u1o korpa am roaopn1e cnoao nacLnnc, Hnorne
nkpn pyHak1 o kocmnke. Bepcnn, ko1opyk Hm 6ypeH ncnonLsoaa1L specL, onncana a
knnre [Morehead 1949]. B ynpaxnennnx Hm paccHo1pnH neko1opme pacnpoc1panennme
pasnoanpnoc1n a1oro nacLnnca.
Pacnonoxenne kap1 nokasano na puc. 8.1. HcnonLsye1cn opna c1anpap1nan konopa ns 52
kap1. Packnap nacLnnca (tableau) coc1on1 ns 28 kap1 a 7 c1onkax. Hepaan c1onka coc1on1
ns 1 kap1m, a1opan ns 2 n 1. p. po 7. Bepxnnn kap1a a kaxpo c1onke nsnauanLno nexn1
kap1nnko aaepx; ace oc1anLnme kap1nnko anns.

Puc. 8.1. HauanLnm packnap nacLnnca
C1onkn Hac1e (nnorpa nasmaaeHme ocnoaannnHn (foundations)) c1pon1cn o1 1ysoa po
kopone no Hac1nH. Onn cospak1cn caepxy packnapa no Hepe 1oro, kak nyxnme kap1m
c1anoan1cn poc1ynnmHn. UenL nrpm cnoxn1L ace 52 kap1m a ocnoaannn no Hac1nH.
Te kap1m, ko1opme ne amnoxenm a c1onkn, nsnauanLno naxopn1cn a konope (deck).
Kap1m 1aH nexa1 kap1nnko anns, onn poc1ak1cn ns konopm no opno n knapy1cn
kap1nnko aaepx a npoHexy1ounyk c1onky (discard pile). O11ypa onn nepeHeuak1cn na
packnap nnn a ocnoaannn. Kap1m poc1ak1cn ns konopm, noka ona ne onyc1ee1. Hrpa
sakanunaae1cn, ecnn panLneune nepeHeuennn kap1 neaosHoxnm.
Kap1m knapy1cn a c1onkn packnapa 1onLko na kap1y cnepykuero no c1apunnc1ay panra
n npo1naononoxnoro uae1a. Kap1a nepenocn1cn a ocnoaanne, ecnn ona 1o xe Hac1n n
cnepye1 no c1apunnc1ay sa aepxne kap1o opnoro ns ocnoaann (nnn ecnn ocnoaanne
nyc1oe n kap1a nanne1cn 1ysoH). Hyc1me npoHexy1kn, aosnnkakune a packnape ao apeHn
nrpm, sanonnnk1cn 1onLko koponnHn.
CaHan aepxnnn kap1a npoHexy1ouno c1onkn acerpa poc1ynna. Cyuec1aye1 1onLko opna
aosHoxnoc1L nepeHec1n1L 6onee opno kap1m nonoxn1L uenm na6op o1kpm1mx kap1
packnapa (nasuaaeMu nocneoaa1enunoc1uk (build)) a ppyryk c1onky packnapa. D1o
Hoxno cpena1L, ecnn caHan nnxnnn kap1a nocnepoaa1enLnoc1n Hoxe1 6m1L no npaannaH
nonoxena na caHyk aepxnkk kap1y a c1onke nasnauennn. Haua nepaonauanLnan nrpa ne
6ype1 noppepxnaa1L nepeHeuenne nocnepoaa1enLnoc1e, no Hm o6cypnH a1o a kauec1ae
aosHoxnoro pacunpennn. CaHan aepxnnn kap1a packnapa acerpa nexn1 kap1nnko aaepx.
Ecnn kap1a ypanne1cn ns packnapa, oc1aannn na aepunne sakpm1yk kap1y, 1o nocnepnkk
Hoxno o1kpm1L nepeaepny1L ee kap1nnko aaepx.
converted to PDF by BoJIoc
Hs a1oro kopo1koro onncannn ncno, u1o nacLnnc a ocnoanoH saknkuae1cn a
Hannnynnpoaannn c1onkaHn kap1. Kaxpm 1nn c1onkn, nHen pnp o6unx caoc1a c ppyrnHn
c1onkaHn, o6napae1 caoe cneunqnko.
B cnepykueH paspene Hm pe1anLno npoanannsnpyeH, kak a 1akoH cnyuae Hoxe1 6m1L
ncnonLsoaano nacnepoaanne pnn ynpouennn peannsaunn pasnnunmx c1onok kap1. Hpen
ncna yxe ceuac: cospa1L knacc c1onkn c ocnoanmHn pec1annHn n pnn kaxpo konkpe1no
c1onkn nepeonpepenn1L ero.
8.4. C1onkn kap1 nacneqoaanne a qec1ann
3naun1enLnan uac1L noaepennn, ko1opoe Hm cansmaaeH co c1onko kap1, nanne1cn
o6unH pnn acex 1nnoa c1onok a nrpe. HanpnHep, kaxpan c1onka copepxn1 cansnm cnncok
kap1; onepaunn po6aanennn n ypanennn aneHen1oa ns a1oro cansnoro cnncka 1oxe noxoxn.
[pyrne onepaunn, ko1opmH npnnncano noaepenne no yHonuannk o1 knacca CardPile,
nnorpa nepeonpepennk1cn pnn pasnmx nopknaccoa. Knacc CardPile nokasan a
nuc1uure 8.5.
Kaxpan c1onka kap1 copepxn1 kooppnna1m caoero aepxnero neaoro yrna, a 1akxe
cansnm cnncok kap1 a c1onke. Bce a1n snauennn yc1anaannaak1cn konc1pyk1opoH knacca.
Honn pannmx o6Lnanenm kak protected n 1aknH o6pasoH poc1ynnm 1onLko He1opaH knacca
(nnn ero nopknacca).
Tpn qynkunn top(), pop() n empty(), Hannnynnpykune cnnckoH kap1, ncnonLsyk1
nn1epqec, npepoc1aanneHm knaccoH LinkedList. Hoaan kap1a po6aanne1cn a cnncok ny1eH
amsoaa addCard(Card). Ona Hopnqnunpye1cn any1pn nopknaccoa. O6pa1n1e annHanne:
He1op knacca front() cansnoro cnncka aosapauae1 snauenne 1nna Object. Ono ponxno 6m1L
npeo6pasoaano k 1nny pannmx Card a qynkunnx top() n pop().
Ruc1uur 8.5. Onncanne knacca 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()
{
return cardList.empty();
}
public Card pop()
{
Card result = (Card) cardList.front();
cardList.pop();
return result;
}
// nw+ecnezyxmwe wnorza nepeonpezennxicn
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)
{
converted to PDF by BoJIoc
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; }
// xoopzwnai- cionxw xapi
protected int x;
protected int y;
protected LinkedList cardList;
}

Oc1aaunecn nn1L onepaun nannk1cn 1nnnunmHn c 1oukn spennn naue a6c1pakunn
c1onkn nrpanLnmx kap1. Opnako onn pasnnuak1cn a pe1annx a kaxpoH o1penLnoH cnyuae.
HanpnHep, qynkunn canTake(Card) sanpaunaae1, Hoxno nn nonoxn1L kap1y a pannyk
c1onky. Kap1a Hoxe1 6m1L po6aanena k ocnoaannk, 1onLko ecnn ona cnepye1 no
c1apunnc1ay n nHee1 1y xe Hac1L, u1o n aepxnnn kap1a ocnoaannn (nnn ecnn kap1a 1ys, a
c1onka nyc1a). C ppyro c1oponm, kap1a Hoxe1 6m1L po6aanena a c1onky packnapa, 1onLko
ecnn 1) uae1 kap1m npo1naononoxen uae1y 1ekyue aepxne kap1m a c1onke n 2) kap1a
nHee1 cnepykuee no panry Hnapuee snauenne, ueH aepxnnn kap1a a c1onke nnn 3) c1onka
nyc1a, a kap1a nanne1cn koponeH.
[ec1ann nn1n anp1yanLnmx qynkun, onpepenennmx a knacce CardPile, Hory1 6m1L
oxapak1epnsoaanm 1ak:
includes
onpepenne1, copepxa1cn nn kooppnna1m, nepepannme a kauec1ae apryHen1oa,
any1pn rpannu c1onkn. [ec1ane no yHonuannk npoc1o npoaepne1 caHyk aepxnkk
kap1y c1onkn. [nn c1onkn DeckPile a1o pec1ane nepeonpepeneno kak npoaepka acex
kap1, copepxaunxcn a c1onke.
canTake
coo6uae1, Hoxno nn nonoxn1L pannyk kap1y a c1onky. TonLko c1onka DeckPile n
ocnoaannn SuitPile Hory1 npnnnHa1L kap1m, noa1oHy pec1ane no yHonuannk
aepny1L ne1. B payx amueynoHnny1mx knaccax c1onok kap1 a1o pec1ane
nepeonpepenne1cn.
addCard
po6aanne1 kap1y k cnncky kap1 (k c1onke). [nn npoHexy1ouno c1onkn kap1
DiscardPile a1o pec1ane nepeonpepenne1cn 1ak, u1o6m rapan1npoaa1L, u1o kap1a
nexn1 kap1nnko aaepx.
display
o1o6paxae1 na akpane c1onky kap1. Ho yHonuannk a1o1 He1op npoc1o nokasmaae1
caHyk aepxnkk kap1y c1onkn, no pnn knacca c1onok packnapa TablePile on
saHenne1cn na nokas kononkn kap1. Hpn a1oH o1o6paxae1cn aepxnnn nonoanna
kaxpo ckpm1o kap1m. Tak u1o ns acex kap1 1ako c1onkn nan6onee paneko
o1c1onunHn okasmaak1cn caHan nepaan n caHan nocnepnnn kap1m. D1o nosaonne1
onpepenn1L rpannum, sannHaeHme c1onko kap1.
select
amnonnne1 pec1ane a o1ae1 na uenuok Hmun. Oynkunn amsmaae1cn, korpa
nonLsoaa1enL am6npae1 c1onky kap1 uenukoH HmuLk a o6nac1n c1onkn. Ho
yHonuannk ne penae1cn nnuero, no pnn c1onok packnapa TablePile, konopm DeckPile
n npoHexy1ouno c1onkn DiscardPile ono nepeonpepenne1cn na onepaunk
posmrpmua aepxne kap1m, ecnn a1o aosHoxno.
Cnepykuan 1a6nnua nnnkc1pnpye1 nonLsy nacnepoaannn. [anm nn1L onepa1opoa n nn1L
knaccoa, 1ak u1o nHee1cn 25 no1enunanLnmx He1opoa, ko1opme Hm ponxnm 6mnn 6m
onpepenn1L. HcnonLsyn nacnepoaanne, Hm ponxnm peannsoaa1L 1onLko 13 He1opoa. Bonee
converted to PDF by BoJIoc
1oro, naH rapan1npoaano, u1o kaxpan c1onka 6ype1 pearnpoaa1L opnnakoamH o6pasoH na
noxoxne sanpocm.

CardPile SuitPile DeckPile DiscardPile TablePile
includes * *
canTake * * *
addCard * *
display * *
select * * * *
8.4.1. Ocnoaanne SuitPile
Mm pe1anLno paccHo1pnH kaxpm ns nopknaccoa CardPile, saoc1pnn annHanne na
pasnnunmx caoc1aax o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn no Hepe nx
nponanennn. CaHm npoc1o nopknacc a1o ocnoaannn SuitPile. On nokasan a
nuc1uure 8.6. C1onka nexn1 a aepxneH yrny c1ona, a ne naxopn1cn kap1m opno Hac1n o1
1ysa po koponn.
Ruc1uur 8.6. Knacc 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()) &&
(aCard.rank() == 1 + topCard.rank());
}
}
Knacc SuitPile onpepenne1 1onLko paa He1opa. Ero konc1pyk1op 6epe1 paa
uenouncnennmx apryHen1a n ne penae1 nnuero, kpoHe amsoaa konc1pyk1opa napknacca
CardPile. O6pa1n1e annHanne na knkueaoe cnoao super, ykasmaakuee popn1enLckn knacc.
Me1op canTake onpepenne1, Hoxno nnn ne1 noHec1n1L kap1y a c1onky. HepeHeuenne kap1m
sakonno, ecnn c1onka nyc1a n a1a kap1a 1ys nnn ecnn a1a kap1a 1o xe Hac1n, u1o n
aepxnnn kap1a a c1onke, n ee panr cnepykun no c1apunnc1ay (nanpnHep, 1poka nnk
Hoxe1 6m1L nonoxena 1onLko na paoky nnk).
Bce oc1anLnoe noaepenne c1onkn SuitPile 1akoe xe, kak n y o6ue c1onkn kap1. Hpn
am6ope HmuLk ocnoaanne ne amnonnne1 nnkaknx pec1an. Korpa kap1a po6aanne1cn, ona
npoc1o ac1aanne1cn a cansnm cnncok. [nn o1o6paxennn c1onkn na akpane pncye1cn 1onLko
aepxnnn kap1a.
8.4.2. Konoqa DeckPile
Knacc DeskPile (nuc1uur 8.7) o6cnyxnaae1 ncxopnyk konopy kap1. Ona o1nnuae1cn o1
c1onkn kap1 o6uero 1nna payHn HoHen1aHn. Hpn konc1pynpoaannn akseHnnnpa aHec1o
nyc1o c1onkn knacc cospae1 nonnyk konopy ns 52 kap1, ac1aannn nx a cnyuanoH nopnpke
a cansnm cnncok. HopnporpaHHa random 6n6nno1ekn nsmka Java renepnpye1 cnyuanyk
aennunny c paono 1ounoc1Lk a pnanasone o1 0 po 1. Ona npeo6pasye1cn a cnyuanoe
uenoe uncno ao apeHn npouecca 1acoaannn konopm.
converted to PDF by BoJIoc
Me1op select amsmaae1cn, korpa uenuok Hmun nponsaopn1cn nap konopo DeskPile. Ecnn
ona nyc1a, 1o nnuero ne nponcxopn1. B npo1nanoH cnyuae aepxnnn kap1a ypanne1cn ns
konopm n po6aanne1cn a npoHexy1ounyk c1onky.
B nsmke Java ne1 rno6anLnmx nepeHennmx. Korpa snauenne ncnonLsye1cn neckonLknHn
o6Lek1aHn knaccoa (1aknHn, kak pasnme c1onkn kap1 a naueH nacLnnce), nepeHennan
o6Lnanne1cn c knkueamH cnoaoH static. Kak Hm yanpnH a rnaae 20, npn a1oH cospae1cn
opna konnn c1a1nuecko nepeHenno, ko1opan poc1ynna aceH akseHnnnpaH. B panno
nporpaHHe c1a1nueckne nepeHennme npnHennk1cn pnn xpanennn pasnnunmx c1onok kap1.
Onn 6ypy1 copepxa1Lcn a akseHnnnpe knacca Solitare, ko1opm Hm onnueH anocnepc1ann.
[nn poc1yna k nnH Hm ncnonLsyeH nonnoc1Lk cneunqnunpoaannoe nHn, ko1opoe kpoHe
nHenn nepeHenno aknkuae1 nasaanne knacca. D1o nokasano a He1ope select
(nuc1uur 8.8), ko1opm o6pauae1cn k nepeHenno Solitare.discardPile pnn poc1yna k
npoHexy1ouno c1onke.
Ruc1uur 8.7. Knacc DeckPile
class DeckPile extends CardPile
{
DeckPile (int x, int y)
{
// cnauana wnwuwanwswpyeicn nazxnacc
super(x, y);
// saiem coszaeicn nosan xonoza
// cnauana ona xnazeicn s noxansnyx cionxy
CardPile pileOne = new CardPile(0, 0);
CardPile pileTwo = new CardPile(0, 0);
int count = 0;
for (int i = 0; i < 4; i++)
{
pileOne.addCard(new CArd(i, j));
count++;
}
// saiem cnyuanno s-iacxwsaeicn xapia
for (; count > 0; count--)
{ int limit = ((int)(Math.random() * 1000))
% count;
// nepememaeicn snws s cnyuannoe mecio
for (int i = 0; i < limit; i++)
pileTwo.addCard(pileOne.pop());
// noiom zoasnneicn xapia oicxza
addCard(pileOne.pop());
// saiem xonoz- cxnaz-saxicn opaino
while (! pileTwo.empty())
pileOne.addCard(pileTwo.pop());
}
}
public void select(int tx, int ty)
{
if (empty())
return;
Solitaire.discardPile.addCard(pop());
}
}


converted to PDF by BoJIoc
8.4.3. HpoHexy1ounan c1onka DiscardPile
Knacc DiscardPile (cH. nnc1nnr 8.8) nn1epecen 1eH, u1o on peHonc1pnpye1 pae
coaepuenno pasnme qopHm nacnepoaannn. Me1op select saHeuae1 nnn nepeonpepenne1
noaepenne, no yHonuannk o6ecneunaaeHoe knaccoH CardPile. Hoam kop npn amsoae (1o
ec1L npn naxa1nn knonkn Hmun a o6nac1n c1onkn) npoaepne1, Hoxe1 nn aepxnnn kap1a
6m1L nepeHeuena na kakoe-nn6ypL ocnoaanne nnn na opny ns c1onok packnapa. Ecnn kap1a
ne Hoxe1 6m1L nepeHeuena, ona oc1ae1cn a npoHexy1ouno c1onke.
Me1op addCard peHonc1pnpye1 ppyro 1nn nepeonpepenennn. 3pecL noaepenne y1ounne1
qynkunonanLnoc1L napknacca. To ec1L nonnoc1Lk o1pa6a1maae1cn noaepenne napknacca n,
kpoHe 1oro, po6aanne1cn noaoe noaepenne. B pannoH cnyuae noam kop rapan1npye1, u1o
korpa kap1a nexn1 a npoHexy1ouno c1onke, ona acerpa 6ype1 cHo1pe1L kap1nnko aaepx.
Hocne 1oro kak a1o ycnoane ypoane1aopeno, ny1eH nocmnkn coo6uennn pnn
nceaponepeHenno super amsmaae1cn kop napknacca, ko1opm po6aanne1 kap1y a c1onky.
[pyran qopHa y1ounennn aosnnkae1 pnn konc1pyk1opoa pasnnunmx nopknaccoa. [o 1oro
kak konc1pyk1op amnonnn1 caon co6c1aennme pec1ann, kaxpm ns nnx ponxen amsaa1L
konc1pyk1op napknacca, pa6m rapan1npoaa1L, u1o npepok nnnunannsnpoaancn ponxnmH
o6pasoH. Konc1pyk1op npepka amsmaae1cn uepes nceapo-nepeHennyk super; on amsmaae1cn
kak qynkunn any1pn konc1pyk1opa pouepnero knacca. B rnaae 11 Hm noroaopnH noppo6nee
o pasnnunn Hexpy saHeuenneH n y1ounenneH npn nepeonpepenennn He1opoa.
Ruc1uur 8.8. Knacc DiscardPile
class discardPile extends CardPile
{
DiscardPile (int x, int y)
{
super (x, y);
}
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;
}
}
converted to PDF by BoJIoc
// nwxio ne mo+ei ee wcnonssosais,
// nono+wm ee nasaz
addCard(topCard);
}
}
8.4.4. C1onka packnaqa TablePile
Han6onee cnoxnm ns nopknaccoa knacca CardPile a1o 1o1, ko1opm ncnonLsye1cn pnn
xpanennn c1onok packnapa TablePile. On nokasan a nuc1uurax 8.9 n 8.10. C1onkn
packnapa o1nnuak1cn o1 c1onok kap1 o6uero nasnauennn cnepykunHn HoHen1aHn:
Hpn nnnunannsaunn (c noHouLk konc1pyk1opa) c1onkn packnapa sa6npak1
onpepenennoe konnuec1ao kap1 ns konopm, nepeHeuan nx k ce6e. Konnuec1ao kap1,
ypanennmx 1aknH o6pasoH, onpepenne1cn pononnn1enLnmH apryHen1oH,
nepepaaaeHmH konc1pyk1opy. Bepxnnn kap1a c1onkn o1kpmaae1cn.
Kap1a Hoxe1 6m1L po6aanena a c1onky (npoaepne1cn He1opoH canTake), 1onLko ecnn
c1onka nyc1a n a1a kap1a koponL nnn ecnn kap1a okasmaae1cn npo1naononoxnoro
uae1a no cpaanennk c 1ekyue aepxne kap1o c1onkn n ee panr na epnnnuy
HenLue, ueH panr aepxne kap1m.
[nn npoaepkn nonapannn uenuka Hmun a npepenm o6nac1n c1onkn (He1op includes)
yun1maak1cn 1onLko neaan, npaaan n aepxnnn rpannum. Hnxnnn rpannua
nrnopnpye1cn, 1ak kak c1onka packnapa TablePile Hoxe1 6m1L nepeHenno pnnnm.
Hpn uenuke HmuLk a npepenax c1onkn kap1 packnapa aepxnnn kap1a o1kpmaae1cn,
ecnn ona 6mna sakpm1a. Ecnn kap1a o1kpm1a, 1o penae1cn nonm1ka nepeHec1n1L ee
cnepaa a kakoe-nn6ypL ocnoaanne, a sa1eH a kakyk-nn6ypL c1onky packnapa.
TonLko ecnn nn opna ns c1onok ne Hoxe1 npnnn1L kap1y, ona oc1ae1cn na Hec1e.
[nn o1o6paxennn c1onkn na akpane kap1m a ne pncyk1cn ppyr sa ppyroH, 1ak u1o
kaxpan cnepykuan kap1a noneHnory cpanrae1cn anns. [nn 1oro u1o6m cpena1L a1o c
nnxne po aepxne kap1m aknkun1enLno, ne6onLuan pekypcnanan nopnporpaHHa
(o6Lnanennan kak private) npocHa1pnaae1 aecL cansnm cnncok, o1o6paxan kap1m a
1o1 HoHen1, korpa ynpaanenne aosapauae1cn nasap nocne pekypcnanoro amsoaa.
D1a qynkunn ncnonLsye1 n1epa1op pnn unkna no aneHen1aH cnncka.
8.5. HonuHopquan urpa
Kak Hm yxe anpenn a 3apaue o aocLHn qepsnx (rnaaa 5), cpepa pnn acex npnnoxenn
na nsmke Java o6ecneunaae1cn knaccoH Applet. [nn cospannn noaoro npnnoxennn
nporpaHHnc1 onpepenne1 nopknaccm Applet, nepeonpepennn npn a1oH pasnnunme He1opm.
Knacc Solitare, ko1opm nanne1cn uen1panLnmH knaccoH nauero npnnoxennn, nokasan a
nuc1uure 8.11.
Mm panee yxe o1Heuann, u1o nepeHennme, ko1opme xpann1 o6une pnn acex o6Lek1oa
pannme, o6Lnannk1cn c knkueamH cnoaoH static. Takne nonn nnnunannsnpyk1cn a He1ope
init knacca1.
Maccnam a nsmke Java a1o neu1o, o1nnunoe o1 Haccnaoa a 6onLunnc1ae ppyrnx
nsmkoa nporpaHHnpoaannn. Java pasnnuae1 pnn Haccnaoa 1pn pec1ann: o6Lnanenne,
pacnpepenenne n npncaanaanne. 3aHe1L1e, u1o o6Lnanenne



converted to PDF by BoJIoc
Ruc1uur 8.9. Knacc TablePile, uac1L I
class TablePile extends CardPile
{
TablePile (int x, int y, int c)
{
// wnwuwanwsauwn nazxnacca
super(x, y);
// saiem wnwuwanwswpyeicn nama cionxa xapi
for (int i = 0; i < c; i++)
{
addCard(Solitaire.deckPile.pop());
}
// sepxnnn xapia oixp-saeicn
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)
{
// ne nposepnei nw+nxx rpanwuy
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);
aCard.draw(g, x, localy);
return localy + 35;
}
...

Ruc1uur 8.10. Knacc TablePile, uac1L II
class TablePile extends CardPile
{
...
public void select (int tx, int ty)
{
if (empty())
return;
// ecnw xapia saxp-ia, nepesepnyis
Card topCard = top();
if (! topCard.faceUp())
{
converted to PDF by BoJIoc
topCard.flip();
return;
}
// wnaue cmoipwm, mo+no nw ee nono+wis s ocnosanwe
topCard = pop();
for (int i = 0; i < 4; i++)
{
if (Solitaire.suitPile[i].canTake(topCard))
{
Solitaire.suitPile[i].addCard(topCard);
return;
}
}
// nenssn nw nono+wis s zpyryx cionxy pacxnaza
for (int i = 0; i < 7; i++)
{
if (Solitaire.tableau[i].canTake(topCard))
{
Solitaire.tableau[i].addCard(topCard);
return;
}
}
// wnaue xnazem opaino
addCard(topCard);
}
public void display (Graphics g)
{
stackDisplay(g, cardList.iterator());
}
}
Ruc1uur. 8.11. Knacc Solitaire
public class Solitaire extends Applet
{
static DeckPile deckPile;
static DisacrdPile discardPile;
static TablePile tableau [ ];
static SuitPile suitPile [ ];
static CardPile allPiles [ ];
public void init()
{
// cnauana oisozwm mecio noz maccws-
allPiles = new CardPile[13];
suitPile = new SuitPile[4];
tableau = new TablePile[7];
// saiem sanonnnem wx zann-mw
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);
}
converted to PDF by BoJIoc
}
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;
}
}
nokasmaae1 1onLko 1o, u1o o6Lek1m nannk1cn HaccnaaHn; npo nx rpannum nnuero ne
roaopn1cn. Opnn ns nepamx uaroa npouepypm nnnunannsaunn ampenenne Hec1a nop 1pn
Haccnaa (ocnoaannn, c1onkn packnapa n Haccna allPiles, ko1opm Hm paccHo1pnH nnxe).
KoHanpa new o1aopn1 naHn1L pnn a1nx Haccnaoa, no ne npncaanaae1 nnkaknx snauenn nx
aneHen1aH.
Cnepykun uar cospanne konopm DeskPile. BcnoHnn1e, u1o konc1pyk1op a1oro knacca
renepnpye1 n nepe1acoamaae1 nonnyk konopy ns 52 kap1. HpoHexy1ounan c1onka
DiscardPile cospae1cn ananornunmH o6pasoH. 3a1eH a unkne nopoxpak1cn n
nnnunannsnpyk1cn ue1mpe ocnoaannn SuitPile, a a1opo unkn cospae1 n nnnunannsnpye1
c1onkn packnapa TablePile. BcnoHnn1e, u1o npn nnnunannsaunn c1onok packnapa kap1m
6epy1cn ns konopm n ac1aannk1cn a c1onky packnapa.
Maccna allPiles ncnonLsye1cn pnn npepc1aanennn acex 13 c1onok kap1. 3aHe1L1e, u1o kak
1onLko cospae1cn ouepepnan c1onka, e 1y1 xe npncaanaae1cn nueka a a1oH Haccnae,
paano kak n coo1ae1c1aykuan c1a1nueckan nepeHennan. Mm aocnonLsyeHcn a1nH HaccnaoH
pnn nnnkc1paunn eue opnoro acnek1a nacnepoaannn. Cnepyn npnnunny nopc1anoakn,
allPiles o6Lnanen kak Haccna ns aneHen1oa c 1nnoH pannmx CardPile, no na caHoH pene on
copepxn1 c1onkn kap1 pasnoo6pasnoro anpa.
[annm Haccna ncnonLsye1cn a cn1yaunnx, korpa pasnnunn Hexpy 1nnaHn c1onok kap1
ne aaxnm. HanpnHep, a npouepype nepepncoakn akpana kaxpyk c1onky npoc1o npocn1
caHoc1on1enLno nepepncoaa1L ce6n. HoxoxnH o6pasoH npn uenuke HmuLk onpaunaae1cn
kaxpan c1onka, ne copepxn1 nn ona ykasannyk 1ouky akpana. Ecnn pa, 1o c1onka
ampenne1cn. Cpepn nnx ec1L ceHL c1onok packnapa, ue1mpe ocnoaannn, npoHexy1ounan
c1onka n konopa. Bonee 1oro, qak1nueckn kop, ncnonnneHm a o1ae1 na amsoa He1opoa
select n includes, Hoxe1 pasnnua1Lcn a saancnHoc1n o1 1nna o6pa6a1maaeHo c1onkn.
HcnonLsoaanne nepeHennmx, o6Lnanennmx kak akseHnnnpm popn1enLckoro knacca, no
copepxaunx snauennn, o1nocnunecn k nopknaccaH, a1o opnn ns acnek1oa nonnMopqnsMa
(1eHa, k ko1opo Hm aepneHcn a cnepykue rnaae).
8.6. Cosqanne 6onee cnoxno nrpm
HacLnnc, onncannm specL, o6napae1 HnnnHanLnmHn aosHoxnoc1nHn n a neH
upesamuano 1pypno amnrpa1L. Bonee peannc1nunan nrpa aknkuana 6m no kpane Hepe
neko1opme ns cnepykunx aapnaun:
converted to PDF by BoJIoc
Me1op select a knacce TablePile cnepye1 pacunpn1L po pacnosnaaannn
nocnepoaa1enLnoc1e (nanoHnnH: nocnepoaa1enLnoc1L a1o uac1L c1onkn,
coc1onuan ns o1kpm1mx kap1 packnapa, ko1opan nepeHeuae1cn a ppyryk c1onky
packnapa kak epnnoe uenoe). B a1oH cnyuae ecnn aepxnnn kap1a c1onkn ne Hoxe1
6m1L nepeHeuena, 1o nyxno cpena1L npoaepky pnn caHo nnxne o1kpm1o kap1m.
Ecnn ee Hoxno nepenec1n, 1o aecL na6op o1kpm1mx kap1 ponxen 6m1L nepeHeuen a
ppyryk c1onky.
Haua nrpa oc1anaannaae1cn nocne opnoro npocHo1pa konopm. AnL1epna1naa: korpa
nonLsoaa1enL am6npae1 nyc1yk c1onky konopm (uenkan Hmuko 1aH, rpe 6mna
konopa), 1o npoHexy1ounan c1onka cknapmaae1cn o6pa1no a konopy, nosaonnn
npoponxn1L nacLnnc.
[pyrne anL1epna1nanme npaanna onncanm a ynpaxnennnx.
Ynpaxnennn
1. D1o1 nacLnnc 6mn npepnaHepenno cospan kak Hoxno 6onee npoc1mH. He6ora1m
na6op aosHoxnoc1e cnerka pasppaxae1, ne npaapa nn? Ero nerko pacunpn1L sa
cue1 po6aanennn kopa. BosHoxnme noame caoc1aa:
a. aepxnnn kap1a c1onkn packnapa ne ponxna nepeHeua1Lcn a ppyryk c1onky
packnapa, ecnn nop ne nHee1cn o1kpm1an kap1a;
b. uenan nocnepoaa1enLnoc1L ne Hoxe1 nepeHeua1Lcn, ecnn caHan nnxnnn
kap1a koponL n ne oc1anocL sakpm1mx kap1.
[nn kaxporo cnyuan onnun1e, kakne npouepypm 1pe6yk1 nsHenenn, n coc1aaL1e
pnn nnx ncnpaanennm kop.
2. Hnxe cnepyk1 o6uensaec1nme aapnaunn kocmnkn. Onnun1e pnn kaxpo ns nnx,
kakne uac1n nacLnnca ponxnm 6m1L nsHenenm.
a. Ecnn nonLsoaa1enL uenknyn HmuLk no nyc1o c1onke konopm, 1o
npoHexy1ounan c1onka nepeHeuae1cn (aosHoxno, c nepeHeunaanneH) nasap
a konopy. TaknH o6pasoH nonLsoaa1enL Hoxe1 nepe6npa1L konopy
neopnokpa1no.
b. Kap1m Hory1 6m1L nepepanny1m ns ocnoaann nasap a c1onky packnapa.
c. Kap1m am1nrnaak1cn ns konopm no 1pn cpasy n pacnonarak1cn a
npoHexy1ouno c1onke a o6pa1noH nopnpke. Kak n npexpe, a nrpe yuac1aye1
1onLko caHan aepxnnn kap1a a npoHexy1ouno c1onke. Ecnn a konope
oc1ae1cn HenLue 1pex kap1, 1o ace oc1aaunecn kap1m nepeHeuak1cn a
npoHexy1ounyk c1onky. (Ha npak1nke a1a pasnoanpnoc1L uac1o coue1ae1cn c
aapnan1oH a, o6ecneunaan Hnoropasoam npoxop konopm.)
d. To xe, u1o aapnan1 a, no a nrpe npnnnHae1 yuac1ne nk6an ns 1pex kap1
npoHexy1ouno c1onkn. (D1o 1pe6ye1 ne6onLuoro nsHenennn ao aneuneH
anpe kap1ounoro c1ona, n 6onLunx ncnpaanenn a knacce npoHexy1ouno
c1onkn.)
e. Ha nyc1yk c1onky packnapa Hoxe1 6m1L nonoxena nk6an qnrypnan kap1a
(koponL, paHa, aane1), a ne 1onLko koponL.
3. HacLnnc thumb and pouch noxox na kocmnky sa ncknkuenneH 1oro, u1o kap1a
Hoxe1 6m1L nonoxena na ppyryk kap1y cnepykuero no c1apunnc1ay panra n nk6o
Hac1n sa ncknkuenneH ee co6c1aenno. Tak, pean1ky nnk paspeuae1cn knac1L na
pecn1ky kpec1e, no ne na pecn1ky nnk. D1a pasnoanpnoc1L snaun1enLno
yaennunaae1 uancm na no6epy. (Cornacno Mopexepy [Morehead 1949], uancm na
no6epy a Klondike coc1aannk1 1 k 30, 1orpa kak a thumb and pouch 1 k 4.)
converted to PDF by BoJIoc
Onnun1e, kakne qparHen1m nporpaHHm 1pe6yk1 npncnoco6nennn k noaoHy
aapnan1y.
Fnaaa 9



Hoa1opuoe
ucnonusoaauue
kopa
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne 6mno o6Lnaneno kak 1exnonornn,
ko1opan nosaonn1 nakoneu konc1pynpoaa1L nporpaHHm ns Hnorokpa1no ncnonLsyeHmx
koHnonen1 o6uero nasnauennn. Takne aa1opm, kak Bpap Kokc, saunn 1ak paneko, u1o yxe
roaopnnn o6 o6Lek1no-opnen1npoaannoH nopxope kak o npepaec1nnke npoHmunenno
peaonkunn a paspa6o1ke nporpaHHnoro o6ecneuennn [Cox 1986]. Hoka pec1an1enLnoc1L
ne anonne coo1ae1c1aye1 oxnpannnH nnonepoa OOH (1eHa, k ko1opo Hm eue o6pa1nHcn a
konue a1o rnaam). 1o pec1an1enLno cnpaaepnnao 1ak a1o 1o, u1o OOH nosaonne1
ac1panaa1L Hnorokpa1no ncnonLsyeHme nporpaHHnme koHnonen1m ropaspo nn1encnanee,
ueH panLue. B a1o rnaae Hm paccHo1pnH paa nan6onee o6unx HexannsHa Hnorokpa1noro
ncnonLsoaannn nporpaHHnoro o6ecneuennn, ko1opme nsaec1nm kak nacneoaanne n
koMnosnnn.
MexannsHm Hnorokpa1noro ncnonLsoaannn a1o 1onLko nepam uar. Hacnepoaanne n
koHnosnunn o6ecneunaak1 cpepc1aa Hnorokpa1noro ncnonLsoaannn, no u1o6m 6m1L
aqqek1nanmHn, onn ponxnm, aoo6ue roaopn, npnHenn1Lcn a epnno cpepe paspa6o1kn,
ko1opan pacnonarae1 noppepxko Hnorokpa1noro ncnonLsoaannn. CxeHm n cpepm
paspa6o1kn, ko1opme npepoc1aannk1 1akoe okpyxenne, 6ypy1 paccHo1penm a rnaae 18.
9.1. Hacneqoaanne n npnnnn noqc1anoakn
HacnepoaannenkoHnosnunkakauec1ae1exnnknHnorokpa1noro ncnonLsoaannn kopa,
aosHoxno, nerue nonn1L a nx cansn c npnnunnoH nopc1anoakn. BcnoHnn1e rnaay 8, a
ko1opo Hm ccmnanncL na a1o1 npnnunn a cansn c nepeHenno, o6Lnanenno c opnnH
knaccoH, ko1opan nonyuae1 snauenne ns ppyroro knacca. Hpnnunn nopc1anoakn y1aepxpae1,
u1o ponyc1nHo npncaanaa1L snauenne nepeHenno, ecnn knacc snauennn nanne1cn knaccoH
nepeHenno nnn ero nopknaccoH.
Mm anpenn npnHepm nepeonpepenennn npn Hopennpoaannn nrpm a 6nnLnpp a rnaae 6. B
npouepype, ko1opan pncye1 o6pas akpana, nepeHennan 6mna o6Lnanena kak
npnnapnexauan knaccy GraphicalObject, no na caHoH pene ona nocnepoaa1enLno copepxana
a kauec1ae snauenn pasnnunme o6Lek1m, kaxpm ns ko1opmx nannncn akseHnnnpoH
nopknacca knacca GraphicalObject.
B a1oH paspene Hm o6cypnH ne nac1onune knaccm, a a6c1pak1nme konuenunn,
nporpaHHno peannsaune ko1opmx amc1ynak1 knaccm. Hpn kaknx ycnoannx opno
a6c1pak1noe nonn1ne Hoxno nopc1aan1L aHec1o ppyroro? To ec1L npn kaknx ycnoannx
akseHnnnp neko1oporo a6c1pak1noro nonn1nn nepec1anoaouen c akseHnnnpoH ppyroro
a6c1pak1noro nonn1nn? Opno ns knaccnuecknx npaann npnHenneHmx specL, c1aauee
ocnoanmH pnn o6Lek1no-opnen1npoaannoro npoek1npoaannn, nsaec1no kak 6m1L
akseHnnnpoH (npaanno is-a).
converted to PDF by BoJIoc
9.1.1. Bm1u skseHnnnpoH n aknmua1u kak uac1u
3nanne payx pasnnunmx qopH o1nouenn ocnoaa nonnHannn 1oro, kak n korpa
npnHenn1L npneHm Hnorokpa1noro ncnonLsoaannn kopa. HHek1cn paa 1nna o1nouenn,
nsaec1nmx kak u1u akseMnnnpoM n aknkua1u kak uac1u (is-a n has-a).
O1nouenne u1u akseMnnnpoM nHee1 Hec1o Hexpy payHn nonn1nnHn, ecnn nepaoe
nanne1cn y1ounenneH a1oporo. To ec1L pnn acex npak1nuecknx uene noaepenne n pannme,
cansannme c 6onee konkpe1nmH nonn1neH, coc1aannk1 nopHnoxec1ao noaepennn n pannmx,
cansannmx c 6onee a6c1pak1nmH nonn1neH. HanpnHep, ace npnHepm nacnepoaannn,
onncannme naHn a npepmpyunx rnaaax, ypoane1aopnk1 o1nouennk u1u akseMnnnpoM
(xosnka uae1ounoro Harasnna Florist nanne1cn akseHnnnpoH knacca anapenLuea Harasnna
Shopkeeper, co6aka Dog nanne1cn akseHnnnpoH knacca Hnekonn1akunx Mammal,
6nnLnppnm uap Ball nanne1cn akseHnnnpoH knacca rpaqnuecknx o6Lek1oa GraphicalObject,
n 1. p.).
Hasaanne a1oro o1nouennn nponcxopn1 ns npoc1oro npaanna npoaepkn. 1o6m
onpepenn1L, nanne1cn nn nonn1ne X y1ounennmH aapnan1oH Y, npoc1o coc1aaL1e
npepnoxenne X nanne1cn akseMnnnpoM Y. Ecnn y1aepxpenne sayun1 koppek1no, 1o ec1L
ono coo1ae1c1aye1 aaueHy xnsnennoHy onm1y, 1o am Hoxe1e saknkun1L, u1o X n Y cansanm
o1nouenneH u1u akseMnnnpoM.
Hanpo1na, o1nouenne aknkua1u kak uac1u nHee1 Hec1o, korpa a1opoe nonn1ne nanne1cn
koHnonen1o nepaoro, no o6a a1n nonn1nn ne coanapak1 nn a kakoH cHmcne nesaancnHo o1
ypoann o6unoc1n a6c1pakunn. HanpnHep, aa1oHo6nnL Car nMee1 panra1enL Engine, xo1n
ncno, u1o a1o ne 1o1 cnyua, korpa Car nanne1cn akseMnnnpoM Engine nnn Engine nanne1cn
akseMnnnpoM Car. Car 1eH ne Henee nanne1cn akseMnnnpoM knacca aa1oHo6nne Vehicle,
ko1opm a caok ouepepL nanne1cn akseMnnnpoM knacca cpepc1a nepepanxennn
MeansOtTransportation 1.

1. Ycnoane X nanne1cn akseHnnnpoH Y ne ponxno 1pak1oaa1Lcn nn kak y1aepxpenne
X nanne1cn nopHnoxec1aoH Y, paccHa1pnaaeHoe a 1eope1nko-Hnoxec1aennoH
cHmcne, nn kak y1aepxpenne X nanne1cn napaunaanneH Y a cHmcne pacunpennn
c1pyk1ypm pannmx. Ycnoane X nanne1cn akseHnnnpoH Y cnpaaepnnao, ecnn
cyunoc1L X a1o y1ounenne, pe1annsaunn, konkpe1nsaunn, cneunannsnpoaannan
qopHa, n 1. p. cyunoc1n Y, n nn a kakoH ppyroH cnyuae. B uac1noc1n, npnaopnHm
panee npnHep c aa1oHo6nneH n panra1eneH amrnnpn1 cnepykunH o6pasoH:
panra1enL axopn1 a aa1oHo6nnL a cHmcne 1eopnn Hnoxec1a (panra1enL 6onee
Henkan noppo6noc1L n qparHen1 yc1poc1aa aa1oHo6nnn), aa1oHo6nnL ycnoxnne1
panra1enL (aa1oHo6nnL a1o panra1enL uennkoH nnkc eue Hnoro ppyroro), no nn a
1oH, nn a ppyroH cnyuae ne cnpaaepnnao ycnoane X nanne1cn akseHnnnpoH Y.
HpnHeu. nepea.

Eue pas, u1o6m npoaepn1L o1nouenne aknkua1L kak uac1L, npoc1o coc1aaL1e
npepnoxenne X aknkuae1 Y kak uac1L n npepoc1aaL1e peua1L sppaaoHy cHmcny.
B 6onLunnc1ae cnyuaea pasnnune ncno. Ho nnorpa ono Hoxe1 6m1L coHnn1enLno nnn
saance1L o1 o6c1on1enLc1a. B cnepykueH paspene Hm anannsnpyeH opnn 1ako cnyua,
u1o6m nponnnkc1pnpoaa1L paa He1opa paspa6o1kn nporpaHHnoro o6ecneuennn, ko1opme
ec1ec1aenno ocnoamaak1cn na a1nx payx o1nouennnx.
9.2. KoHnosnnn n nacneqoaanne: onncanne
1o6m nponnnkc1pnpoaa1L koHnosnunk n nacnepoaanne, Hm noc1ponH 1nn pannmx set
a6c1pakunk Hnoxec1aa na ocnoae cyuec1aykuero knacca List. DkseHnnnpm knacca List
converted to PDF by BoJIoc
copepxa1 cnnckn uenouncnennmx aennunn. [onyc1nH, u1o Hm yxe cospann knacc List co
cnepykunH nn1epqecoH:
class List
{
public:
// xoncipyxiop
List ();
// meioz-
void addToFront (int);
int firstElement ();
int length ();
int includes (int);
int remove (int);
...
};
Haua a6c1pakunn cnncka nosaonne1 naH po6aann1L noam aneHen1 a nauano cnncka,
ampaaa1L ero nepam aneHen1, naxopn1L konnuec1ao aneHen1oa, npoaepn1L, copepxn1cn nn
snauenne a cnncke, n ypann1L aneHen1 ns cnncka.
Mm xo1nH cospa1L a6c1pakunk Hnoxec1aa, u1o6m amnonnn1L 1akne onepaunn, kak
po6aanenne snauennn k Hnoxec1ay, onpepenenne konnuec1aa aneHen1oa, amncnenne
npnnapnexnoc1n k Hnoxec1ay.
9.2.1. Hcnonusoaanne koHnosnnn
Cnauana Hm nccnepyeH, Hoxe1 nn a6c1pakunn Hnoxec1aa 6m1L cospana c noHouLk
koHnosnunn. HanoHnnH, u1o o6Lek1 a1o npoc1o nnkancynnunn pannmx n noaepennn.
Korpa pnn Hnorokpa1noro ncnonLsoaannn cyuec1aykue a6c1pakunn pannmx npn cospannn
noaoro 1nna ncnonLsye1cn koHnosnunn, 1o uac1L noao c1pyk1ypm pannmx nanne1cn npoc1o
akseHnnnpoH cyuec1aykue c1pyk1ypm. D1o nokasano nnxe, rpe 1nn pannmx Set copepxn1
none, nasaannoe theData, ko1opoe o6Lnaneno c 1nnoH List.
Class Set
{
public:
Set (); // xoncipyxiop

// onepauww
void add (int);
int size ();
int includes (int)
private: // onacis zann-x znn snauenwn
List theData;
};
HockonLky a6c1pakunn List xpann1cn kak uac1L o6nac1n pannmx nauero Hnoxec1aa, ona
ponxna 6m1L nnnunannsnpoaana a konc1pyk1ope. Bypyun ananornunmHn koHanpaH
nnnunannsaunn none pannmx pnn knaccoa (rnaaa 4), koHanpm nnnunannsa1opa a nauane
konc1pyk1opa sapak1 apryHen1m pnn nnnunannsaunn none pannmx. B pannoH cnyuae
konc1pyk1op, ko1opm Hm amsmaaeH pnn knacca List, 6esapryHen1nm:
// cnwcox wnwuwanwsauww
Set::Set() : theData()
{
// nwxaxon zansnenmen wnwuwanwsauww
}
Onepaunn a noao c1pyk1ype pannmx peannsoaanm c ncnonLsoaanneH yxe
cyuec1aykunx pec1an, npepoc1aanneHmx c1apmH 1nnoH pannmx. HanpnHep, onepaunn
converted to PDF by BoJIoc
includes pnn Hnoxec1aa npoc1o amsmaae1 qynkunk c ananornunmH nasaanneH, yxe
onpepenennyk pnn cnnckoa:
int Set::size ()
{
return theData.length();
}
int Set::includes (int newValue)
{
return theData.includes(newValue);
}
TonLko opna onepaunn okasmaae1cn uy1L 6onee cnoxno. D1o po6aanenne noaoro
aneHen1a, 1ak kak nyxno cnauana y6epn1Lcn, u1o pannan aennunna ne copepxn1cn a
Hnoxec1ae (aennunnm ne Hory1 nonann1Lcn a Hnoxec1ae 6onee opnoro pasa):
void Set::add (int newValue)
{
// ecnw ne s mno+ecise
if (! Includes (newValue))
{
// iorza zoaswis
theData.addToFront(newValue);
};
// wnaue nwuero ne zenais
}
BaxnmH nanne1cn 1o1 qak1, u1o 1akan koHnosnunn noHorae1 noa1opnoHy ncnonLsoaannn
kopa a noamx npnnoxennnx. 3a cue1 cyuec1aykuero knacca List 6onLuan uac1L 1pypno
pa6o1m no ynpaanennk snauennnHn pannmx pnn naue noao koHnonen1m 6mna yxe
npopenana.
Opnako koHnosnunn nnuero ne roaopn1 o co6nkpennn npnnunna nopc1anoakn. Hpn
cospannn noaoro 1nna ykasannmH cnoco6oH a6c1pakunn List n Set 6ypy1 a6conk1no
pasnnunm, n nn opna nx nnx ne Hoxe1 6m1L nopc1aanena aHec1o ppyro.
KoHnosnnn a qpyrnx nsmkax
KoHnosnunn Hoxe1 6m1L npnHenena a nk6oH o6Lek1no-opnen1npoaannoH nsmke
nporpaHHnpoaannn, paccHa1pnaaeHoH a a1o knnre. Ho ona ac1peuae1cn n a nsmkax, ne
nannkunxcn o6Lek1no-opnen1npoaannmHn. Epnnc1aennan cyuec1aennan pasnnua a
cnoco6e nnnunannsaunn nnkancynnpoaannmx pannmx. B nsmke Smalltalk a o6ueH cnyuae
a1o amnonnne1cn uepes knacc-Me1ou, a nsmke Objective-C c noHouLk Me1ooa-qapnk,
a nsmkax Java n Object Pascal c ncnonLsoaanneH konc1pyk1opoa.
9.2.2. HpnHenenne nacneqoaannn
A6conk1no ppyrnH HexannsHoH Hnorokpa1noro ncnonLsoaannn kopa a OOH nanne1cn
nacnepoaanne. C ero noHouLk noam knacc Hoxe1 6m1L o6Lnanen kak nopknacc, nnn
pouepnn knacc, cyuec1aykuero knacca. B a1oH cnyuae ace o6nac1n pannmx n qynkunn,
cansannme c ncxopnmH knaccoH, aa1oHa1nueckn nepenocn1cn na noayk a6c1pakunk pannmx.
Hoam knacc Hoxe1 onpepenn1L pononnn1enLnme snauennn nnn qynkunn. On
nepeonpepenne1 neko1opme qynkunn ncxopnoro knacca, npoc1o o6Lnana noame c 1aknHn xe
nHenaHn, kak n a ncxopnoH knacce.
Bce a1o nponnnkc1pnpoaano nnxe a knacce, ko1opm peannsye1 ppyryk aepcnk
a6c1pakunn Set. YnoHnnan knacc List a saronoake knacca, Hm nokasmaaeH, u1o naua
a6c1pakunn Set nanne1cn pacunpenneH nnn y1ounenneH cyuec1aykuero knacca List. TaknH
o6pasoH, onepaunn, cansannme co cnnckaHn, npnHennHm n k Hnoxec1aaH:
Class Set : public List
converted to PDF by BoJIoc
{ public:
// xoncipyxiop
Set();
// onepauww
void add (int);
int size ();
};
3aHe1L1e, u1o noam knacc ne onpepenne1 nnkaknx noamx none pannmx. BHec1o a1oro
nonn pannmx knacca List 6ypy1 ncnonLsoaa1Lcn pnn xpanennn aneHen1oa Hnoxec1aa. D1n
nonn ponxnm 6m1L no-npexneHy nponnnunannsnpoaanm. [annan onepaunn amnonnne1cn
amsoaoH konc1pyk1opa napknacca a konc1pyk1ope noaoro knacca:
Set::Set() : List()
{
// nwxaxon zansnenmen wnwuwanwsauww
}
Ananornuno qynkunn, onpepenennme a popn1enLckoH knacce, Hory1 6m1L ncnonLsoaanm
6es kaknx-nn6o panLneunx ycnnn, n, cnepoaa1enLno, naH ne nyxno 6ecnokon1Lcn no
noaopy He1opa includes, 1ak kak nacnepoaannm He1op ns List nHee1 1akoe xe nHn n cnyxn1
1eH xe uennH. [o6aanenne a Hnoxec1ao noaoro aneHen1a 1pe6ye1 neHnoro 6onLue pa6o1m,
ueH a knacce List:
void Set::add (int newValue)
{
// zoaswis, ecnw nei s mno+ecise
if (! Includes(newValue))
addToFront (newValue);
}
Cpaann1e a1y qynkunk c npepmpyue aepcne. O6e 1exnnkn Hounme HexannsHm pnn
Hnorokpa1noro ncnonLsoaannn kopa, no a o1nnune o1 koHnosnunn nacnepoaanne
noppepxnaae1 nenanoe npepnonoxenne, u1o nopknaccm na caHoH pene nannk1cn
nop1nnaHn. D1o snaun1, u1o akseHnnnpm noao a6c1pakunn ponxnm aec1n ce6n 1ak xe, kak
n akseHnnnpm popn1enLckoro knacca.
Hacneqoaanne a qpyrnx nsmkax
B rnaae 7 Hm akpa1ue onncann cnn1akcnc, ncnonLsyeHm pnn nacnepoaannn a kaxpoH ns
paccHa1pnaaeHmx naHn nsmkoa nporpaHHnpoaannn. Kak n a cnyuae koHnosnunn, rnaanoe
rapan1npoaa1L, u1o a6c1pakunn napknacca nponnnunannsnpoaana ponxnmH o6pasoH.
9.2.3. 3akpm1oe nacneqoaanne a nsmke C++
C++ npepoc1aanne1 nn1epecnm koHnpoHncc Hexpy koHnosnune n nacnepoaanneH kak
HexannsHaHn Hnorokpa1noro ncnonLsoaannn kopa. D1o nponcxopn1 ny1eH ncnonLsoaannn
knkueaoro cnoaa private aHec1o knkueaoro cnoaa public a saronoake onpepenennn knacca. B
a1oH cnyuae nporpaHHnc1 cnrnannsnpye1, u1o nacnepoaanne cnepye1 ncnonLsoaa1L npn
konc1pynpoaannn noao a6c1pakunnpannmx, no 1akan a6c1pakunn ne ponxna
paccHa1pnaa1Lcn kak y1ounennan qopHa popn1enLckoro knacca:
Class Set : private List
{
public:
// xoncipyxiop
Set () : List () { }
// onepaiop-
void add (int);
int includes (int x);
converted to PDF by BoJIoc
{
return List::includes(x);
}
int size ()
{
return List::length();
}
};
HpnHennn 1epHnnm, ko1opme 6ypy1 onpepenenm 6onee c1poro a rnaae 10, Hoxno
ckasa1L, u1o sakpm1oe nacnepoaanne cospae1 nopknacc, ko1opm ne nanne1cn nop1nnoH.
TeH caHmH sakpm1oe nacnepoaanne ncnonLsye1 HexannsH nacnepoaannn, no a nanoH anpe
napyuae1 npnnunn nopc1anoakn. Onepaunn n o6nac1n pannmx, nacnepyeHme ns
popn1enLckoro knacca, sapec1ayk1cn a He1opax noao a6c1pakunn, no onn ne
npocHa1pnaak1cn nackaosL n nepoc1ynnm ee nonLsoaa1ennH. Ho a1o npnunne nk6o
He1op, ko1opm nporpaHHnc1 xoue1 akcnop1npoaa1L (1ako, kak includes a a6c1pakunn
Hnoxec1aa), ponxen 6m1L nepeonpepenen sanoao pnn noaoro knacca, paxe ecnn ace, u1o on
penae1, a1o amsoa He1opa knacca-npepka. (Kak 6mno yxe nponnnkc1pnpoaano, u1o6m
ns6exa1L naknapnmx pacxopoa npn amsoaax npouepyp a nopo6nmx npoc1mx cnyuanx, uac1o
ncnonLsyk1cn ac1panaaeHme He1opm.)
3akpm1oe nacnepoaanne nanne1cn nn1epecno npee n nan6onee nonesno, korpa (kak a
pannoH cnyuae) o6Lek1 a ocnoanoH coc1aanne1cn ns a6c1pakunn pannmx ppyroro 1nna n
pa6o1a npn cospannn noaoro o6Lek1a amnonnne1cn a ocnoanoH nnkancynnpoaanno
a6c1pakune, opnako noaoe nonn1ne ne ypoane1aopne1 o1nouennk u1u akseMnnnpoM,
neo6xopnHoHy pnn o1kpm1oro nacnepoaannn.
9.3. Hpo1naonoc1aanenne koHnosnnn n nacneqoaannn
Hponnnkc1pnpoaaa paa HexannsHa Hnorokpa1noro ncnonLsoaannn nporpaHHnoro
o6ecneuennn n yanpea, u1o onn o6a npnHennHm pnn peannsaunn Hnoxec1a, Hm HoxeH
npokoHHen1npoaa1L neko1opme nepoc1a1kn n npenHyuec1aa payx nopxopoa:
KoHnosnunn 6onee npoc1a. Ee npenHyuec1ao saknkuae1cn a 1oH, u1o ona ncno
nokasmaae1, kakne 1ouno onepaunn 6ypy1 amnonnn1Lcn nap konkpe1no c1pyk1ypo
pannmx. Hpn asrnnpe na onncanne a6c1pakunn pannmx Set c1anoan1cn oueanpno,
u1o pnn 1nna pannmx npepycHo1penm 1onLko onepaunn po6aanennn aneHen1a,
npoaepkn na nannune aneHen1a n onpepenenne uncna aneHen1oa a na6ope. D1o
cnpaaepnnao nesaancnHo o1 1oro, kakne onepaunn onpepenenm pnn cnnckoa.
Hpn nacnepoaannn onepaunn noao a6c1pakunn pannmx nannk1cn napHnoxec1aoH
onepaun ncxopno c1pyk1ypm pannmx. TaknH o6pasoH, u1o6m 1ouno sna1L, kakne
onepaunn paspeuenm pnn noao c1pyk1ypm, nporpaHHnc1 ponxen paccHo1pe1L
o6Lnanenne ncxopno c1pyk1ypm. HanpnHep, nsyuenne onncannn knacca Set ne
nokasmaae1 cpasy xe, u1o npoaepka na nannune aneHen1a (He1op includes)
paspeuena pnn Hnoxec1a. TonLko ns paccHo1pennn onncanno panee a6c1pakunn
pannmx List anpno, u1o nHee1cn eue uenm na6op ponyc1nHmx onepaun. Tpypnoc1L
coc1on1 a cnepykueH: u1o6m nonn1L knacc, ckonc1pynpoaannm c noHouLk
nacnepoaannn, nporpaHHnc1 ponxen noc1onnno nepeknkua1Lcn asap-anepep
Hexpy payHn (nnn 6onee) onncannnHn knaccoa. Ona nsaec1na kak npo6neHa aaepx-
anns (o-o) [Taenzer 1989].
C ppyro c1oponm, nakonnunoc1L a6c1pakunn pannmx, cospanno c noHouLk
nacnepoaannn, nanne1cn npenHyuec1aoH. HcnonLsyn nacnepoaanne, ne o6nsa1enLno
nnca1L aecL kop pnn poc1yna k qynkunnH 6asoaoro knacca. Ho a1o npnunne
peannsaunn c ncnonLsoaanneH nacnepoaannn (kak a1o 6mno a naueH cnyuae)
snaun1enLno HenLue no o6LeHy, ecnn cpaann1L nx c koHnosnune. Hacnepoaanne
1akxe uac1o o6ecneunaae1 6onLuyk qynkunonanLnoc1L. HanpnHep, npnHenenne
converted to PDF by BoJIoc
nacnepoaannn a naueH cnyuae penae1 poc1ynnmH pnn Hnoxec1a ne 1onLko npoaepky
include, no n qynkunk remove.
Hacnepoaanne ne sanpeuae1 nonLsoaa1ennH Hannnynnpoaa1L noamHn c1pyk1ypaHn
uepes amsoam He1opoa popn1enLckoro knacca, paxe ecnn a1n He1opm ne anonne
nopxopn1 nop npeonornk no1oHka. HanpnHep, korpa Hm ncnonLsoaann
nacnepoaanne pnn nonyuennn Hnoxec1a Set ns cnnckoa List, 1o nnu1o ne Heuano
nonLsoaa1ennH po6aann1L noame aneHen1m k Hnoxec1ay, amsmaan ynacnepoaannm
o1 knacca List He1op addToFront.
Hpn koHnosnunn 1o1 qak1, u1o knacc List ncnonLsye1cn pnn xpanennn naunx
Hnoxec1a, npoc1o pe1anL peannsaunn. C a1o 1exnnko 6mno 6m nerko sanoao
peannsoaa1L knacc, u1o6m nsaneuL nonLsy ns npnHenennn ppyrnx He1opoa
(nanpnHep, 1aknx, kak xau-1a6nnum) c HnnnHanLnmH aospec1aneH na
nonLsoaa1ene a6c1pakunn pannmx Set. Ecnn nonLsoaa1enL paccun1maae1 na 1o1
qak1, u1o a6c1pakunn Set a1o npoc1o y1ounennan qopHa a6c1pakunn List, 1o
1akne nsHenennn 6mno 6m 1pypno peannsoaa1L.
Hacnepoaanne nosaonne1 naH ncnonLsoaa1L noayk a6c1pakunk kak apryHen1
cyuec1aykue nonnHopqno qynkunn. Mm 6ypeH nccnepoaa1L a1y aosHoxnoc1L
6onee pe1anLno a rnaae 14. Tak kak koHnosnunn ne noppasyHeaae1 co6nkpenne
npnnunna nopc1anoakn, ona o6muno yc1panne1 nonnMopqnsM.
O c1enenn nonn1noc1n kopa cypn1L 1pypno. Hacnepoaanne nHee1 npenHyuec1ao a
kpa1koc1n kopa, no ne npo1okona. Hpn koHnosnunn kop knacca, xo1n on n
okasmaae1cn pnnnnee, a1o ace, u1o ponxen nonn1L ppyro nporpaHHnc1, u1o6m
ncnonLsoaa1L a6c1pakunk. enoaek, c1onknyauncn c neo6xopnHoc1Lk paso6pa1Lcn
c aepcne kopa c nacnepoaanneH, amnyxpen npoaepn1L, nanne1cn nn noaepenne,
nacnepyeHoe ns popn1enLckoro knacca, neo6xopnHmH pnn ponxnoro ncnonLsoaannn
noaoro knacca, n 1aknH o6pasoH on anannsnpye1 o6a knacca.
C1pyk1ypm pannmx, peannsoaannme c noHouLk nacnepoaannn, nHek1
(nesnaun1enLnoe) npeaocxopc1ao a cHmcne ckopoc1n amnonnennn nap c1pyk1ypaHn c
koHnosnune. Hpnunna coc1on1 a 1oH, u1o a nepaoH cnyuae ncknkuae1cn opnn
pononnn1enLnm amsoa qynkunn (xo1n 1exnnka inline a nsmke C++ Hoxe1
nnkanpnpoaa1L npak1nueckn ace naknapnme pacxopm no amsoay qynkun).
HHen paa pasnnunmx HexannsHa peannsaunn, HoxeH nn Hm ckasa1L, ko1opm ns nnx
nyuue a naueH konkpe1noH cnyuae? O6pa1nHcn k npnnunny nopc1anoakn. Cnpocn1e ce6n,
koppek1no nn a npnnoxennn, ko1opoe npepnonarae1 ncnonLsoaanne a6c1pakunn pannmx
List, nopc1aann1L aHec1o nee Hnoxec1ao Set? Xo1n unc1o 1exnnuecknH o1ae1oH Hoxe1 6m1L
pa (a6c1pakunn Set pec1an1enLno peannsye1 ace onepaunn List), sppaam cHmcn
roaopn1, ckopee, ne1. Hoa1oHy a annoM cnyuae koHnosnunn nopxopn1 nyuue.
Hocnepnn u1pnx: o6e 1exnnkn ouenL nonesnm, n o6Lek1no-opnen1npoaannm
nporpaHHnc1 ponxen 6m1L snakoH c o6enHn.
9.4. Hoa1opnoe ncnonusoaanne koqa: peanunoc1u?
Ha sape o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn y1aepxpanocL, u1o koHnosnunn
n nacnepoaanne o6ecneunnn aosHoxnoc1L cospannn nporpaHHnoro o6ecneuennn ns
asanHosaHenneHmx koHnonen1 o6uero nasnauennn. Ho aonpekn oueanpnoHy nporpeccy
(ceuac pa6o1ae1 orpoHnoe konnuec1ao koHHepuecknx noc1aaunkoa, npepnarakunx
o6Lek1no-opnen1npoaannme 6n6nno1ekn o6uero nasnauennn pnn pasnnunmx npnnoxenn
nonLsoaa1enLckne nn1epqecm, kon1enepm pannmx n 1. p.), o6un npouecc ne onpaapan
acex oxnpann. Ha 1o nHee1cn pnp npnunn:
Hacnepoaanne n koHnosnunn npepoc1aannk1 cpepc1aa pnn cospannn Hnorokpa1no
ncnonLsyeHmx koHnonen1, no caHn co6o onn ne pak1 ykasann, kak 1akan uenL
converted to PDF by BoJIoc
Hoxe1 6m1L poc1nrny1a. Okasmaae1cn, u1o cospanne xopounx n nonesnmx
nporpaHHnmx koHnonen1 nou1n acerpa 1pypnee paspa6o1kn konkpe1no 1pe6yeHo
nporpaHHm.
Tak kak cospanne Hnorokpa1no ncnonLsyeHmx koHnonen1 sa1pypneno, 1o
okynaeHoc1L ne poc1nrae1cn a npepenax opnoro npoek1a. Ha caHoH pene c1peHnenne
k HopynLnoc1n Hoxe1 paxe cnnsn1L ckopoc1L peannsaunn npoek1a. HeHepnennan
amropa ne poc1nrae1cn, 1ak u1o npnxopn1cn paccun1maa1L na norauenne sa1pa1 sa
cue1 nocnepykunx nporpaHHnmx npoek1oa. Ho 1ak kak nocnepnne sauac1yk nHek1
cao co6c1aennm 6kpxe1 n nnannpoaanne, 1o apHnnnc1pa1nanme HexannsHm
nopo6no aHop1nsaunn o1cy1c1ayk1.
Tak kak npnHenenne Hnorokpa1no ncnonLsyeHmx koHnonen1 nenocpepc1aenno ne
ynyuuae1 npoek1, nporpaHHnc1m ne nHek1 c1nHyna 6opo1Lcn sa Hnorokpa1noe
ncnonLsoaanne.
Tak kak kaxpan noaan sapaua o6muno 1pe6ye1 cneunqnuecko qynkunonanLnoc1n,
uac1o 1pypno c caHoro nauana cnpoek1npoaa1L pec1an1enLno nonesnme
nporpaHHnme koHnonen1m o6uero nasnauennn. Ckopee acero, nopxopnune
koHnonen1m 6ypy1 noc1enenno aaonkunonnpoaa1L ao Hnornx npoek1ax, noka
nakoneu ne poc1nrny1 c1a6nnLnoro coc1onnnn.
Mnorne nporpaHHnc1m n apHnnnc1pa1opm nopospn1enLno o1nocn1cn k nporpaHHaH,
ko1opme ne 6mnn paspa6o1anm a popnmx c1enax. Takan oc1opoxnoc1L nasmaae1cn
cnnppoHoH cpenano ne specL. Tak kak caHn apHnnnc1pa1opm roppn1cn caonHn
koHanpaHn, onn, ec1ec1aenno, aepn1, u1o nx nporpaHHnc1m Hory1 ace cpena1L
nyuue, ueH k1o-nn6o.
Tak kak Hnorne nporpaHHnc1m nepoc1a1ouno qopHanLno o6yuenm nnn ne pepxa1
pyky na nynLce nocnepnnx nporpaHHnmx noaoaaepenn (1aknx, kak OOH), onn Hory1
ne sna1L o HexannsHax pnn paspa6o1kn Hnorokpa1no ncnonLsyeHoro kopa.
Kopoue roaopn, pasan1ne nporpaHHnmx HexannsHoa pnn Hnorokpa1noro ncnonLsoaannn
caHo no ce6e ne rapan1npye1 1exnonornueckyk n ynpaanenueckyk kynu1ypy, ko1opan 6m
noppepxnaana n nooupnna noa1opnoe ncnonLsoaanne nporpaHHnmx koHnonen1.
enoaeueckne oprannsaunn nporpeccnpyk1 Hepnennee, ueH 1exnonornn, noa1oHy,
aosHoxno, npope1 eue Hnoro ne1 po 1oro, kak Hm yanpnH pec1an1enLnme amropm,
o6euannme o6Lek1no-opnen1npoaannmH nopxopoH. TeH ne Henee Hnorokpa1noe
ncnonLsoaanne o6Lek1oa npnHenne1cn, aosHoxno, ne aespe n paneko ne 1ak uac1o, kak
6mno sananeno, no ace xe npnHenne1cn, n npn npaannLnoH o6pauennn ono 1mcnuy pas
pokasano caok nonesnoc1L n cnoco6noc1L cokpaua1L sa1pa1m. Ho a1o npnunne
Hnorokpa1noe ncnonLsoaanne nens6exno c1ane1 nopHo paspa6o1kn nporpaHHnoro
o6ecneuennn.
Bo1 cnncok nepaano nspannmx knnr, nocanuennmx paspa6o1ke Hnorokpa1no
ncnonLsyeHmx koHnonen1 [Carroll 1995, McGregor 1992, Meyer 1994, Goldberg 1995].
Ynpaxnennn
Ec1L pasnnunme cnoco6m peannsaunn c1pyk1ypm pannmx c1ek nanpnHep, c
noHouLk cnnckoa nnn a anpe Haccnaa. HpepnonoxnH, Hm nHeeH n knacc cnnckoa
List, n knacc Haccnaoa Array. [nn kaxporo ns nnx nponnnkc1pnpy1e, kak Hoxno
noc1pon1L c1ek, ncnonLsyn n nacnepoaanne, n koHnosnunk. BaH paspeuae1cn
aaec1n kakne yropno He1opm pnn 6asoamx knaccoa. Kakan ns 1exnnk peannsaunn
kaxe1cn aaH 6onee nopxopnue a pannoH cnyuae?
Cnoaa npepnonoxnH, u1o Hm nHeeH ro1oayk c1pyk1ypy pannmx List n xo1nH
noc1pon1L a6c1pakunk, npepc1aannkuyk co6o ynopnpouennm cnncok, a ko1opoH
aneHen1m ac1aannk1cn a nyxno nocnepoaa1enLnoc1n, a ne npoc1o a nauano cnncka.
converted to PDF by BoJIoc
Hacnepoaanne nnn koHnosnunk am 6ype1e ncnonLsoaa1L a pannoH cnyuae?
O6ocny1e aau o1ae1.
Fnaaa 10

Hopknaccm u nop1unm
Opno ns nan6onee nn1epecnmx oco6ennoc1e o6Lek1no-opnen1npoaannmx nsmkoa
nporpaHHnpoaannn nanne1cn 1o1 qak1, u1o qak1nueckn 1nn nepeHenno Hoxe1 ne
coanapa1L c 1nnoH, sananennmH npn ee onncannn. Mm anpenn a1o a rnaae 6, a konue
nporpaHHm, Hopennpykue 6nnLnpp, rpe nepeHennan 1nna GraphicalObject a
pec1an1enLnoc1n npnnnHana snauennn 1nna Ball, Wall nnn Hole. D1o opnn ns acnek1oa
nonnHopqnsHa, ko1opm Hm 6ypeH pe1anLno o6cyxpa1L a rnaae 14. B 1papnunonnmx
nsmkax nporpaHHnpoaannn, 1aknx kak Pascal nnn C, ecnn nepeHennan onncana kak integer,
1o, u1o 6m nn cnyunnocL, copepxnHoe uac1n naHn1n, o1aepenno nop a1y nepeHennyk,
rapan1npoaanno 6ype1 nn1epnpe1npoaa1Lcn kak uenan aennunna. B o6Lek1no-
opnen1npoaannoH nsmke nepeHennan, onncannan kak Window, a pec1an1enLnoc1n Hoxe1
npnnnHa1L snauennn GraphicWindow, TextEditWindow nnn kakoro-nn6o ppyroro okonnoro
1nna.
HeckonLko 1epHnnoa noHory1 naH nonn1L, u1o xe noppasyHeaae1cn nop a1nHn nsHenennnHn.
BypeH ncnonLsoaa1L nonn1ne c1a1nueckn 1nn pnn o6osnauennn 1nna, npncaoennoro
nepeHenno npn ee onncannn. TepHnn pnnaHnueckn 1nn xapak1epnsye1 1nn qak1nueckoro
snauennn. To1 qak1, u1o pnnaHnueckn n c1a1nueckn 1nnm nepeHenno ne o6nsanm
coanapa1L, nanne1cn opnnH ns rnaaneunx poc1onnc1a o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn. HepeHennan, pnn ko1opo pnnaHnueckn 1nn ne coanapae1 (1ounee,
Hoxe1 ne coanapa1L) co c1a1nuecknH, nasmaae1cn nonnHopqno.
Eue paa 1epHnna nHek1 o1nouenne k o6cyxpaeHoHy aonpocy. Honn1ne nopknacc yxe 6mno
aaepeno a rnaae 7. Bnaropapn nopknaccaH noame koHnonen1m nporpaHH paspa6a1maak1cn
na ocnoae yxe cyuec1aykunx. Honn1ne nop1nna nanne1cn 6onee a6c1pak1nmH. Hop1nn
onpepenne1cn a 1epHnnax noaepennn, a ne c1pyk1ypm. Mm roaopnH, u1o 1nn B ec1L nop1nn
1nna A, ecnn Hm HoxeH a nk6o cn1yaunn nopc1aan1L akseHnnnp knacca B aHec1o
akseHnnnpa knacca A 6es kaknx-nn6o anpnHmx nsHenenn a noaepennn.
O1He1nH, u1o nonn1ne nop1nna coo1ae1c1aye1 naueHy npeannsnpoaannoHy npnnunny
nopc1anoakn, aaepennoHy a rnaae 9. Opnako, a6c1pak1no paccyxpan, ne cyuec1aye1
nnkaknx anpnHmx npnunn pnn 1oro, u1o6m nonn1nn nopknacca n nop1nna nHenn kakyk-1o
asanHocansL. [ec1an1enLno, a nsmkax nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn
pannmx apope Smalltalk onn n ne cansanm. [aa knacca Hory1 nHe1L o6uee noaepenne
nanpnHep, no o1nouennk k opnoHy n 1oHy xe na6opy coo6uenn, no 6es o6ue
peannsaunn nnn epnnoro npepka (sa ncknkuenneH knacca acex o6Lek1oa Object). Ecnn nx
peakunn na o6une coo6uennn a poc1a1ouno c1enenn ananornunm, 1o opnn knacc Hoxe1
6m1L c nerkoc1Lk nopc1aanen aHec1o ppyroro. Hpepc1aaL1e ce6e knacc paspexennmx
Haccnaoa, ko1opm noppepxnaae1 onepaunn knacca Haccnaoa Array. Torpa a nk6o
anropn1H, npepnasnauennm pnn pa6o1m c HaccnaaHn, Hoxno nopc1aan1L paspexennm
Haccna.
BonLunnc1ao o6Lek1no-opnen1npoaannmx nsmkoa nporpaHHnpoaannn co c1pornH kon1poneH
1nnoa pannmx (1akne, kak Object Pascal n C++) penak1 pasHm1mH pasnnune Hexpy
nop1nnaHn n nopknaccaHn a payx o1nouennnx. Bo-nepamx, onn paspeuak1 nepeHennmH
npnnnHa1L snauennn ppyroro 1nna, 1onLko korpa pnnaHnueckn 1nn snauennn nanne1cn
nopknaccoH c1a1nueckoro 1nna. Bo-a1opmx, onn npepnonarak1, u1o qak1nueckn ace
nopknaccm nannk1cn nop1nnaHn. D1o npepnonoxenne ne acerpa cnpaaepnnao. HockonLky
nopknaccm Hory1 nepeonpepenn1L He1opm popn1ene na nponsaonLnoe noaoe noaepenne, 1o
converted to PDF by BoJIoc
a o6ueH cnyuae ne cyuec1aye1 nnkaknx rapan1n 1oro, u1o nopknacc 6ype1 1akxe n
nop1nnoH.
10.1. Cansmaauue He1opoa u coo6euun
B cnepykueH paspene Hm o6cypnH, kak amrnnpn1 pasnnune Hexpy nopknaccaHn n
nop1nnaHn a o6cyxpaeHmx naHn nsmkax nporpaHHnpoaannn. Hpexpe acero, opnako, Hm
ponxnm sa1pony1L pae npo6neHm. Hepaan ns nnx cansana c aonpocoH o cnoco6e
cansmaannn. [onxnm nn Hm cansmaa1L coo6uenne n He1op, ocnoamaancL na c1a1nueckoH
1nne nepeHenno, nnn cnepye1 npnnn1L ao annHanne ee pnnaHnueckn 1nn? B1opo
npo6neHo nanne1cn aonpoc o6pauennn nonnHopqnsHa Hoxno nn npncaon1L snauenne
nepeHenno, ocnoamaancL na ee pnnaHnueckoH, a ne na c1a1nueckoH 1nne? Tpe1n, 1ecno
cansannm c panno 1eHo aonpoc a nHenno o 1ounoH snauennn nepeonpepenennn
He1opoa, poc1a1ouno cnoxen, noa1oHy Hm o1noxnH ero o6cyxpenne po cnepykue
rnaam.
10.1.1. Cansmaauue He1opoa
Cyuec1aoaanne nonnHopqno nepeHenno ec1ec1aennmH o6pasoH

Puc. 10.1. [ae 1oukn spennn na nonnHopqnyk nepeHennyk
noppasyHeaae1 nannune payx pasnnunmx npepc1aanenn o ne (pnc. 10.1). Mm HoxeH
paccHa1pnaa1L nepeHennyk c 1oukn spennn onncannn (c1a1nueckn) nnn c 1oukn spennn ee
1ekyuero snauennn (pnnaHnueckn). D1o pasnnune c1anoan1cn aaxnmH, korpa Hm ac1peuaeH
He1op, onpepenennm a popn1enLckoH knacce n nepeonpepenennm a pouepneH. Ha
pnc. 10.1, nanpnHep, nokasan He1op mouseDown, ko1opm nHee1cn kak y popn1enLckoro
knacca (Window), 1ak n y pouepnero (TextEditWindow). Korpa nonnHopqnan nepeHennan
nonyuae1 coo6uenne o uenuke HmuLk, c kaknH ns He1opoa ponxno cansmaa1Lcn a1o
coo6uenne?
Opnosnaunoro npaannLnoro o1ae1a na a1o1 aonpoc ne cyuec1aye1. aue acero Hm oxnpaeH,
u1o cansmaanne coo6uennn ponxno nponcxopn1L ncxopn ns pnnaHnueckoro 1nna pannmx.
Opnako cyuec1ayk1 cn1yaunn, rpe npo1naononoxnm nopxop 1akxe nonesen. Hnxe Hm
o6cypnH, kak pasnme nsmkn nporpaHHnpoaannn peuak1 a1y npo6neHy.
10.1.2. Hpo6neHa o6paeuun nonuHopqusHa
Hpnnunn nopc1anoakn rnacn1, u1o nepeHenno, onncanno kak akseHnnnp popn1enLckoro
knacca, Hm HoxeH npncaon1L snauenne pouepnero 1nna. MoxeH nn Hm panra1Lcn a ppyroH
nanpaanennn? To ec1L, npncaona nepeHenno Y 1nna Window snauenne nepeHenno X 1nna
TextEditWindow, cHoxeH nn Hm sa1eH npncaon1L noao nepeHenno Z 1nna TextEditWindow
nauy nepeHennyk Y?
converted to PDF by BoJIoc
D1o1 aonpoc a pec1an1enLnoc1n copepxn1 a ce6e pae 1ecno cansannme npo6neHm. 1o6m
nponnnkc1pnpoaa1L a1o, npepnonoxnH, u1o Hm onpepenneH knacc 6nnLnppnmx uapoa Ball n
paa ero nopknacca uepnme uapm BlackBall n 6enme uapm WhiteBall. [anee Hm cospaeH
nporpaHHnm akanaanen1 kopo6kn, a ko1opyk Hm HoxeH nonoxn1L payx npepc1aan1ene
knacca Ball, opnn ns ko1opmx

Puc. 10.2. BnnLnppnm uap, 1epnkun caok opnosnaunoc1L
(am6pannm cnyuano) sa1eH amnnHae1cn o6pa1no (pnc. 10.2). Mm onyckaeH BlackBall n
WhiteBall a kopo6ky n cHo1pnH, u1o nonyuae1cn na amxope.
Bm1auennm ns kopo6kn o6Lek1 onpepenenno Hoxe1 paccHa1pnaa1Lcn kak uap Ball n
noa1oHy Hoxe1 6m1L npncaoen nepeHenno, onncanno c a1nH 1nnoH. Ho 6ype1 nn on
uepnmH uapoH BlackBall? Mm HoxeH sapa1L specL paa aonpoca: 1) Hory nn n onpepenn1L,
u1o a1o1 o6Lek1 npnnapnexn1 k 1nny BlackBall, nnn ne1? 2) kakne HexannsHm neo6xopnHm
pnn 1oro, u1o6m npncaon1L a1o snauenne nepeHenno pouepnero knacca BlackBall?
Xo1n npnHep c uapaHn BlackBall n WhiteBall Hoxe1 nokasa1Lcn napyHannmH, ckpmaakuancn
sa nnH npo6neHa nanne1cn aecLHa o6ue. PaccHo1pnH npoek1npoaanne knaccoa pnn uac1o
ncnonLsyeHmx c1pyk1yp pannmx: Hnoxec1aa, c1ekn, ouepepn, cnnckn n 1. n., ko1opme
ncnonLsyk1cn, u1o6m xpann1L coaokynnoc1L o6Lek1oa. PeknaHnpyeHan amropa o6Lek1no-
opnen1npoaannoro nporpaHHnpoaannn coc1on1 a nponsaopc1ae Hnorokpa1no ncnonLsyeHmx
koHnonen1 nporpaHH, n kon1enepm pnn coaokynnoc1e pannmx nannk1cn nennoxnHn
kanpnpa1aHn na a1y ponL. Opnako kon1enepm npn onpepenennmx o6c1on1enLc1aax noxoxn
na Haunny c am6opoH uapoa. Ecnn nporpaHHnc1 noHeuae1 paa pasnnunmx o6Lek1a a na6op
n nosxe amnnHae1 o11ypa opnn, kak on ysnae1, u1o sa o6Lek1 on nonyunn?
D1n npo6neHm ne1pnananLnm, no paspeunHm. B paspenax a1o rnaam, nocanuennmx
konkpe1nmH nsmkaH nporpaHHnpoaannn, Hm onnueH HexannsHm o6pauennn nonnHopqnsHa
(ecnn onn nHek1cn). B cnepykue rnaae Hm paccHo1pnH npo6neHy kon1enepnmx knaccoa
noppo6nee.
10.2. Cansmaauue a nsmkax nporpaHHupoaauun
B cnepykunx paspenax Hm onnueH cansmaanne He1opoa a paccHa1pnaaeHmx nsmkax
nporpaHHnpoaannn. B uac1noc1n, 6ypy1 npo1naonoc1aann1Lcn Hexpy co6o cnoco6m
cansmaannn a nsmkax co c1a1nuecknHn 1nnaHn pannmx (Object Pascal, C++ n Java) n a
nsmkax c pnnaHnuecknHn 1nnaHn (Smalltalk n Objective-C).

converted to PDF by BoJIoc
10.2.1. Cansmaauue a nsmke Object Pascal
Hsmk ObjectPascal nanne1cn nsmkoH nporpaHHnpoaannn co c1a1nuecknHn 1nnaHn pannmx.
Kaxpm npen1nqnka1op a nporpaHHe ponxen 6m1L onncan. KpoHe 1oro, nonn1nn nopknacca
n nop1nna o6Lepnnenm. Henano npepnonarae1cn, u1o nopknaccm nannk1cn nop1nnaHn n u1o
npen1nqnka1op, onncannm kak o6Lek1, Hoxe1 nHe1L snauenne a1oro 1nna nnn nk6oro,
nonyuennoro ns nero ny1eH nacnepoaannn.
Xo1n nsmk 6asnpye1cn na c1a1nuecknx 1nnax pannmx, o6Lek1m 1eH ne Henee necy1 c co6o
snannn o6 nx co6c1aennoH pnnaHnueckoH 1nne. B Object Pascal aepcnn Apple Hoxe1
ncnonLsoaa1Lcn ac1poennan a cnc1eHy nornueckan qynkunn Member, ko1opan onpepenne1,
nanne1cn nn npen1nqnka1op akseHnnnpoH onpepenennoro knacca. Oynkunn Member
ncnonLsye1 a kauec1ae napaHe1poa ccmnky na o6Lek1 (o6muno a1o npoc1o npen1nqnka1op) n
nHn knacca (a 1epHnnonornn nsmka Object Pascal nHn 1nna pannmx). Ona aosapauae1
snauenne true, ecnn napaHe1p-ccmnka nanne1cn o6Lek1oH pannoro 1nna, n false a
npo1nanoH cnyuae. HanpnHep, sapan knacc xnao1nmx Animal. Mm xo1nH onpepenn1L,
nanne1cn nn nepeHennan fido, o1nocnuancn k 1nny Animal, akseHnnnpoH 6onee yskoro
nopknacca Hnekonn1akunx Mammal c noHouLk cnepykuero 1ec1a:
if Member (fido, Mammal) then
writeln ('fido is a mammal')
else
writeln ('fido is not a mammal');
O1He1nH, u1o qynkunn Member aosapauae1 snauenne true, paxe ecnn fido nanne1cn
npepc1aan1eneH eue 6onee yskoro knacca (1akoro, kak knacc co6ak Dog). Bkyne c
npnaepenneH 1nna pannmx qynkunn Member Hoxe1 6m1L ncnonLsoaana pnn uac1nunoro
peuennn npo6neHm o6pauennn nonnHopqnsHa. Korpa uap amny1 ns nunka, nporpaHHnc1
Hoxe1 ncnonLsoaa1L qynkunk Member pnn 1oro, u1o6m onpepenn1L, kako a1o uap
BlackBall nnn WhiteBall, n coo1ae1c1aenno npnaec1n ero k npaannLnoHy 1nny.
B nsmke nporpaHHnpoaannn Delphi Pascal knacc o6Lek1a Hoxe1 6m1L npo1ec1npoaan c
noHouLk onepa1opa is. On aosapauae1 snauenne true, korpa knacc neaoro apryHen1a nnn
coanapae1 c nHeneH knacca cnpaaa, nnn xe nanne1cn ero nopknaccoH. Onepa1op as
ocyuec1anne1 6esonacnoe npnaepenne pnnaHnueckoro 1nna pannmx. Ecnn neam apryHen1
ne nanne1cn akseHnnnpoH npaaoro knacca, aosnnkae1 ncknkun1enLnan cn1yaunn. B
npo1nanoH cnyuae on npnaopn1cn k 1nny npaaoro apryHen1a. D1n HexannsHm Hory1
ncnonLsoaa1Lcn npn pa6o1e c o6pauenneH nonnHopqnsHa.
if aBall is BlackBall then
bBall := aBall as BlackBall
else
writeln('cannot convert ball to black ball');
Ecnn Hm xo1nH ysna1L nc1nnnm knacc o6Lek1a (1o ec1L pec1an1enLnoe nHn knacca, a ne c
1ounoc1Lk po nacnepoaannn), Hm HoxeH ncnonLsoaa1L none ClassInfo o6Lek1a (kaxpm
o6Lek1 a nsmke Delphi Pascal nHee1 none ClassInfo).
if aBall.ClassInfo = BlackBall then
...
Xo1n nsmk Object Pascal nanne1cn nsmkoH nporpaHHnpoaannn co c1a1nuecknHn 1nnaHn
pannmx, on acerpa ncnonLsye1 pnnaHnueckoe cansmaanne pnn 1oro, u1o6m conoc1aan1L
nepecmnaeHoe coo6uenne n ncnonLsyeHm He1op. TaknH o6pasoH, ecnn He1op hasLiveYoung
(nHee1 xnaoe no1oHc1ao?) onpepenen a knacce Hnekonn1akunx Mammal n nepeonpepenen
pnn knacca y1konocoa Platypus, nonck He1opa 6ype1 nponsaopn1Lcn a nopknacce Platypus,
paxe ecnn ero nonyua1enL o6Lnanen kak knacc (1nn) Mammal.
Me1opm a nsmke Object Pascal cansmaak1cn pnnaHnueckn, no sakonnoc1L nepecmnkn
coo6uennn onpepenne1cn c1a1nuecknH knaccoH nonyua1enn. TonLko ecnn c1a1nueckn knacc
nonnHae1 nepecmnaeHoe coo6uenne, koHnnnn1op 6ype1 renepnpoaa1L kop pnn o6pa6o1kn
coo6uennn. 1o6m nponnnkc1pnpoaa1L a1o, paccHo1pnH npnHep. Hyc1L nepeHennan phyl
converted to PDF by BoJIoc
o6Lnanena kak akseHnnnp knacca Animal, no copepxn1 snauenne 1nna Mammal. Torpa
koHnnnn1op 6ype1 aospaxa1L npo1na coo6uennn hasLiveYoung, ko1opoe, no
npepnonoxennk, onpepeneno pnn Hnekonn1akunx Mammal, no ne onpepeneno pnn knacca
acex xnao1nmx Animal.
var
phyl : Animal;
newPlat : Platypus;
begin
new (newPlat);
phyl := newPlat;
(* xomnwnniop onapy+wi omwxy s oion xomanze *)
if phyl.hasLiveYoung then
...
10.2.2. Cansmaauue a nsmke Smalltalk
Smalltalk a1o nsmk c pnnaHnuecknHn 1nnaHn pannmx. HepeHennme-akseHnnnpm, kak Hm
anpenn a npepmpyunx rnaaax, onncmaak1cn 1onLko nHeneH, a ne 1nnoH pannmx. Kak n ao
acex nsmkax c pnnaHnuecknHn 1nnaHn pannmx, nepeonpepenenne n o6pauenne
nonnHopqnsHa ne nannk1cn npo6neHo, nockonLky nk6o nepeHenno Hoxe1 6m1L
npncaoeno nk6oe snauenne.
Moxno sanpocn1L knacc nk6oro o6Lek1a. Bce o6Lek1m pearnpyk1 na coo6uenne class,
aosapauan o6Lek1, npepc1aannkun knacc o6Lek1a. TaknH o6pasoH, ecnn fido
nepeHennan, pnn ko1opo oxnpae1cn, u1o ona copepxn1 snauenne 1nna Dog, 1o 1ec1
( fido class == Dog ) ifTrue: [ ... ]
nokaxe1 naH, onpaapanncL nn naun oxnpannn. Ho 1ec1 ne cpa6o1ae1, ecnn Hm
nopc1aanH knacc Mammal aHec1o knacca Dog. TaknH o6pasoH, a nsmke Smalltalk
(kak n a Objective-C) pnn o6Lek1oa Hory1 ncnonLsoaa1Lcn paa 1ec1a. Hepam, c
nHeneH isMemberOf:, 6epe1 nHn knacca a kauec1ae apryHen1a n akanaanen1en
1onLko u1o paccHo1pennoHy 1ec1y. B1opo, c nHeneH isKindOf:, nopo6en qynkunn
Member a nsmke Object Pascal on coo6uae1, nanne1cn nn nonyua1enL, nanpnHyk
nnn ny1eH nacnepoaannn, akseHnnnpoH knacca, nepepaaaeHoro a kauec1ae
apryHen1a. TaknH o6pasoH, ecnn nepeHennan fido copepxn1 snauenne 1nna Dog, 1o
nokasannm nnxe 1ec1 npope1, a 1ec1 c ncnonLsoaanneH He1opa isMemberOf:
ne1:
( fido isKindOf: Mammal ) ifTrue: [ ... ]
HcnonLsoaanne He1opa isKindOf cun1ae1cn nnoxo npak1nko nporpaHHnpoaannn,
1ak kak on xec1ko cansmaae1 kop co snauenneH onpepenennoro 1nna. O6muno knacc
o6Lek1a nn1epecye1 nac HenLue, ueH aonpoc o 1oH, 6ype1 nn on nonnHa1L
onpepenennoe coo6uenne. [pyrnHn cnoaaHn, Hm 6onLue nn1epecyeHcn nop1nnaHn,
ueH nopknaccaHn. TaknH o6pasoH, n knacc 1knene Seal, n knacc co6ak Dog Hory1
peannsoamaa1L He1op bark (nan1L). Bsna onpepenennm o6Lek1, ckaxeH fido, Hm
HoxeH ncnonLsoaa1L pnn onpepenennn 1oro, o1ae1n1 nn o6Lek1 na coo6uenne bark,
cnepykun He1op:
( fido respondsTo: #bark ) ifTrue: [ ... ]
O1He1nH, u1o cenek1op He1opa npepc1aanen kak cnHaon (naunnae1cn c #).
B nsmke Smalltalk nonn1nn nopknacca n nop1nna ue1ko paspenenm. Tak kak
c1a1nueckne 1nnm nepoc1ynnm, pnn nop6opa He1opoa k coo6uennnH acerpa
ncnonLsye1cn pnnaHnueckoe cansmaanne. Ecnn nonyua1enL ne nonnHae1
konkpe1noro coo6uennn, ampae1cn pnarnoc1nka oun6kn na a1ane amnonnennn.
C1anpap1nan 6n6nno1eka nsmka Smalltalk (nasmaaeHan standard image)
o6ecneunaae1 6ora1m na6op kon1enepnmx knaccoa, ko1opme unpoko ncnonLsyk1
converted to PDF by BoJIoc
caoc1ao nospnero cansmaannn pnn a1oro nsmka nporpaHHnpoaannn. 10.2.3.
Cansmaanne a nsmke Objective-C
Objective-C nanne1cn a caoe ocnoae nsmkoH c pnnaHnuecknHn 1nnaHn pannmx. D1o
neypnan1enLno, yun1maan Hnenne ero cospa1enn Bpepa Kokca, npnaopnHoe a konue
a1o rnaam. BonLue uac1Lk ace, ckasannoe pnn nsmka Smalltalk o cansmaannn n
noncke He1opoa, nopxopnunx nop nepecmnaeHoe coo6uenne, cnpaaepnnao 1akxe n
pnn Objective-C. D1o aknkuae1 n 1o1 qak1, u1o ace o6Lek1m nonnHak1 coo6uennn
class, isKindOf: n isMemberOf:. Korpa nonyua1enL nanne1cn o6Lek1oH c
pnnaHnuecknH 1nnoH pannmx, acerpa ncnonLsye1cn pnnaHnueckoe cansmaanne.
[nn 1oro u1o6m onpepenn1L, nonnHae1 nn pannm o6Lek1 konkpe1noe coo6uenne,
Hm HoxeH ncnonLsoaa1L cnc1eHnyk npouepypy @selector pnn npeo6pasoaannn
1ekc1oaoro qopHa1a cenek1opa coo6uennn ao any1pennee npepc1aanenne, kak a1o
nokasano a cnepykueH npnHepe:
if ( [ fido respondsTo: @selector(bark) ] ) { ... }
Hn1epecno oco6ennoc1Lk nsmka Objective-C nanne1cn aosHoxnoc1L ncnonLsoaa1L
nepeHennme c1a1nueckoro 1nna a koH6nnaunn c aennunnaHn, o6napakunHn
pnnaHnuecknH 1nnoH pannmx. B npo1naononoxnoc1L 1oHy u1o 6mno ckasano a
rnaae 3, o6Lek1m Hory1 6m1L onncanm c ykasanneH c1a1nueckoro 1nna, npnueH
payHn cnoco6aHn. Hpn nannunn onpepenennoro knacca (nanpnHep, Mammal)
o6Lnanenne nepeHenno
Mammal anAnimal;
cospae1 noam npen1nqnka1op c nHeneH anAnimal n o1aopn1 pnn nero Hec1o a
naHn1n. Kak n a nsmke C++, nocnepykune nepecmnkn coo6uenn 6ypy1
ocnoamaa1Lcn na c1a1nueckoH 1nne pannmx (Mammal), n 1akne snauennn ne
noppepxnaak1 nonnHopqnoe npncaanaanne. To ec1L ecnn am nonm1ae1ecL
npncaon1L 1ako nepeHenno a kauec1ae snauenne akseHnnnp nopknacca
(nanpnHep, Dog), 1o ono no-npexneHy 6ype1 paccHa1pnaa1Lcn kak Mammal.
[pyro ny1L a1o onncanne, ko1opoe sapae1cn a 1epHnnax nanmx ykasa1ene. Ono
ne o1aopn1 npoc1panc1ao pnn o6Lek1a, n nonnHopqnoe npncaanaanne a a1oH cnyuae
aosHoxno. HaHn1L ampenne1cn pnn 1aknx snauenn c noHouLk coo6uennn new, kak
a1o penae1cn pnn o6munmx pnnaHnuecknx nepeHennmx. Opnako korpa a kauec1ae
nonyua1enn ncnonLsyk1cn c1a1nueckne nHena knaccoa, 1o cansmaanne He1opoa, xo1n
ono no-npexneHy nanne1cn pnnaHnuecknH, amnonnne1cn uy1L 6mc1pee.
Mammal *fido;
fido = [ Dog new ];
[nn nsmka Objective-C o1nocn1enLno nerko nnca1L kon1enepnme knaccm 6naropapn
nospneHy cansmaannk. Ha caHoH pene 6onLuo na6op 1aknx c1pyk1yp pannmx
nHee1cn a c1anpap1no 6n6nno1eke Objective-C.
10.2.4. Cansmaauue a nsmke C++
[ayHn ocnoanmHn uennHn npn paspa6o1ke nsmka nporpaHHnpoaannn C++ 6mnn
aqqek1nanoe ncnonLsoaanne naHn1n n ckopoc1L amnonnennn [Ellis 1990,
Stroustrup 1994]. On 6mn sapyHan kak ycoaepuenc1aoaanne nsmka C, a uac1noc1n,
pnn o6Lek1no-opnen1npoaannmx npnnoxenn. Ocnoano npnnunn C++: nnkakoe
caoc1ao nsmka ne ponxno npnaopn1L k aosnnknoaennk pononnn1enLnmx nspepxek
(kak no naHn1n, 1ak n no ckopoc1n), ecnn pannoe caoc1ao nporpaHHnc1oH ne
ncnonLsye1cn. HanpnHep, ecnn acn o6Lek1nan opnen1npoaannoc1L C++
nrnopnpye1cn, 1o oc1aauancn uac1L ponxna pa6o1a1L 1ak xe 6mc1po, kak n
1papnunonnm C. Hoa1oHy neypnan1enLno u1o 6onLunnc1ao He1opoa a C++
cansmaak1cn c1a1nueckn (ao apeHn koHnnnnunn), a ne pnnaHnueckn (ao apeHn
amnonnennn).
converted to PDF by BoJIoc
Kak 6mno o1Heueno a rnaae 7, nsmk C++ ne noppepxnaae1 npnnunn nopc1anoakn sa
ncknkuenneH ncnonLsoaannn ykasa1ene n snauenn-ccmnok. Mm yanpnH npnunny
a1oro a rnaae 12, rpe 6ypeH nccnepoaa1L ynpaanenne naHn1Lk a C++.
Cansmaanne He1opoa a a1oH nsmke nanne1cn poaonLno cnoxnmH. [nn o6munmx
nepeHennmx (ne ykasa1ene nnn ccmnok) ono ocyuec1anne1cn c1a1nueckn. Ho korpa
o6Lek1m o6osnauak1cn c noHouLk ykasa1ene nnn ccmnok, ncnonLsye1cn
pnnaHnueckoe cansmaanne. B nocnepneH cnyuae peuenne o am6ope He1opa
c1a1nueckoro nnn pnnaHnueckoro 1nna pnk1ye1cn 1eH, onncan nn coo1ae1c1aykun
He1op c noHouLk knkueaoro cnoaa virtual. Ecnn on o6Lnanen nHenno 1ak, 1o He1op
noncka coo6uennn 6asnpye1cn na pnnaHnueckoH knacce, ecnn ne1 na
c1a1nueckoH. [axe a 1ex cnyuanx, korpa ncnonLsye1cn pnnaHnueckoe cansmaanne,
sakonnoc1L nk6oro sanpoca onpepenne1cn koHnnnn1opoH na ocnoae c1a1nueckoro
knacca nonyua1enn.
PaccHo1pnH, nanpnHep, cnepykuee onncanne knaccoa n rno6anLnmx nepeHennmx:
class Mammal
{
public:
void speak()
{
printf("cant speak");
}
};
class Dog : public Mammal
{
public:
void speak()
{
printf("wouf wouf");
}
void bark()
{
printf("wouf wouf, as well");
}
};
Mammal fred;
Dog lassie;
Mammal * fido = new Dog;
Bmpaxenne fred.speak() neua1ae1 cant speak. lassie.speak() ampac1 naH co6aun
na. Opnako amsoa fido->speak() 1akxe naneua1ae1 cant speak, nockonLky
coo1ae1c1aykun He1op a knacce Mammal ne o6Lnanen kak anp1yanLnm.
Bmpaxenne fido->bark() ne ponyckae1cn koHnnnn1opoH, paxe ecnn Hm snaeH, u1o
pnnaHnueckn 1nn pnn fido knacc Dog. TeH ne Henee c1a1nueckn 1nn
nepeHenno acero nnuL knacc Mammal, a Hnekonn1akune o6muno ne nak1.
Ecnn Hm po6aanH cnoao virtual:
class Mammal
{
public:
virtual void speak()
{
printf("cant speak");
}
};
converted to PDF by BoJIoc
1o nonyunH na amxope pnn ampaxennn fido->speak() oxnpaeHm pesynL1a1
(a nHenno, fido 6ype1 nan1L).
O1nocn1enLno nepaanee nsHenenne a nsmke C++ po6aanenne cpepc1a pnn
pacnosnaaannn pnnaHnueckoro knacca o6Lek1a. Onn o6pasyk1 cnc1eHy RTTI (Run-
Time Type Identification npen1nqnkaunn 1nna ao apeHn amnonnennn).
B cnc1eHe RTTI kaxpm knacc nHee1 cansannyk c nnH c1pyk1ypy 1nna typeinfo,
ko1opan kopnpye1 pasnnunyk nnqopHaunk o knacce. Hone pannmx name opno ns
none pannmx a1o c1pyk1ypm copepxn1 nHn knacca a anpe 1ekc1oao c1pokn.
Oynkunn typeid Hoxe1 ncnonLsoaa1Lcn pnn anannsa nnqopHaunn o 1nne pannmx.
Cnepoaa1enLno, cnepykuan nnxe koHanpa 6ype1 neua1a1L c1poky Dog
pnnaHnueckn 1nn pannmx pnn fido. B a1oH npnHepe neo6xopnHo pasmHenoamaa1L
nepeHennyk-ykasa1enL fido, u1o6m apryHen1 6mn snauenneH, na ko1opoe ccmnae1cn
ykasa1enL, a ne caHnH ykasa1eneH:
cout << "fido is a " << typeid(*fido).name() << endl;
Moxno 1akxe cnpocn1L, ncnonLsyn qynkunk-unen before, coo1ae1c1aye1 nn opna
c1pyk1ypa c nnqopHaune o 1nne pannmx nopknaccy knacca, coo1nocnuerocn c
ppyro c1pyk1ypo. HanpnHep, cnepykune paa onepa1opa ampak1 true n false:
if (typeid(*fido).before(typeid(fred))) ...
if (typeid(fred).before(typeid(lassie))) ...
[o nonanennn cnc1eHm RTTI c1anpap1nm nporpaHHnc1ckn 1pkk coc1onn a 1oH,
u1o6m nanmH o6pasoH sakopnpoaa1L a nepapxnn knacca He1opm 6m1L
akseHnnnpoH. HanpnHep, pnn npoaepkn snauennn nepeHennmx 1nna Animal na
npnnapnexnoc1L k 1nny Cat nnn k 1nny Dog Hoxno 6mno 6m onpepenn1L cnepykuyk
cnc1eHy He1opoa:
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;
converted to PDF by BoJIoc
TenepL pnn onpepenennn 1oro, nanne1cn nn 1ekyunH snauenneH nepeHenno fido
aennunna 1nna Dog, Hoxno ncnonLsoaa1L koHanpy fido->isaDog(). Ecnn
aosapauae1cn nenyneaoe snauenne, 1o Hoxno npnaec1n 1nn nepeHenno k nyxnoHy
1nny pannmx.
Bosapauan ykasa1enL, a ne uenoe uncno, Hm o6LepnnneH npoaepky na
npnnapnexnoc1L k nopknaccy n npnaepenne 1nna. D1o ananornuno ppyro uac1n
cnc1eHm RTTI, nasmaaeHo dynamic_cast, ko1opyk Hm akpa1ue onnueH. Ecnn nekan
qynkunn a knacce Mammal aosapauae1 ykasa1enL na Dog, 1o knacc Dog ponxen
6m1L npepaapn1enLno onncan. PesynL1a1oH npncaanaannn nanne1cn nn6o nyneao
ykasa1enL, nn6o npaannLnan ccmnka na knacc Dog. H1ak, npoaepka pesynL1a1a ace
eue ponxna ocyuec1ann1Lcn, no Hm ncknkuaeH neo6xopnHoc1L npnaepennn 1nna.
D1o nokasano a cnepykueH npnHepe:
class Dog; // npezsapwiensnoe onwcanwe
class Cat;
class Mammal
{
public:
virtual Dog* isaDog()
{
return 0;
}
virtual Cat* isaCat()
{
return 0;
}
};
class Dog : public Mammal
{
public:
virtual Dog* isaDog()
{
return this;
}
};
class Cat : public Mammal
{
public:
virtual Cat* isaCat()
{
return this;
}
};
Mammal *fido;
Dog *lassie;
Onepa1op lassie = fido->isaDog(); 1enepL amnonnnH acerpa. B pesynL1a1e
nepeHennan lassie nonyuae1 nenyneaoe snauenne, 1onLko ecnn fido nHee1
pnnaHnueckn knacc Dog. Ecnn fido ne npnnapnexn1 Dog, 1o nepeHenno lassie
6ype1 npncaoen nyneao ykasa1enL.
lassie = fido->isaDog();
if (lassie)
{
... // fido w s camom zene oinocwicn x iwny Dog
};
else
{
converted to PDF by BoJIoc
... // npwcsawsanwe ne cpaoiano
... // fido ne npwnazne+wi x iwny Dog
};
Xo1n nporpaHHnc1 n Hoxe1 ncnonLsoaa1L a1o1 He1op pnn o6pauennn nonnHopqnsHa,
nepoc1a1ok 1akoro cnoco6a coc1on1 a 1oH, u1o 1pe6ye1cn po6aanenne He1opoa kak a
popn1enLckn, 1ak n a pouepnn knaccm. Ecnn ns opnoro o6uero popn1enLckoro
knacca nponc1ekae1 Hnoro pouepnnx, He1op c1anoan1cn rpoHospknH. Ecnn
nsHenennn a popn1enLckoH knacce ne ponyckak1cn, 1akan 1exnnka aoo6ue
neaosHoxna.
HockonLky nopo6nme npo6neHm ac1peuak1cn uac1o, 6mno napeno nx o6uee
peuenne. Oynkunn ua6nona dynamic_cast 6epe1 1nn a kauec1ae apryHen1a ua6nona
n, a 1ounoc1n kak qynkunn, onpepenennan amue, aosapauae1 nn6o snauenne
apryHen1a (ecnn npnaepenne 1nna sakonno), nn6o nyneaoe snauenne (ecnn
npnaepenne 1nna nepaspeueno). Hpncaanaanne, akanaanen1noe cpenannoHy a
npepmpyueH npnHepe, Hoxe1 6m1L sanncano 1ak:
// xonsepiwposais ionsxo s iom cnyuae, ecnw fido nsnneicn coaxon
lassie = dynamic_cast (fido);
// saiem nposepwis, s-nonneno nw npwsezenwe
if (lassie) . . .
B nsmk C++ 6mnn po6aanenm eue 1pn 1nna npnaepennn (static_cast, const_cast n
reinterpret_cast), no onn ncnonLsyk1cn a oco6mx cnyuanx n noa1oHy specL ne
onncmaak1cn. HporpaHHnc1aH pekoHenpye1cn npnHenn1L nx kak 6onee 6esonacnme
cpepc1aa aHec1o npexnero HexannsHa npnaepennn 1nnoa.
10.2.5. Cansmaauue a nsmke Java
Xo1n aneune nsmk Java noxox na C++, ero cnoco6m cansmaannn coo6uenn n
noncka nopxopnunx He1opoa cyuec1aenno npoue, ueH a C++. B Java ace
nepeHennme snak1 cao pnnaHnueckn 1nn pannmx. Hpepnonarae1cn, u1o
nopknaccm nannk1cn nop1nnaHn, n noa1oHy snauenne Hoxe1 6m1L npncaoeno 1nny
popn1enLckoro knacca 6es nanoro npeo6pasoaannn. O6pa1noe npncaanaanne
(o6pauenne nonnHopqnsHa) ponyckae1cn c nanmH npnaepenneH 1nna. [nn 1oro
u1o6m onpepenn1L ponyc1nHoc1L npncaanaannn, ao apeHn amnonnennn nporpaHHm
nponsaopn1cn npoaepka n, ecnn npncaanaanne neponyc1nHo, nnpyunpye1cn
ncknkun1enLnan cn1yaunn.
Ball aBallValue;
// ... nponymenn-n xoz
// s-nonnwis npwcsawsanwe uepes opamenwe nonwmopwsma
BlackBall bball = (BlackBall) aBallValue;
Takxe Hoxno npoaepn1L pnnaHnueckn 1nn snauennn, ncnonLsyn onepa1op
instanceOf.
if (aBallValue instanceOf BlackBall)
...
else
...
Coo6uennn acerpa cansmaak1cn c He1opaHn na ocnoae pnnaHnueckoro 1nna pannmx
nonyua1enn. B o1nnune o1 C++ knkueame cnoaa virtual o1cy1c1ayk1. Hn1epecnan
oco6ennoc1L nsmka Java coc1on1 a 1oH, u1o nonn pannmx 1akxe Hory1
nepeonpepenn1Lcn, no a a1oH cnyuae poc1yn k nepeHenno ocnoamaae1cn na
c1a1nueckoH 1nne pannmx, a ne na pnnaHnueckoH. O6a cnyuan nepeonpepenennn
nnnkc1pnpyk1cn cnepykunH npnHepoH:
class A
{
converted to PDF by BoJIoc
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); // neuaiaei "xnacc A"
println(b); // neuaiaei "xnacc B"
a.print(); // neuaiaei "xnacc B", a ne "xnacc A"
b.print(); // neuaiaei "xnacc B"
}
}
Xo1n nsmk Java cHeunaae1 nonn1nn nopknacca n nop1nna, npepnonaran, u1o ace
nopknaccm nannk1cn nop1nnaHn, no ns acex paccHa1pnaaeHmx naHn c1poro
1nnnsnpoaannmx nsmkoa Java nan6onee ue1ko paspenne1 a1n konuenunn, npepnaran
nonn1ne nn1epqeca.
Hn1epqecm o6ecneunaak1 nepapxnueckyk oprannsaunk, nopo6nyk knaccaH, no ne
saancn1 o1 nocnepnnx. Kak o1HeuanocL a rnaae 7, nn1epqecm onpepennk1 1onLko
npo1okon amnonnennn onepaun, no ne nx peannsaunk. HcnonLsyn knkueaoe cnoao
extends, Hoxno c1pon1L noame nn1epqecm noaepx cyuec1aykunx, kak a1o
penae1cn pnn knaccoa. TaknH o6pasoH cospae1cn nepapxnn nop1nnoa, nonnoc1Lk
nesaancnHan o1 nepapxnn nopknaccoa.
Hn1epqecm Hory1 ncnonLsoaa1Lcn pnn onpepenennn nepeHennmx, ko1opme 6ypy1
npnnnHa1L snauennn nk6mx 1nnoa, sanannkunx, u1o onn peannsyk1 nn1epqec.
TeH caHmH c1a1nueckn 1nn a1o 1nn nn1epqeca, 1orpa kak pnnaHnueckn 1nn
a1o 1nn knacca. Cansmaanne ocnoamaae1cn na pnnaHnueckoH 1nne.
HanpnHep, npn npoek1npoaannn na nsmke Java cnc1eHm n1epa1opa pnn c1pyk1yp
pannmx (cH. rnaay 16, rpe o6cyxpak1cn n1epa1opm) Hoxno npepc1aan1L ce6e
nepapxnk n1epa1opoa: opnonanpaanennme, paynanpaanennme, c nponsaonLnmH
poc1ynoH. Onn onpepenenm cnepykunHn npo1okonaHn:
interface forwardIterator
{
void advance();
int currentValue();
}
interface bidirectionalIterator extends forwardIterator
{
void moveBack();
}
interface randomAccessIterator extends
converted to PDF by BoJIoc
bidirectionalIterator
{
int at(int);
}
C1pyk1ypm pannmx Hory1 sa1eH onpepenn1L nyxnm n1epa1op. D1o sapae1cn
peannsaune.
class listIterator implements forwardIterator
{
// ...
}
10.3. Kak cansmaa1u: c1a1uvecku unu
puuaHuvecku?
Cnopm no noaopy o1nocn1enLnmx poc1onnc1a c1a1nuecknx n pnnaHnuecknx 1nnoa
pannmx, a 1akxe c1a1nueckoro n pnnaHnueckoro cansmaannn nensHenno caopn1cn k
npo1naonoc1aanennk aqqek1nanoc1n n rn6koc1n. OopHynnpyn akpa1ue, c1a1nueckne
1nnm pannmx n c1a1nueckoe cansmaanne 6onee aqqek1nanm, a pnnaHnueckne 1nnm
pannmx n pnnaHnueckoe cansmaanne 6onee rn6kn.
Kak Hm anpenn, pnnaHnueckne 1nnm pannmx noppasyHeaak1, u1o kaxpm o6Lek1
ponxen o1cnexnaa1L cao co6c1aennm 1nn pannmx. Ecnn Hm nocHo1pnH na
o6Lek1m c 1oukn spennn nenpeknonnoro nnpnanpyannc1a (kaxpm o6Lek1 ponxen
caH sa6o1n1Lcn o ce6e n ne saance1L o1 ppyrnx), 1o pnnaHnueckne 1nnm pannmx
kaxy1cn 6onee o6Lek1no-opnen1npoaanno He1opnko. Hx npnHenenne cnnLno
ynpouae1, nanpnHep, paspa6o1ky c1pyk1yp pannmx o6uero nasnauennn. Ho ne ace
1ak npoc1o ao apeHn amnonnennn nponcxopn1 noc1onnnm nonck nopxopnuero
kopa. Xo1n nHek1cn cnoco6m yHenLuennn a1nx sa1pa1, onn ne Hory1 6m1L
yc1panenm nonnoc1Lk. B ocnoanoH nHenno no1oHy, u1o 1akne naknapnme pacxopm
Hory1 6m1L aecLHa snaun1enLnmHn, 6onLunnc1ao nsmkoa nporpaHHnpoaannn
ncnonLsye1 c1a1nueckne 1nnm pannmx.
C1a1nueckne 1nnm pannmx ynpouak1 peannsaunk nsmka, paxe ecnn (kak a Java,
Object Pascal n nnorpa a C++) ncnonLsye1cn pnnaHnueckoe cansmaanne He1opa c
coo6uenneH. Korpa koHnnnn1opy nsaec1nm c1a1nueckne 1nnm pannmx, ampenenne
naHn1n nop nepeHennme nponcxopn1 6onee aqqek1nano, a pnn npoc1mx onepaun
(nanpnHep, cnoxenne uncen) renepnpye1cn nou1n coaepuennm kop. Ho nHek1cn
sa1pa1m, cansannme n co c1a1nuecknHn 1nnaHn pannmx. K npnHepy, uac1o o6Lek1m
Hory1 1epn1L snanne o ce6e (xo1n a1o ne o6nsa1enLno, kak Hm anpenn a cnyuae
nsmka Object Pascal). Kak Hm o1Heuann npn o6cyxpennn npo6neHm kon1enepoa
pannmx, a1o1 qak1 sa1pypnne1 nanncanne a6c1pakun c1pyk1yp pannmx o6uero
nasnauennn.
Ecnn c1a1nueckne 1nnm pannmx ynpouak1 peannsaunk nsmka nporpaHHnpoaannn,
1o c1a1nueckoe cansmaanne penae1 ero nou1n aneHen1apnmH. Ecnn coo1ae1c1ane
Hexpy He1opoH n coo6uenneH Hoxe1 6m1L o6napyxeno koHnnnn1opoH, 1o
nepecmnka coo6uenn (nesaancnHo o1 ncnonLsyeHoro cnn1akcnca) npepc1aanne1cn
npoc1o kak amsoa npouepypm: ao apeHn amnonnennn yxe ne 1pe6ye1cn nonck
nopxopnuero He1opa. D1o, no-anpnHoHy, HakcnHanLno aqqek1nano (xo1n HexannsH
inline-qynkun a C++ Hoxe1 yc1pann1L paxe n kop amsoaa npouepypm).
Hanpo1na, pnnaHnueckoe cansmaanne acerpa 1pe6ye1 nekoero run-time-HexannsHa
(xo1n 6m n npnHn1nanoro) pnn conoc1aanennn He1opa n coo6uennn. B nsmkax
nporpaHHnpoaannn, ko1opme ncnonLsyk1 pnnaHnueckne 1nnm pannmx (n, kak
cnepc1ane, pnnaHnueckoe cansmaanne), aoo6ue roaopn, nenLsn onpepenn1L
sapanee, 6ype1 nn nepecmnaeHoe coo6uenne aocnpnnnHa1Lcn nonyua1eneH. Korpa
coo6uenne ne pacnosnae1cn, 1o ne oc1ae1cn ppyro anL1epna1nam, kpoHe kak
crenepnpoaa1L coo6uenne o6 oun6ke a1ana amnonnennn. 3aun1nnkn c1a1nuecknx
converted to PDF by BoJIoc
nsmkoa nporpaHHnpoaannn nac1anaak1, u1o 6onLunnc1ao 1aknx oun6ok Hoxe1 6m1L
o1noaneno na a1ane koHnnnnunn pnn nsmka co c1a1nuecknH cansmaanneH He1opoa.
D1a 1ouka spennn ocnapnaae1cn (kak n cnepoaano oxnpa1L) c1oponnnkaHn
pnnaHnueckoro nporpaHHnpoaannn.
TeH caHmH naH npnxopn1cn peua1L, u1o 6onee aaxno: aqqek1nanoc1L nnn rn6koc1L,
npaannLnoc1L nnn nerkoc1L ncnonLsoaannn. Bpap Kokc [Cox 1986] nac1anaae1, u1o
peuenne saancn1 kak o1 ypoann a6c1pakunn nporpaHHm, 1ak n o1 1oro, nanneHcn nn
Hm nponsaopn1ennHn nnn no1pe6n1ennHn nporpaHHno cnc1eHm. Kokc y1aepxpae1,
u1o o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne 6ype1 ocnoanmH (xo1n n ne
epnnc1aennmH) cpepc1aoH a peaonkunn a o6nac1n nporpaHHno nnpyc1pnn.
Touno 1ak xe kak a pean1napua1oH aeke nnpyc1pnanLnan peaonkunn c1ana
aosHoxna 1onLko nocne paspa6o1kn asanHosaHenneHmx pe1ane, 1ak n uenLk
nporpaHHno peaonkunn nanne1cn konc1pynpoaanne Hnorokpa1no ncnonLsyeHmx n
napexnmx a6c1pak1nmx koHnonen1 amcokoro ypoann pnn nporpaHHnoro
o6ecneuennn, ns ko1opmx co6npak1cn peanLnme npnnoxennn.
Dqqek1nanoc1L rnaanoe, o ueH cnepye1 pyHa1L na nnskoH ypoane (Kokc nasmaae1
a1o1 ypoaenL a6c1pakune ypoann 1pansnc1opoa (gate-level abstractions) no
ananornn c anek1ponnko). Ho Hepe noamuennn ypoann a6c1pakunn (po
nn1erpanLnmx HnkpocxeH n ro1oamx nna1) rn6koc1L c1anoan1cn 6onee
cyuec1aenno.
Dqqek1nanoc1L nporpaHHm nanne1cn nepaoc1enenno sa6o1o paspa6o1unka. [nn
no1pe6n1enn, sann1epecoaannoro a koH6nnnpoaannn nporpaHHnmx cnc1eH, rn6koc1L
Hoxe1 6m1L 6onee aaxno.
H1ak, ne1 epnnc1aenno npaannLnoro o1ae1a na aonpoc, kakan ns 1exnnk cansmaannn
nanne1cn 6onee nopxopnue. Bce saancn1 o1 konkpe1no cn1yaunn.
Ynpaxueuun
Hsmk Object Pascal ncnonLsye1 n c1a1nueckne 1nnm pannmx, n pnnaHnueckoe
cansmaanne. O6Lncnn1e, noueHy o6pa1nan cn1yaunn (pnnaHnueckne 1nnm
pannmx n c1a1nueckoe cansmaanne coo6uenn c He1opaHn) neaosHoxna.
Hponnnkc1pnpy1e, noueHy a o6Lek1no-opnen1npoaannmx nsmkax
nporpaHHnpoaannn co c1a1nuecknHn 1nnaHn pannmx (nanpnHep, C+ nnn
Object Pascal) koHnnnn1op npoaepne1, u1o6m snauenne nepeHenno
popn1enLckoro knacca ne npncaanaanocL nepeHenno, onncanno kak
akseHnnnp nopknacca.
HopyHa1e o 1oH, c1on1 nn npoaepka oun6ok, ko1opan c1anoan1cn
aosHoxno npn c1a1nuecknx 1nnax pannmx, nponrpmua a rn6koc1n.
HackonLko aaxnm kon1enepnme knaccm?
Ipe ne cpa6a1maae1 ananornn Bpapa Kokca Hexpy annapa1nmH n
nporpaHHnmH o6ecneuenneH? 1o npenn1c1aye1 paspa6o1ke nepenocnHmx
napexnmx nporpaHHnmx koHnonen1?


converted to PDF by BoJIoc
Fnaaa 11

3aHeeuue u y1ovueuue
[o cnx nop a naue nn1yn1nano Hopenn nacnepoaannn pannme n He1opm pouepnnx
knaccoa okasmaanncL unpe, ueH a popn1enLckoH knacce. To ec1L nopknacc npoc1o po6aannn
noame pannme nnn He1opm k 1eH, u1o 6mnn ynacnepoaanm o1 napknacca. Opnako ncno, u1o
a1o ne acerpa nHee1 Hec1o. HoHnn1e y1konocoa Onna n Onnnnc, onncannmx a rnaae 1? B
coo1ae1c1ann c naunHn paccyxpennnHn Hnekonn1akune a1o xnaopopnune. TeH ne Henee
Onnnnc, c1oko 1pe6ykuan o1nec1n ee k sakonnmH unenaH ceHec1aa Hnekonn1akunx,
pae1 xnsnL pe1enmuaH, o1knapmaan nua.
B a1o cn1yaunn nopknacc y1konocoa Platypus ne npoc1o po6aanne1 u1o-1o k nnqopHaunn o
popn1enLckoH knacce Hnekonn1akunx Mammal. Ha caHoH pene pouepnn knacc nsHenne1
neko1opme caoc1aa popn1enLckoro knacca. 1o6m nonn1L, kak nopknacc Hopnqnunpye1
cHmcn He1opa popn1enLckoro knacca, Hm ponxnm nccnepoaa1L nonn1ne nacnepoaannn 6onee
noppo6no.
11.1. Qo6aaneuue, saHeeuue u y1ovueuue
Mm npepnonarann po cnx nop, u1o pannme n He1opm, ko1opme po6aannk1 nopknaccm,
o1nnuak1cn o1 ynacnepoaannmx o1 popn1enLckoro knacca. [pyrnHn cnoaaHn, He1opm n
snauennn pannmx, poonpepenennme pouepnnH knaccoH, o1nnuak1cn o1 snauenn n He1opoa
popn1enLcknx (n npapopn1enLcknx) knaccoa. Mm roaopnH o 1ako cn1yaunn, kak o
po6aanennn k npo1okony popn1enLckoro knacca.
Bce Henne1cn, korpa pouepnn knacc onpepenne1 neko1opm He1op nop 1eH xe caHmH
nHeneH, u1o n a popn1enLckoH knacce. B pesynL1a1e He1op pouepnero knacca
nepeonpepenne1 He1op popn1enLckoro knacca. HanpnHep, Hon cneunqnueckne snannn o
knacce y1konocoa nsHennk1 Hon asrnnpm na knacc Hnekonn1akunx.
Kak onncano a rnaae 1, ecnn coo6uenne nocmnae1cn neko1opoHy o6Lek1y, 1o nonck
nopxopnuero He1opa acerpa naunnae1cn c npoaepkn He1opoa, cansannmx c knaccoH a1oro
o6Lek1a. Ecnn nn opnn He1op ne napen, npoaepnk1cn He1opm, cansannme c 6nnxaunH
popn1enLcknH knaccoH. Ecnn onn1L-1akn nn opnn He1op ne napen, npoaepne1cn
6nnxaun popn1enLckn knacc popn1enLckoro knacca n 1. p. B konue konuoa nn6o 6onLue
yxe ne oc1ae1cn knaccoa (n 1orpa ampae1cn coo6uenne o6 oun6ke), nn6o naxopn1cn
nopxopnun He1op.
Ioaopn1, u1o He1op a knacce, nHekun 1o xe nHn, u1o n a napknacce, nepeonpepenne1
(override) He1op napknacca. Bo apeHn noncka He1opa, ko1opm amsmaae1cn a kauec1ae
peakunn na coo6uenne, He1op pouepnero knacca, ec1ec1aenno, 6ype1 napen panLue, ueH
opnonHennm He1op a popn1enLckoH knacce. (Ho 6onLue uac1n nepeonpepenenne cansano
1onLko c He1opaHn. Java epnnc1aennm ns o6cyxpaeHmx naHn nsmkoa nporpaHHnpoaannn
(n ne 1onLko cpepn nnx), ko1opm ponyckae1 orpannuennyk qopHy nepeonpepenennn none
pannmx.)
11.1.1. AHepukauckan u ckaupuuaackan ceHau1uku
Me1op pouepnero knacca Hoxe1 nepeonpepenn1L nacnepyeHm He1op opnnH ns payx
cnoco6oa. B cnyuae saHeuennn He1op popn1enLckoro knacca saHeuae1cn uennkoH ao apeHn
pa6o1m nporpaHHm. To ec1L kop popn1enLckoro knacca nnkorpa ne ncnonnne1cn npn
o6pa6o1ke akseHnnnpoa pouepnero knacca. Y1ounnkun He1op aknkuae1 a ce6n kak uac1L
converted to PDF by BoJIoc
caonx pec1an amsoa He1opa popn1enLckoro knacca. TaknH o6pasoH, popn1enLckoe
noaepenne coxpanne1cn n npncoepnnne1cn.
Hepam 1nn nepeonpepenennn uac1o nasmaak1 aHepnkancko ceHan1nko, nockonLky on
o6muno accounnpye1cn c nsmkaHn nporpaHHnpoaannn aHepnkanckoro nponcxoxpennn
(Smalltatk nnn C++). B1opo nsaec1en kak ckanpnnaackan ceHan1nka, 1ak kak on uaue
acero accounnpye1cn c nsmkoH Simula [Dahl 1966, Birtwistle 1979, Kirkerud 1989], nepamH
o6Lek1no-opnen1npoaannmH nsmkoH nporpaHHnpoaannn, n c 6onee nospnnH nsmkoH Beta
[Madsen 1993]. O6a nsmka nHek1 ckanpnnaackoe nponcxoxpenne.
B rnaae 7 Hm onncann HexannsH po6aanennn noaoro He1opa a pouepnn knacc. Oc1anLnan
uac1L panno rnaam nocanuena HexannsHy saHeuennn n y1ounennn a kaxpoH ns
paccHa1pnaaeHmx naHn nsmkoa. Mm noroaopnH 1akxe o neko1opmx poc1onnc1aax n
nepoc1a1kax a1nx payx nopxopoa.
11.2. 3aHeeuue He1opoa
B nsmke Smalltalk uenme uncna n uncna c nnaaakue 1ouko nannk1cn o6Lek1aHn
akseHnnnpaHn knaccoa Integer n Float coo1ae1c1aenno. B caok ouepepL o6a a1n knacca
nopknaccm 6onee o6uero knacca Number. TenepL npepnonoxnH, u1o y nac ec1L nepeHennan
aNumber, ko1opan copepxn1 a 1ekyun HoHen1 snauenne, o1nocnueecn k knaccy uenmx
uncen, n Hm nocmnaeH a1o nepeHenno coo6uenne sqrt, amsmaakuee amuncnenne
kaappa1noro kopnn. Me1opa, coo1ae1c1aykuero a1oHy nHenn, a knacce Integer ne1, noa1oHy
nccnepye1cn knacc Number n naxopn1cn cnepykun He1op:
sqrt
" npeopasosais x uwcnam c nnasaxmen iouxon "
" saiem s-uwcnwis xsazpain-n xopens "
self asFloat sqrt
D1o1 He1op nocmnae1 coo6uenne asFloat nepeHenno self, ko1opan nonyunna coo6uenne
sqrt. Coo6uenne asFloat aosapauae1 uncno c nnaaakue 1ouko 1o xe aennunnm, u1o n
ncxopnoe uenoe. 3a1eH a1oHy snauennk nocmnae1cn coo6uenne sqrt. Honck He1opa
naunnae1cn c knacca Float. O6napyxnaae1cn, u1o knacc Float copepxn1 He1op c 1eH xe
nHeneH sqrt, ko1opm pnn uncen c nnaaakue 1ouko nepeonpepenne1 co6o He1op a
knacce Number. Me1op knacca Float (ko1opm specL ne npnaepen) amuncnne1 n aosapauae1
kaappa1nm kopenL.
Hepeonpepenenne n nonnoe saHeuenne He1opa sqrt osnauae1, u1o Hnorne 1nnm uncen Hory1
o6napa1L epnnc1aenno npouepypo no yHonuannk, pacnonoxenno a knacce acex uncen
Number. Takoe coaHec1noe ncnonLsoaanne nosaonne1 ns6exa1L neo6xopnHoc1n noa1opn1L
a1o1 kop pnn kaxporo nopknacca knacca Number (ko1opm copepxn1 ne 1onLko uenme uncna
n uncna c nnaaakue 1ouko, no n uenme uncna c neorpannuenno paspnpnoc1Lk, a 1akxe
paunonanLnme uncna). Knaccm 1nna Float, ko1opme 1pe6yk1 o1nnunoro o1 yc1anoanennoro
no yHonuannk noaepennn, nerko Hory1 nepeonpepenn1L He1op n nopc1aann1L
anL1epna1nanm kop.
11.2.1. 3aHeeuue He1opoa u npuuun nopc1auoaku
Ocnoanan 1pypnoc1L npn ncnonLsoaannn saHeuennn He1opoa a kauec1ae qynpaHen1anLno
Hopenn nacnepoaannn npepnonoxenne, u1o nopknaccm nannk1cn nop1nnaHn. BcnoHnn1e,
u1o ocnoao npnnunna nopc1anoakn nanne1cn 1o, u1o npepc1aan1enn nopknacca aepy1 ce6n
ao acex cyuec1aennmx o1nouennnx nopo6no npepc1aan1ennH popn1enLckoro knacca. Ho
ecnn nopknaccm Hory1 nepeonpepenn1L He1opm, 1o rpe rapan1nn 1oro, u1o noaepenne
pouepnero knacca 6ype1 nHe1L xo1L kakoe-1o o1nouenne k popn1enLckoHy knaccy?
B 1onLko u1o ynoHnny1oH npnHepe He1opm a o6onx knaccax Float n Number 6mnn
cansanm 1onLko nx konuen1yanLnmHn o1nouennnHn k a6c1pak1noHy nonn1nk kaappa1nm
kopenL. Moxe1 aosnnkny1L 6onLuo 6ecnopnpok, ecnn nopknacc noHenne1 noaepenne
converted to PDF by BoJIoc
ynacnepoaannoro He1opa cnnukoH papnkanLno nanpnHep, nsHennn sqrt 1ak, u1o6m on
amuncnnn norapnqH.
B nsmkax nporpaHHnpoaannn Hoxno ac1pe1n1L neckonLko aosHoxnmx ny1e peuennn
konqnnk1a Hexpy saHeuenneH He1opoa n npnnunnoH nopc1anoakn:
1. Hpoc1o nrnopnpoaa1L a1y npo6neHy n npepoc1aan1L nporpaHHnc1y sa6o1n1Lcn o 1oH,
u1o6m nopknaccm penann npaannLnme aeun ao acex aaxnmx cn1yaunnx. D1o1
nopxop npnnn1 na aoopyxenne ao acex paccHa1pnaaeHmx naHn nsmkax
nporpaHHnpoaannn: Object Pascal, C++, Objective-C, Java n Smalltalk.
2. B nsmke Eiffel [Meyer 1988a, Rist 1995], eue opnoH xopouo nsaec1noH o6Lek1no-
opnen1npoaannoH nsmke, nporpaHHnc1 Hoxe1 npncoepnnn1L k He1opy y1aepxpennn.
Onn nannk1cn nornuecknHn ampaxennnHn, ko1opme npoaepnk1 coc1onnne o6Lek1a
ao apeHn ncnonnennn, o6ecneunaan 1eH caHmH amnonnenne onpepenennmx ycnoan.
Y1aepxpennn aa1oHa1nueckn nacnepyk1cn nopknaccaHn a npexne qopHe, paxe
korpa 1ekyune He1opm nepeonpepennk1cn He1opaHn nopknacca. TaknH o6pasoH,
y1aepxpennn Hory1 ncnonLsoaa1Lcn pnn 1oro, u1o6m ypoc1oaepn1Lcn, u1o pouepnn
knacc aepe1 ce6n ponyc1nHmH o6pasoH.
3. Paspenn1L nonn1nn nopknacca n nop1nna, kak a1o uac1nuno cpenano a Java.
Hopknaccm sa1eH Hory1 ncnonLsoaa1L ceHan1nky saHeuennn a kauec1ae 1exnnkn
peannsaunn, npn a1oH ne o6nsa1enLno noppasyHeaan, u1o pesynL1npykun knacc
6ype1 nop1nnoH nepaonauanLnoro knacca.
4. Coaepuenno o16pocn1L ceHan1nky saHeuennn, a ncnonLsoaa1L y1ounenne. D1a
aosHoxnoc1L paccHa1pnaae1cn a cnepykueH paspene.
11.2.2. YaepoHneuue o saHeeuuu
Hsmkn nporpaHHnpoaannn ncnonLsyk1 pasnnunme nopxopm k ykasannk na 1o, u1o
neko1opm He1op nepeonpepenne1cn (neaaxno kak: c saHeuenneH nnn y1ounenneH).
Smalltalk, Java n Objective-C aoo6ue ne 1pe6yk1 ykasannn na nepeonpepenenne. B C++
6asoam (popn1enLckn) knacc ponxen nHe1L cneunanLnme ykasannn o aosHoxnoc1n
nepeonpepenennn. B nsmke Object Pascal aepcnn Apple a1o ykasanne nponcxopn1 ne a
popn1enLckoH, a a pouepneH knacce. B nsmke Delphi Pascal knkueaoe cnoao ponxno 6m1L
noHeueno a o6a knacca.
Pacnonoxenne kakoro-nn6o ykasannn a popn1enLckoH knacce o6muno o6neruae1
peannsaunk, nockonLky ecnn ne1 aosHoxnoc1n nepeonpepenn1L He1op, 1o nocmnka
coo6uennn peannsye1cn uepes 6onee aqqek1nanm amsoa npouepypm (1o ec1L
pnnaHnueckn nonck ao apeHn amnonnennn ne nyxen). C ppyro c1oponm, cnn1ne
neo6xopnHoc1n yaepoHnennn penae1 nsmk 6onee rn6knH, 1ak kak nosaonne1 ns nk6oro
knacca nopoxpa1L nopknaccm, paxe ecnn aa1op popn1enLckoro knacca ne npepycHa1pnaan
1ako aosHoxnoc1n.
HanpnHep, opnn nporpaHHnc1 cospae1 knacc (ckaxeH, List) pnn konkpe1noro npnnoxennn.
Hospnee ppyro nporpaHHnc1 saxoue1 noc1pon1L cneunannsnpoaannyk qopHy a1oro knacca
(nanpnHep, OrderedList), nepeonpepennn Hnorne ero He1opm. B 1aknx nsmkax, kak C++, a1o
no1pe6ye1 1ekc1oamx nsHenenn a ncxopnoH knacce, u1o6m o6Lnan1L He1opm kak
anp1yanLnme (virtual). Hanpo1na, a Java nnn Objective-C ne no1pe6ye1cn nnkaknx
nsHenenn a onncannn popn1enLckoro knacca.
11.3. 3aHeeuue a pasumx nsmkax
Hnxe kpa1ko xapak1epnsye1cn saHeuenne a pasnnunmx nsmkax nporpaHHnpoaannn.

converted to PDF by BoJIoc
11.3.1. 3aHeeuue a C++
Hepeonpepenenne He1opoa a nsmke C++ ycnoxnne1cn asanHnmH nepekpm1neH
nepeonpepenennn, neperpyskn, anp1yanLnmx (nnn nonnHopqnmx) qynkun n
konc1pyk1opoa. Heperpyska n anp1yanLnme qynkunn paccHo1penm noppo6nee a cnepykunx
rnaaax. B a1oH xe paspene Hm orpannunHcn npoc1mH saHeuenneH (nnc1nnr 11.1).
Ruc1uur 11.1. Onncannn knacca n nopknacca a C++
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 &);
};

Hpoc1oe saHeuenne ac1peuae1cn a 1oH cnyuae, korpa 1) apryHen1m He1opa pouepnero
knacca npen1nunm no 1nny n uncny apryHen1oa He1opy popn1enLckoro knacca, 2) pnn
onncannn He1opa a popn1enLckoH knacce ncnonLsye1cn Hopnqnka1op virtual
1
. HpnHep:
knacc CardPile, ncnonLsyeHm a nacLnnce ns rnaam 8. Ecnn Hm ponxnm nepeaec1n nacLnnc
na C++, 1o o6Lnanennn Hory1 amrnnpe1L npnHepno 1ak, kak a nnc1nnre 11.1, rpe
npepc1aaneno onncanne knacca CardPile n ero nopknacca (a pannoH cnyuae SuitPile).
Me1op canTake onncan 1aknH o6pasoH, u1o on nepeonpepenne1 He1op popn1enLckoro knacca.
Hocnepnn roaopn1 1onLko ne1 on acerpa aosapauae1 snauenne noxL a o1ae1 na
sanpoc, Hoxno nn nonoxn1L a c1onky noayk kap1y:
bool CardPile::canTake (card & c) {
// scerza nei
return false;
}

1
3a opnnH ncknkuenneH: korpa 1nn aosapauaeHoro snauennn pnn qynkunn,
onncanno a pouepneH knacce, ne coanapae1 c 1nnoH aosapauaeHoro snauennn
He1opa popn1enLckoro knacca. CH. paspen 12.3.1 cnepykue rnaam.
Hanpo1na, a1o1 He1op a knacce SuitPile pae1 snauenne nc1nna, ecnn konopa nyc1a n kap1a
nanne1cn 1ysoH nnn ecnn kap1a nopxopn1 no Hac1n aepxne kap1e konopm n na epnnnuy
6onLue ee no panry:
bool SuitPile::canTake (card & c)
{
converted to PDF by BoJIoc
// mo+no zoaswis x cionxe iysa
if (empty ())
return c.rank() == 0;
card & topcard = top()
// wnaue zon+no -is cosnazenwe no maciw
// w xapia zon+na -is cnezyxmen no ciapmwncisy
if ((c.suit() == topcard.suit()) &&
(c.rank() == 1 + topcard.rank()))
return true;
return false;
}
[nn a1o He1opnkn coaepuenno neo6xopnHo, u1o6m He1op pouepnero knacca nonnoc1Lk
nepekpmaan He1op popn1enn.
[nn koHnnnn1opa nsmka C++ ec1L 1onkn cHmcnoao nkanc a 1oH, o6Lnanen nn He1op
canTake anp1yanLnmH n a knacce CardPile 1oxe. O6a aapnan1a nHek1 npaao na
cyuec1aoaanne. 1o6m He1op pa6o1an a coo1ae1c1ann c 1eH, u1o Hm nasmaaeH o6Lek1no-
opnen1npoaannmH c1nneH, on ponxen 6m1L o6Lnanen kak anp1yanLnm. Mopnqnka1op
virtual neo6nsa1enen npn onncannn a pouepneH knacce. Kak 1onLko He1op o6Lnanne1cn
anp1yanLnmH, on oc1ae1cn anp1yanLnmH n ao acex nopknaccax. Opnako pnn uene
pokyHen1npoaannn a1o1 Hopnqnka1op o6muno noa1opne1cn ao acex nponsaopnmx knaccax.
Ecnn Hopnqnka1op virtual ne sapan, He1op no-npexneHy 6ype1 saHeua1L opnonHennm
He1op popn1enLckoro knacca. Opnako npouecc cansmaannn He1opa n coo6uennn 6ype1
nponcxopn1L no-ppyroHy. Heanp1yanLnme He1opm nannk1cn c1a1nuecknHn a cHmcne,
onncannoH a rnaae 10. To ec1L cansmaanne amsoaa neanp1yanLnoro He1opa 6ype1
amnonnn1Lcn ao apeHn koHnnnnunn, ncxopn ns o6Lnanennoro (c1a1nueckoro) 1nna
nonyua1enn, a ne ao apeHn amnonnennn nporpaHHm, ncxopn ns pnnaHnueckoro 1nna
nonyua1enn. Ecnn knkueaoe cnoao virtual ypaneno ns onncannn He1opa canTake, 1o
nepeHennme, o6Lnanennme kak SuitPile, 6ypy1 amnonnn1L He1op ns knacca SuitPile, a
nepeHennme, o6Lnanennme kak CardPile, 6ypy1 amnonnn1L He1op no yHonuannk nesaancnHo
o1 pec1an1enLnoro snauennn nepeHenno.
Eue opnn ycnoxnnkun qak1op npn nepeonpepenennn qynkun a nsmke C++ a1o
asanHopec1ane Hexpy neperpysko n nepeonpepenenneH. Mm 6ypeH o6cyxpa1L a1o a
rnaae 17. Cnoxnan ceHan1nka saHeuennn He1opoa a nsmke C++ naxopn1cn a cornacnn c ero
qnnocoqne, ko1opan coc1on1 a 1oH, u1o caoc1aa nsmka ponxnm npnaopn1L k
pononnn1enLnmH naknapnmH pacxopaH ao apeHn amnonnennn nporpaHHm 1onLko a 1oH
cnyuae, korpa a1n caoc1aa ncnonLsyk1cn. Ecnn anp1yanLnme qynkunn ne npnHennk1cn, 1o
nacnepoaanne ne naansmaae1
a6conk1no nnkaknx nspepxek apeHenn amnonnennn. D1o ne 1ak pnn ppyrnx
paccHa1pnaaeHmx naHn nsmkoa nporpaHHnpoaannn. Opnako 1o1 qak1, u1o cnn1akcncoH
nsmka posaonenm kak anp1yanLnme, 1ak n neanp1yanLnme qopHm sanncn, npnueH a1o
npnaopn1 k pasnnunnH a nn1epnpe1aunn, uac1o nanne1cn nc1ounnkoH 1pypnoynoanHmx
oun6ok a nporpaHHax na nsmke C++.
Konc1pyk1opm a C++ acerpa ncnonLsyk1 ceHan1nky y1ounennn, a ne saHeuennn. Mm
o6cypnH a1o nocne 1oro, kak aaepeH nonn1ne y1ounennn He1opoa.
11.3.2. 3aHeeuue He1opoa a Object Pascal
B nsmke Object Pascal aepcnn qnpHm Apple He1op Hoxe1 saHeua1L He1op napknacca, 1onLko
ecnn:
nHn He1opa nnue1cn 1ouno 1ak xe, kak a popn1enLckoH knacce;
nopnpok, 1nnm, nHena napaHe1poa n 1nn pesynL1a1a qynkun 1ouno coanapak1;
converted to PDF by BoJIoc
onncanne He1opa a pouepneH knacce cnepye1 sa knkueamH cnoaoH override.
nc1nnr 11.2 nnnkc1pnpye1 saHeuenne He1opa. 3pecL knacc Employee o6uee onncanne
cnyxaunx qnpHm, a knaccm SalaryEmployee n HourlyEmployee paa ero nopknacca.
Oynkunn computePay a knacce Employee amuncnne1 sapnna1y sa pannm nepnop. D1o1
He1op nepeonpepenne1cn pnn nopknaccoa, nockonLky amuncnennn, ncnonLsyeHme pnn payx
1nnoa cnyxaunx, pasnnunm.
PaccHo1pnH nepeHennyk emp, o6Lnanennyk kak akseHnnnp knacca Employee. Kak Hm yxe
o1Heuann, ona Hoxe1 nHe1L snauenne nn6o knacca SalaryEmployee, nn6o knacca
HourlyEmployee (nnn nk6oro ppyroro 1nna cnyxaunx). HesaancnHo o1 ee snauennn
o6pauenne k npouepype computePay npnaepe1 k amsoay nyxnoro He1opa pnn
coo1ae1c1aykuero 1nna cnyxaunx.
Cnn1akcnc, ncnonLsyeHm nsmkoH Delphi Pascal qnpHm Borland, ropaspo 6nnxe k C++. B
nsmke qnpHm Borland He1op, nepeonpepenneHm a nopknaccax, ponxen cnepoaa1L sa
knkueamH cnoaoH virtual a popn1enLckoH knacce, kak a1o nokasano a nnc1nnre 11.3.
Ruc1uur 11.2. 3aHeuenne He1opa a nsmke Object Pascal qnpHm 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; (* yzei nepeonpezenniscn nozxnaccamw *)
end;
function HourlyEmployee.computePay : integer;
begin
return hoursworked * wage;
(* sapnnaia pasna uwcny uacos, ymno+ennomy na ciasxy *)
end;
function SalaryEmployee.computePay : integer;
begin
return salary div 12;
(* zenwm ycianosnennyx sapnnaiy s roz na uwcno mecnues *)
end;



converted to PDF by BoJIoc
Ruc1uur 11.3. YaepoHnenne o saHeuennn a Delphi Pascal
type
Employee = class (TObject)
name : string;
function computePay : integer; virtual;
function hourlyWorker : boolean; virtual;
constructor create; virtual;
end;
SalaryEmployee = class (Employee)
salary : integer;
function computePay : integer; override;
function hourlyWorker : boolean; override;
constructor create; override;
end;

11.3.3. 3aHeeuue a Smalltalk u Objective-C
B nsmkax Smalltalk n Objective-C He1op, nHekun 1o xe caHoe nHn, u1o n He1op
popn1enLckoro knacca, acerpa nepeonpepenne1 n nonnoc1Lk saHenne1 co6o He1op knacca-
npepka. HonLsoaa1enk ne1 neo6xopnHoc1n nano ykasmaa1L, u1o kako-1o He1op
nepeonpepenne1cn. Opnako npn xopoueH pokyHen1npoaannn nporpaHHnc1y nonesno
o1He1n1L a1o1 qak1 a koHHen1apnn.
11.3.4. 3aHeeuue a Java
B nsmke nporpaHHnpoaannn Java, kak n a Smalltalk n Objective-C, ne1 neo6xopnHoc1n
yaepoHnn1L o nepeonpepenennn He1opa. [oc1a1ouno 1oro, u1o noam He1op nHee1 1o xe
caHoe nHn, cnncok apryHen1oa n 1nn aosapauaeHoro snauennn, u1o n He1op popn1enLckoro
knacca.
Kak o1HeuanocL a rnaae 10, a nsmke Java aosHoxno paxe saHeuenne none pannmx (uero
ne1 nn a opnoH ppyroH ns paccHa1pnaaeHmx naHn nsmkoa). Opnako 1akne saHeuennn ne
nannk1cn pnnaHnuecknHn, n npn ncnonLsoaannn nonn pannmx ero 1nn 6ype1 onpepenn1Lcn
o6LnanennmH (c1a1nuecknH) knaccoH nepeHenno, a ne ee pnnaHnuecknH snauenneH. D1a
cn1yaunn nnorpa onncmaae1cn kak Hacknpoaka a pouepneH knacce nepeHenno popn1enn.
HpnHep 6mn npnaepen a rnaae 10.
Hn1epecnoe caoc1ao Java knkueaoe cnoao final. Ecnn ono npnHenne1cn k nHenn He1opa,
1o onpepenne1, u1o He1op nanne1cn nnc1oH (1epHnna1opoH) a nepapxnueckoH pepeae
knacca n ne Hoxe1 6m1L a panLneueH nepeonpepenen kaknH 6m 1o nn 6mno cnoco6oH.
Ecnn a1o knkueaoe cnoao ac1peuae1cn a onpepenennn knacca, 1o a1o osnauae1, u1o ns
knacca ne Hory1 nopoxpa1Lcn nopknaccm. KoHnnnn1opy nsmka Java nosaoneno
on1nHnsnpoaa1L He1opm, onncannme c noHouLk knkueaoro cnoaa final, npeapauan nx a
inline-qynkunk n nopc1aannn ee nanm kop a 1ouky amsoaa (u1o nanoHnnae1 nsmk Beta, cH.
paspen 11.4.1).
11.4. Y1ovueuue He1opoa
Panee Hm o1Heuann pnneHHy Hexpy nepeonpepenenneH c saHeuenneH n coxpanenneH
caoc1a knacca a nop1nne. Opnn ns cnoco6oa cHnruennn a1o npo6neHm nsHenenne
ceHan1nkn nepeonpepenennn. BHec1o 1oro u1o6m nonnoc1Lk saHenn1L kop popn1enLckoro
knacca, pec1ann pouepnero knacca koH6nnnpyk1cn c pec1annHn popn1enn. TeH caHmH
rapan1npye1cn, u1o pec1ann popn1enLckoro knacca 6ypy1 o1pa6a1maa1Lcn ao acex cnyuanx
(a1nH o6ecneunaae1cn HnnnHanLnm ypoaenL qynkunonanLnoc1n). Xena1enLnoc1L 1akoro
noaepennn nan6onee uac1o nponanne1cn npn nnnunannsaunn noaoro o6Lek1a. B a1oH cnyuae
Hm xo1nH ocyuec1an1L pec1ann no nnnunannsaunn, onpepenennme pnn popn1enLckoro
converted to PDF by BoJIoc
knacca, n sa1eH yxe nk6me ppyrne nnnunannsnpykune pec1ann, ko1opme Hory1
nonapo6n1Lcn pnn pouepnero knacca.
Tak kak a 6onLunnc1ae o6Lek1no-opnen1npoaannmx nsmkoa pouepnnH knaccoH nacnepye1cn
poc1yn n k pannmH, n k He1opaH, po6aanenne noamx qynkun Hoxe1 6m1L poc1nrny1o
npoc1o konnpoaanneH kopa ns popn1enLckoro knacca. Ho a1o1 nopxop napyuae1 neko1opme
aaxnme npnnunnm xopouero c1nnn nporpaHHnpoaannn. HanpnHep, on cnnxae1 ypoaenL
coaHec1noro ncnonLsoaannn kopa, npenn1c1aye1 Hacknpoake nnqopHaunn o popn1enLckoH
knacce n yHenLuae1 napexnoc1L, 1ak kak a npouecce konnpoaannn Hory1 nonann1Lcn
oun6kn, n, kpoHe 1oro, ncnpaanenne oun6ok a popn1enLckoH knacce Hoxe1 ne
pacnpoc1pann1Lcn na no1oHkoa.
Ho a1o npnunne nonesno nHe1L neko1opm HexannsH any1pn nepeonpepenneHoro He1opa,
ko1opm 6m amsmaan He1op-npepuec1aennnk popn1enLckoro knacca n 1aknH o6pasoH
noa1opno ncnonLsoaan 6m kop nepeonpepenneHoro He1opa. Korpa He1op amsmaae1 1aknH
cnoco6oH nepeonpepenneHm He1op popn1enLckoro knacca, Hm 6ypeH roaopn1L, u1o noam
He1op y1ounne1 noaepenne popn1enLckoro knacca.
11.4.1. Y1ovueuue a nsmkax Simula u Beta
CeHan1nka y1ounennn nonannacL a caHoH nepaoH o6Lek1no-opnen1npoaannoH nsmke
Simula, ko1opm 6mn paspa6o1an a nauane 1960 ropa [Dahl 1966]. B nsmke Simula
nnnunannsaunn anoaL cospaaaeHoro o6Lek1a onpepennnacL 6nokoH koHanp,
npncoepnnennmH k onpepenennk knacca, kak nokasano nnxe:
class Employee
begin
integer identificationNumber;
comment onwcanwe xnacca onymeno;
comment onepaiop-, npezciasnenn-e szecs,
comment s-nonnnxi wnwuwanwsauwx
comment xa+zoro snoss coszasaemoro o+exia;
identificationNumber :=
prompt_for_integer("Enter idNumber: ");
inner;
end;
D1o1 6nok nnnunannsaunn amnonnne1cn kaxpm pas npn cospannn akseHnnnpa knacca
Employee. Knkueaoe cnoao inner a 6noke nnnunannsaunn onpepenne1 1ouky ac1aakn, rpe
amnonnnk1cn pononnn1enLnme pec1ann nopknacca. HanpnHep, npepc1aaL1e ce6e, u1o Hm
c1ponH nopknacc knacca Employee, ko1opm npepc1aanne1 pa6o1akunx c nouacoao
onna1o. [nn a1oro knacca 1akxe Hoxe1 6m1L sapan 6nok nnnunannsaunn. Korpa cospae1cn
kako-nn6o akseHnnnp knacca HourlyEmployee, cnauana amnonnne1cn 6nok nnnunannsaunn
ns popn1enLckoro knacca Employee. Hpn poc1nxennn koHanpm inner amsmaae1cn 6nok
nnnunannsaunn knacca HourlyEmployee. D1o1 6nok Hoxe1 a caok ouepepL copepxa1L
co6c1aennm onepa1op inner, ko1opm sanyckae1 6nokn nnnunannsaunn nopknaccoa
cnepykuero ypoann anoxennoc1n n 1. p. Ecnn ne1 nnkaknx nopknaccoa, onepa1op inner ne
penae1 nnuero:
Employee class HourlyEmployee
begin
integer hourlyWage;
... comment onwcanwe xnacca onymeno;
hourlyWage :=
prompt_for_integer("Enter wage: ");
inner;
end;
Hsmk nporpaHHnpoaannn Simula ncnonLsye1 y1ounenne n knkueaoe cnoao inner 1onLko ao
apeHn nnnunannsaunn noamx o6Lek1oa. Hepeonpepenenne o6munmx He1opoa nponsaopn1cn
converted to PDF by BoJIoc
c noHouLk ceHan1nkn saHeuennn. Hsmky nporpaHHnpoaannn Beta [Madsen 1993] oc1anocL
1onLko cnc1eHa1nueckn npnHenn1L ceHan1nky y1ounennn ko aceH He1opaH nocpepc1aoH
ynnqnkaunn onncann knaccoa, qynkun n He1opoa a epnnyk konc1pykunk, nasmaaeHyk
cxeHo (pattern) (ne ny1a1e co cxeHaHn paspa6o1kn knaccoa (design patterns), ko1opme Hm
6ypeH o6cyxpa1L a rnaae 18).
1o6m nponnnkc1pnpoaa1L c1nnL nsmka Beta a o1nouennn cxeH n y1ounennn He1opoa,
paccHo1pnH cnauana npnHep, ko1opm ncnonLsye1 npoc1me qynkunn, a ne knaccm n
He1opm. HpepnonoxnH, Hm xo1nH sapa1L pec1ann pnn neua1n HTML-1aroa pnn WWW-
appecoa. Me1ka-1ar coc1on1 ns neko1oporo nauanLnoro 1ekc1a, sa ko1opmH cnepye1 none
URL (appec Haunnm n nonnoe nHn qana), a sa nnH npe1 neko1opm saknkun1enLnm
1ekc1. Honnm HTML-1ar Hoxe1 amrnnpe1L 1ak:
<A HREF="http://www.cs.orst.edu/~budd/oop.html">
B nsmke Beta Hm HoxeH poc1nrny1L a1oro, ncnonLsyn cnauana qynkunk, ko1opan neua1ae1
1onLko nauanLnm n koneunm 1ekc1, a ne peanLnm WWW-appec:
printAnchor:
(#
do
'<A HREF="http:'->puttext; INNER
'">'->puttext
#);
KoHanpa puttext ocyuec1anne1 1ekc1oam amaop. Tpn onepa1opa npnaepenno amue
qynkunn cospak1 nauanLnm 1ekc1, sa ko1opmH cnepye1 y1ounenne (ecnn ono ec1L), a
no1oH koneunm 1ekc1.
B1opan qynkunn Hoxe1 cneunannsnpoaa1L pec1ane nepao ny1eH orpannuennn Web-
appecoa po konkpe1no o6nac1n nanpnHep, po appecoa, coo1ae1c1aykunx Web-cepaepy a
ynnaepcn1e1e u1a1a Operon. D1o poc1nrae1cn onpepenenneH noao qynkunn, y1ounnkue
nepayk. Hoaan qynkunn sapae1 pec1ann, ko1opme ponxnm 6m1L amnonnenm aHec1o
onepa1opa INNER a ncxopno qynkunn:
printOSUAnchor : printAnchor
(#
do
'//www.cs.orst.edu/'->puttext;
INNER
#);
Korpa Hm amsmaaeH a1y qynkunk, kop a popn1enLcko qynkunn (printAnchor) amnonnne1cn
nepamH, 1ak kak Hm onpepennnn, u1o noaan qynkunn nanne1cn y1ounenneH nepao. Bo
apeHn amnonnennn onepa1opa INNER a popn1enLcko qynkunn nponcxopn1 amsoa kopa
noao qynkunn. B pannoH cnyuae a1o1 kop neua1ae1 konkpe1nm Web-appec, a sa1eH
amnonnne1 nk6me panLneune y1ounnkune pec1ann.
Y1ounenne Hoxe1 6m1L pacunpeno na nk6yk rny6nny. HanpnHep, 1pe1Ln qynkunn Hoxe1
konc1pynpoaa1L Web-appec pnn o1penLnoro nnpnanpyyHa:
printBuddAnchor : printOSUAnchor
(#
do
'~budd/'->puttext;
INNER
#);
B pesynL1a1e amnonnennn qynkunn printBuddAnchor nonyuaeH 1ekc1:
<A HREF="http://www.cs.orst.edu/~budd/">
B nsmke Beta y1ounennn Hory1 6m1L paxe ac1panaaeHmHn. B pesynL1a1e amnonnennn
onepa1opa
converted to PDF by BoJIoc
printBuddAnchor(# do 'oop.html/'->puttext #)
nonyuaeH na amxope 1ekc1:
<A HREF="http://www.cs.orst.edu/~budd/oop.html/">
Hpn onncannn knacca aqqek1, ko1opm Hm onncmaaeH kak y1ounenne, poc1nrae1cn
koH6nnnpoaanneH y1ounennn qynkunn n anp1yanLnmx He1opoa (nasmaaeHmx a
nsmke Beta virtual pattern declarations). Kak n a npepmpyueH naueH npnHepe, Hm
cospaeH knacc Employee, copepxaun cpepn npounx aneHen1oa ynnkanLnm
npen1nqnkaunonnm noHep cnyxauero n qynkunk pnn o1o6paxennn nnqopHaunn o
cnyxaueH:
Employee :
(#
identificationNumber : @integer;
display:<
(#
do 'Employee Number: '->puttext;
identificationNumber->printInteger;
INNER
#);
#);
Hopknacc, nopo6nm npnaepennoHy nnxe nopknaccy HourlyEmployee, pacunpne1
popn1enLckn knacc. Onpepenennme any1pn nopknacca anp1yanLnme qynkunn
pacunpnk1 He1opm popn1enLckoro knacca c noHouLk yxe paccHo1pennoro naHn
HexannsHa. B pannoH cnyuae amsoa He1opa display npnaopn1 cnepaa k amnonnennk
qynkunn popn1enLckoro knacca, a sa1eH, a 1ouke axoxpennn onepa1opa INNER,
nponcxopn1 o6pauenne k kopy pouepnero knacca:
HourlyEmployee : Employee
(#
wage : @integer;
display::<
(#
do
' wage: '->puttext;
wage->printInteger;
INNER
#)
#);
HopunnnncL npnnunny nopc1anoakn, akseHnnnp nopknacca HourlyEmployee Hoxno
nopc1aan1L aHec1o akseHnnnpa knacca Employee. Ecnn nap nepamH amnonnn1L
pec1ane display, 1o cnepaa amsoae1cn He1op popn1enLckoro knacca. Hpn
poc1nxennn koHanpm INNER 6ypy1 ac1aanenm koHanpm ns qynkunn display
pouepnero knacca.
Cnc1eHa1nueckoe ncnonLsoaanne y1ounennn pnn nepeonpepenennn He1opoa
nanne1cn konuen1yanLno aneran1nmH n penae1 nou1n neaosHoxnmH nanncanne
nopknacca, ne nannkuerocn nop1nnoH. C ppyro c1oponm, Hnorne 1pkkn, nonesnme
npn ceHan1nke saHenm (apope onncanno panee peannsaunn qynkunn sqrt a nsmke
Smalltalk), 1pypno cHopennpoaa1L a 1aknx nsmkax nporpaHHnpoaannn, kak Beta.
Ckopee acero, 1onLko nc1opnueckan cnyuanoc1L (aosHoxno, akyne c 1eH qak1oH,
u1o y1ounenne neckonLko cnoxnee a peannsaunn, ueH saHeuenne) o6Lncnne1
poHnnnpoaanne nsmkoa, ncnonLsykunx saHeuenne He1opoa.
11.4.2. Me1opm-o6onovku a nsmke CLOS
Eue opna nn1epecnan aapnaunn na 1eHy ceHan1nkn y1ounennn ac1peuae1cn a nsmke
nporpaHHnpoaannn CLOS [Keen 1989] pnanek1e Lisp. B nsmke CLOS nopknacc Hoxe1
converted to PDF by BoJIoc
nepeonpepenn1L He1op a popn1enLckoH knacce n aaopn1L He1op-o6onouky (wrapping
method). Me1op-o6onouka Hoxe1 6m1L He1opoH-po, He1opoH-nocne nnn He1opoH-any1pn. B
coo1ae1c1ann co caonH 1nnoH, po He1opa, nocne He1opa nnn any1pn He1opa, amnonnne1cn
amsoa He1opa popn1enLckoro knacca. B nocnepneH cnyuae cneunanLnm onepa1op
pouepnero He1opa call-next-method amsmaae1 He1op popn1enLckoro knacca. D1o nanoHnnae1
cnoco6, ko1opmH Hopennpye1cn y1ounenne a 1aknx nsmkax nporpaHHnpoaannn, kak C++ n
Object Pascal.
11.5. Y1ovueuue a pasumx nsmkax
Cpepn paccHa1pnaaeHmx naHn nsmkoa nporpaHHnpoaannn 1onLko C++ ncnonLsye1
ceHan1nky y1ounennn, pa n 1o a ocnoanoH pnn konc1pyk1opoa. (C++ cnepye1 a a1oH
o1nouennn nsmky Simula, rpe ceHan1nka y1ounennn ncnonLsye1cn 1onLko ao apeHn
nnnunannsaunn.) Opnako ao acex nsmkax aqqek1 y1ounennn Hoxe1 6m1L cHopennpoaan c
noHouLk ppyrnx HexannsHoa. Kak poc1nuL a1oro, 6ype1 onncano a cnepykunx paspenax.
11.5.1. Y1ovueuue a Object Pascal
Y1ounenne a nsmke nporpaHHnpoaannn Object Pascal ocyuec1anne1cn He1opoH pouepnero
knacca, ko1opm nanmH o6pasoH amsmaae1 nepeonpepenneHm He1op popn1enLckoro
knacca. D1o nopxop 6onee nnn Henee npo1naononoxen npnnn1oHy a nsmkax Simula nnn
Beta, rpe He1op popn1enLckoro knacca caH amsmaae1 He1op ns pouepnero knacca. Opnako a
6onLunnc1ae cnyuaea aqqek1 opnnakoa o6a He1opa 6ypy1 amnonnenm.
Knkueaoe cnoao inherited ncnonLsye1cn any1pn pouepnero knacca, u1o6m o1He1n1L 1ouky
any1pn pouepnero He1opa, a ko1opo ponxen 6m1L amsaan He1op popn1enLckoro knacca.
HpepnonoxnH, nanpnHep, u1o Hm xo1nH nannca1L He1op c nHeneH initialize, ko1opm 6ype1
sanpaunaa1L y nonLsoaa1enn snauennn, nnnunannsnpykune nonn pannmx o6Lek1a. D1o1
He1op pnn popn1enLckoro knacca Employee Hor 6m amrnnpe1L cnepykunH o6pasoH:
procedure Employee.initialize;
begin
writeln("enter employee name: ");
readln(name);
end;
Hopknacc SalaryEmployee Hoxe1 npncoepnnn1L nnnunannsaunk none pannmx
popn1enLckoro knacca k caoeHy kopy nnnunannsaunn cnepykunH o6pasoH:
procedure SalaryEmployee.initialize;
begin
inherited initialize;
writeln("enter salary: ");
readln(salary);
end;
B nsmke Delphi Pascal knkueaoe cnoao inherited ncnonLsye1cn paxe a konc1pyk1opax.
Hnorpa a1o nonesno, nockonLky 1ouka any1pn konc1pyk1opa pouepnero knacca, rpe
amsmaae1cn konc1pyk1op popn1enn, Hoxe1 sapaaa1Lcn nporpaHHnc1oH.
11.5.2. Y1ovueuue a C++
B nsmke C++ amsoa He1opa Hoxe1 nHe1L pacunpennm cnn1akcnc coc1aanoro nHenn, npn
ko1opoH aHec1o ncnonLsyeHo no yHonuannk npouepypm noncka nopxopnuero He1opa 1ouno
ykasmaae1cn, ns kakoro knacca ponxen 6pa1Lcn amsmaaeHm He1op. D1o coc1aanoe nHn
sanncmaae1cn kak nHn knacca, sa ko1opmH cnepyk1 paa paoe1ounn n sa1eH nHn He1opa.
HcnonLsoaanne 1akoro coc1aanoro nHenn penae1 nenyxnmH anp1yanLnm HexannsH
nocmnkn coo6uenn n noncka appeca1a n rapan1npye1, u1o He1op 6ype1 amsmaa1Lcn ns
nasaannoro knacca.
converted to PDF by BoJIoc
MexannsH coc1aanoro nHenn npnHenne1cn a nsmke C++ pnn Hopennpoaannn y1ounennn npn
nepeonpepenennn. 3aHeuaeHm He1op nanmH o6pasoH amsmaae1 He1op popn1enn, 1eH
caHmH rapan1npyn, u1o o6a He1opa 6ypy1 amnonnenm.
Mm eue pas sapec1ayeH npnHep ns nporpaHHm nacLnnca, nepenncanno na C++. Me1op
addCard a knacce CardPile amnonnne1 ocnoanme pec1ann no noHeuennk noao kap1m
naaepx c1onkn. Knacc DiscardPile ponxen pononnn1enLno o6ecneunaa1L, u1o6m po6aanneHan
kap1a 6mna o1kpm1a. D1o penae1cn c noHouLk cnepykuero He1opa:
void DiscardPile::addCard (card & newCard)
{
// yezwiscn, uio nosan xapia ne+wi xapiwnxon ssepx
if (! newCard.faceUp())
newCard.flip();
// saiem zoaswis ee x xonoze
CardPile::addCard (newCard);
}
Panee Hm o1Heuann, u1o opnn ns acnek1oa, a ko1opmx konc1pyk1op o1nnuae1cn o1 ppyrnx
He1opoa nsmka C++, coc1on1 a 1oH, u1o a pouepneH knacce on acerpa ncnonLsye1
y1ounenne, a ne saHeuenne. To ec1L konc1pyk1op a pouepneH knacce acerpa amsmaae1
konc1pyk1op popn1enLckoro knacca.
B 1oH cnyuae, ecnn a konc1pyk1ope pouepnero knacca ne pae1cn nnkaknx ykasann n ecnn
popn1enLckn knacc copepxn1 konc1pyk1op, npnnnHaeHm no yHonuannk (konc1pyk1op 6es
apryHen1oa), 1o on n 6ype1 aa1oHa1nueckn amsmaa1Lcn. B npo1nanoH cnyuae ponxnm 6m1L
nano sapanm apryHen1m, ko1opme ncnonLsyk1cn konc1pyk1opoH popn1enLckoro knacca.
Konc1pyk1op popn1enLckoro knacca Hoxe1 amsmaa1Lcn nanmH o6pasoH a saronoake
pouepnero konc1pyk1opa ny1eH ykasannn nHenn popn1enLckoro knacca n apryHen1oa,
ko1opme ncnonLsye1 konc1pyk1op popn1enn. HpnHep: konc1pyk1op knacca TablePile, ko1opm
6epe1 paa apryHen1a n ncnonLsye1 nx pnn amsoaa konc1pyk1opa popn1enLckoro knacca:
TablePile::TablePile (int c, int x, int y)
: CardPile(x, y)
{
column = c;
}
[ec1pyk1opm a nsmke C++ ncnonLsyk1 kak pas npo1naononoxnm nopxop. 3a amsoaoH
pec1pyk1opa pouepnero knacca cnepyk1 amsoam acex ppyrnx pec1pyk1opoa pnn aneHen1oa
pannmx n pnn popn1enLcknx knaccoa.
11.5.3. Y1ovueuue a Smalltalk, Java u Objective-C
B rnaae 6 Hm c1onknynncL c nceaponepeHenno super. Epnnc1aennan (npak1nueckn) ponL
a1o nepeHenno a nsmkax Smalltalk, Java n Objective-C a1o paspeun1L y1ounenne npn
nepeonpepenennn He1opa. Hepepaua coo6uennn nepeHenno super ykasmaae1 na 1o, u1o
nonck coo1ae1c1aykuero He1opa ponxen naunna1Lcn c popn1enn 1ekyuero knacca:
class A
{
private int a;
public initialize()
{
a = 3;
}
}
class B extends A
{
private int b;
public initialize()
converted to PDF by BoJIoc
{
b = 7;
// s-nonnwis meioz pozwienscxoro xnacca
super.initialize();
}
}
Konc1pyk1op a nsmke Java amsmaae1 konc1pyk1op popn1enLckoro knacca c noHouLk
knkueaoro cnoaa super:
class newClass extends oldClass
{
newClass (int a, int b, int c)
{
// s-ssais xoncipyxiop pozwienscxoro xnacca
super (a, b);
// . . .
}
}
C 1o xe caHo uenLk nopo6nme konc1pykunn ncnonLsyk1cn a nsmkax nporpaHHnpoaannn
Smalltalk n Objective-C.
Ynpaxueuun
1. O6ocny1e y1aepxpenne: ecnn ne ncnonLsye1cn nn saHeuenne, nn y1ounenne, 1o
nopknacc acerpa ponxen 6m1L nop1nnoH.
2. Hpnaepn1e npnHep, nnnkc1pnpykun, u1o ecnn nHee1 Hec1o ceHan1nka saHeuennn,
1o nopknacc ne o6nsan 6m1L nop1nnoH.
3. Xo1n cnc1eHa1nueckoe ncnonLsoaanne ceHan1nkn y1ounennn penae1 6onee cnoxnmH
cospanne nopknaccoa, ne nannkunxcn nop1nnaHn, 1akoe ace xe aosHoxno.
Hponnnkc1pnpy1e a1o, npnaepn npnHep y1ounnkuero nopknacca, ne nannkuerocn 1eH ne
Henee nop1nnoH 6asoaoro knacca.
4. ac1o ao apeHn nnnunannsaunn akseHnnnpa nopknacca ponxnm 6m1L amnonnenm no
ouepepn: neko1opm kop popn1enLckoro knacca, kop pouepnero knacca, sa1eH cnoaa kop
popn1enLckoro knacca. HanpnHep, pnn okonnmx cnc1eH popn1enLckn knacc ampenne1
naHn1L pnn aaxnmx c1pyk1yp pannmx, sa1eH pouepnn knacc Hopnqnunpye1 neko1opme
nonn a1nx c1pyk1yp (1akne, kak nHn n pasHep okna), n nakoneu popn1enLckn knacc
o1o6paxae1 okno na akpane pncnnen. Kak pannan nocnepoaa1enLnoc1L amsoaoa Hoxe1 6m1L
amnonnena a o6Lek1no-opnen1npoaannoH nsmke nporpaHHnpoaannn? Hopckaska: aepon1no,
aaH npnpe1cn pas6n1L npouecc nnnunannsaunn na paa coo6uennn.
5. He acerpa ceHan1nka y1ounennn nerko Hopennpye1cn ceHan1nko saHeuennn. 1o6m
npopeHonc1pnpoaa1L a1o, nannun1e na6op knaccoa, o6ecneunaakunx amnonnenne
pec1an, nanoHnnakunx nopnporpaHHm cospannn 1aroa WWW-appecoa, onncannme a
paspene 11.4.1. Kak n a cnyuae ynpaxnennn 4, aaH, aosHoxno, nonapo6n1cn aaec1n pnp
ckpm1mx He1opoa.
Fnaaa 12

Cnepc1aun uacnepoaauun
converted to PDF by BoJIoc
Hacnepoaanne okasmaae1 6onLuoe annnnne npak1nueckn na ace acnek1m nsmka
nporpaHHnpoaannn. B a1o rnaae Hm nccnepyeH neko1opme cnepc1ann, am1ekakune ns
peannsaunn nacnepoaannn, paccHa1pnaan cnc1eHy 1nnoa pannmx, cHmcn onepa1opoa (1nna
onepa1opa npncaanaannn), npoaepky paaenc1aa o6Lek1oa, ampenenne naHn1n.
Mm yxe onncann o1nouenne 6m1L akseHnnnpoH kak qynpaHen1anLnoe caoc1ao
nacnepoaannn. Opna ns 1ouek spennn na o1nouenne 6m1L akseHnnnpoH a1o
paccHa1pnaa1L ero kak cpepc1ao, cansmaakuee 1nn pannmx (a cHmcne 1nna nepeHenno) n
na6op snauenn (a nHenno snauennn, ko1opme Hory1 sakonnmH o6pasoH copepxa1Lcn a
nepeHenno). Ecnn nepeHennan win onncana kak akseHnnnp konkpe1noro knacca, ckaxeH
Window, 1o koneuno xe ona Hoxe1 copepxa1L snauennn 1nna Window. Ecnn Hm nHeeH
nopknacc knacca Window, nanpnHep TextWindow, 1o, nockonLky TextWindow nanne1cn
akseHnnnpoH Window, nHee1 cHmcn npncaon1L nepeHenno win snauenne 1nna
TextWindow. D1o nasmaae1cn npnnunnoH nopc1anoakn, ko1opm Hm ac1peuann a
npepmpyunx rnaaax.
B 1o apeHn kak caH npnnunn nHee1 nn1yn1nano nonn1nm cHmcn, c npak1nuecko 1oukn
spennn nannuec1ayk1 1pypnoc1n peannsaunn o6Lek1no-opnen1npoaannmx nsmkoa 1aknH
o6pasoH, u1o6m a1o nn1yn1nanoe noaepenne Horno 6m1L peannsoaano. Takne 1pypnoc1n ne
nannk1cn nenpeoponnHmHn, no cnoco6 nx peuennn pasnnuae1cn a pasnmx nsmkax.
Hccnepoaanne a1nx npo6neH, a 1akxe 1o, kak onn annnk1 na nsmk nporpaHHnpoaannn,
amnanne1 ckpm1me caoc1aa nsmka, ko1opme c 6onLuo aepon1noc1Lk poc1aannk1
nenpnn1noc1n neoc1opoxnmH nporpaHHnc1aH.
12.1. Bmpeneuue naHn1u
HauneH c paccHo1pennn aneune npoc1oro aonpoca, o1ae1m na ko1opm aecLHa pasnn1cn, a
nHenno: ckonLko naHn1n napo ampenn1L nepeHenno, ko1opan onncana kak npnnapnexauan
konkpe1noHy knaccy? CkonLko naHn1n ponxno 6m1L ampeneno nepeHenno win, ko1opan
onncana kak akseHnnnp knacca Window?
BceHn npnsnano, u1o pasHeuenne nepeHennmx a c1eke npn amsoae npouepypm 6onee
aqqek1nano, ueH ncnonLsoaanne kyun (cH., opnako, [Appel 1987]).
Coo1ae1c1aenno paspa6o1unkn nsmkoa nporpaHHnpoaannn npnnarak1 HakcnHyH ycnnn,
u1o6m cpena1L aosHoxnmH pasHeuenne nepeHennmx a c1eke. Ho npn ampenennn naHn1n
uepes c1ek nHee1cn npo6neHa ee pasHep ponxen onpepenn1Lcn c1a1nueckn, 1o ec1L ao
apeHn koHnnnnunn nnn no kpane Hepe ao apeHn axopa a npouepypy. D1n pec1ann
ocyuec1annk1cn po 1oro, kak c1anoan1cn nsaec1no snauenne, ko1opoe 6ype1 copepxa1Lcn a
nepeHenno.
Tpypnoc1L coc1on1 a 1oH, u1o nopknaccm Hory1 po6aann1L pannme, ne npncy1c1aykune a
napknacce. HanpnHep, knacc TextWindow, aepon1no, npnanocn1 c co6o o6nac1n pannmx
pnn 6yqepa 1ekc1a, nonoxennn 1ekyue 1oukn pepak1npoaannn n 1. p. Cnepykuee
onncanne Hoxe1 6m1L 1nnnunmH npnHepoH:
class Window
{
int height;
int width;
...
public
virtual void oops();
};
class TextWindow : public Window
{
char *contents;
int cursorLocation;
...
converted to PDF by BoJIoc
public:
virtual void oops();
};
Window win; // o+nsnena nepemennan xnacca Window
Cnepye1 nn npnnnHa1L ao annHanne pononnn1enLnme snauennn pannmx (nonn contents n
cursorLocation) npn pasHeuennn nepeHenno win? HHee1cn no kpane Hepe 1pn
npaaponopo6nmx cnoco6a pec1an:
1. Bmpenn1L naHn1L, poc1a1ounyk 1onLko pnn 6asoaoro knacca. To ec1L pasHec1n1L pnn
nepeHenno win ncknkun1enLno pannme, onncannme kak uac1L knacca Window,
nrnopnpyn 1pe6oaannn naHn1n pnn nopknacca.
2. PasHec1n1L HakcnHyH naHn1n, poc1a1ouno pnn nk6oro sakonnoro snauennn,
nesaancnHo o1 1oro, npnnapnexn1 nn ono 6asoaoHy knaccy nnn opnoHy ns
nopknaccoa.
3. PasHec1n1L naHn1L nop ykasa1enL. Bmpenn1L naHn1L, neo6xopnHyk pnn peanLnoro
snauennn, ns kyun ao apeHn amnonnennn nporpaHHm (npn a1oH ykasa1enL
yc1anaannaae1cn napnexaunH o6pasoH).
BosHoxnm ace 1pn peuennn, n paa ns nnx ac1peuak1cn a nsmkax nporpaHHnpoaannn,
ko1opme Hm paccHa1pnaaeH. B nocnepykunx paspenax Hm 6ypeH nccnepoaa1L neko1opme
nocnepc1ann a1nx peuenn.
12.1.1. PasHeeuue HuuuHanuuo c1a1uvecko naHn1u
Hsmk nporpaHHnpoaannn C 6mn paspa6o1an a coo1ae1c1ann c 1pe6oaannnHn HakcnHanLno
aqqek1nanoc1n npn amnonnennn nporpaHHm. TeH caHmH, yun1maan pacnpoc1panennoe
y6expenne, u1o ampenenne naHn1n uepes c1ek npnaopn1 k 6onee 6mc1poHy amnonnennk
nporpaHHm, ueH npn pnnaHnueckoH pasHeuennn, neypnan1enLno, u1o n ero npeeHnnk C++
coxpanne1 konuenunn nepnnaHnuecknx n pnnaHnuecknx (pasHeuaeHmx ao apeHn pa6o1m
nporpaHHm) nepeHennmx.
B nsmke C++ o1cnexnaae1cn, kak nHenno onncmaae1cn nepeHennan n coo1ae1c1aenno
ncnonLsyk1cn nn ykasa1enn pnn poc1yna k ee nonnH pannmx. Hnxe, nanpnHep, nepeHennan
win pasHeuae1cn uepes c1ek. Hpoc1panc1ao pnn nee 6ype1 ampeneno a c1eke npn axope a
npouepypy, rpe onncmaae1cn nepeHennan. HaHn1L ampenne1cn nop pasHep nepeHenno
6asoaoro knacca. HepeHennan tWinPtr, c ppyro c1oponm, copepxn1 1onLko ykasa1enL.
HaHn1L nop snauenne, na ko1opoe ykasmaae1 tWinPtr, 6ype1 ampenn1Lcn pnnaHnueckn npn
amnonnennn onepa1opa new. HockonLky k a1oHy apeHenn pasHep o6Lek1oa 1nna TextWindow
yxe nsaec1en, npn ampenennn ns kyun naHn1n, nyxno pnn o6Lek1a TextWindow,
npo6neH ne aosnnkne1.
Window win;
Window *tWinPtr;
...
tWinPtr = new TextWindow;
1o nponcxopn1, korpa snauenne, na ko1opoe ykasmaae1 nepeHennan tWinPtr,
npncaanaae1cn nepeHenno win? [pyrnHn cnoaaHn, kak amnonnne1cn onepa1op
win = *tWinPtr;
HaHn1L, ampenennan nop nepeHennyk win, aHeuae1 1onLko o6Lek1m 1nna Window, a 1o
apeHn kak snauenne, na ko1opoe ykasmaae1 nepeHennan tWinPtr, 6onLue no pasHepy.
Oueanpno, u1o ne ace snauennn, na ko1opme ykasmaae1 tWinPtr, Hory1 6m1L ckonnpoaanm.
Hoaepenne no yHonuannk coc1on1 a 1oH, u1o konnpyk1cn 1onLko coanapakune nonn (pnc.
12.1). (B nsmke C++ nonLsoaa1enL Hoxe1 nepeonpepenn1L cHmcn onepa1opa npncaanaannn
n o6ecneun1L nk6oe xenaeHoe qynkunonnpoaanne. Tak u1o specL Hm nHeeH a anpy 1onLko
converted to PDF by BoJIoc
c1anpap1noe noaepenne, ko1opoe nHee1 Hec1o npn o1cy1c1ann onpepenneHmx
nonLsoaa1eneH anL1epna1na.)
Oueanpno, u1o neko1opan nnqopHaunn (copepxauancn a pononnn1enLnmx nonnx tWinPtr)
1epne1cn. Heko1opme aa1opm ncnonLsyk1 1epHnn cpeska (slicing) pnn a1oro npouecca,
nockonLky nonn pannmx o6Lek1a cnpaaa cpesak1cn nepep npncaanaanneH neaoHy
o6Lek1y.
HackonLko onacna no1epn nnqopHaunn? TonLko a 1oH cnyuae, ecnn nonLsoaa1enL u1o-1o
sanopospn1. Bonpoc: kak nonLsoaa1enL cHoxe1 saHe1n1L o1cy1c1ane nnunnx none?
CeHan1nka nsmka rapan1npye1, u1o pnn nepeHenno win amsmaak1cn 1onLko He1opm,
onpepenennme pnn knacca Window, no ne He1opm knacca TextWindow.
Me1opm, onpepenennme n peannsoaannme a knacce Window, ne Hory1 nHe1L poc1yna k
nonnH pannmx nopknaccoa. Ho kak nacue1 He1opoa, onpepenennmx a knacce Window n
nepeonpepenneHmx a nopknaccax?
PaccHo1pnH, k npnHepy, pae npouepypm oops, nokasannme amue. Ecnn nonLsoaa1enL
amnonnne1 koHanpy win.oops() n npn a1oH am6npae1cn He1op knacca TextWindow, 1o Hoxe1
nponso1n nonm1ka amaec1n pannme ns nonn win.cursorLocation, ko1oporo ne cyuec1aye1 a
6noke naHn1n nepeHenno win. D1o amsoae1 nn6o napyuenne poc1yna k naHn1n, nn6o (u1o
6onee aepon1no) npnaepe1 k amaopy Hycopa.
void Window::oops()
{
printf("Window oops");
}
void TextWindow::oops()
{
printf("TextWindow oops %d", cursorLocation);
}
Peuenne a1o pnneHHm, am6pannoe paspa6o1unkoH nsmka C++, nsHenn1L npaanna
npnanskn npouepypm k amsoay anp1yanLnoro He1opa. Hoame npnnunnm Hory1 6m1L
cqopHynnpoaanm cnepykunH o6pasoH:
[nn ykasa1ene n ccmnok, korpa coo6uenne amsmaae1 qynkunk-unen, ko1opan
Hoxe1 a npnnunne 6m1L nepeonpepenenno, am6npaeHan qynkunn-unen
onpepenne1cn pnnaHnuecknH snauenneH nonyua1enn.

Puc. 12.1. Hpncaanaanne 6onLuoro snauennn HanenLkoHy
converted to PDF by BoJIoc
[nn ppyrnx nepeHennmx cansmaanne amsoaa anp1yanLno qynkunn onpepenne1cn
c1a1nuecknH (sapannmH npn onncannn), a ne pnnaHnuecknH knaccoH (1o ec1L
qak1nuecknH snauenneH).
Bonee 1ouno, ao apeHn npouecca npncaanaannn snauenne Henne1 1nn c nopknacca na 1nn
pannmx popn1enn. D1o ananornuno 1oHy, kak uenouncnennoe snauenne Hoxe1 6m1L
nsHeneno npn npncaanaannn aeuec1aenno nepeHenno. Hpn 1ako nn1epnpe1aunn Hoxno
rapan1npoaa1L, u1o pnn nepeHennmx, pasHeuaeHmx uepes c1ek, pnnaHnueckn knacc acerpa
coanapae1 co c1a1nuecknH. Hpn co6nkpennn a1oro npaanna npouepypa nnkorpa ne nonyun1
poc1yna k nonnH pannmx, ko1opme qnsnueckn o1cy1c1ayk1 a o6Lek1e. Me1op, am6npaeHm
npn amsoae win.oops(), 6ype1 npnnapnexa1L knaccy Window, n nonLsoaa1enL ne saHe1n1,
u1o uac1L naHn1n 6mna no1epnna npn onepaunn npncaanaannn.
TeH ne Henee a1o peuenne nonyueno sa cue1 neko1opo nenocnepoaa1enLnoc1n. B
ampaxennnx c ykasa1ennHn anp1yanLnme He1opm cansmaak1cn 1ak, kak Hm onncmaann a
npepmpyunx rnaaax. Hoa1oHy a1n snauennn 6ypy1 aec1n ce6n nnmH o6pasoH, ueH
ampaxennn, ncnonLsykune nepnnaHnueckne snauennn. PaccHo1pnH cnepykun npnHep:
Window win;
TextWindow *tWinPtr, *tWin;
...
tWinPtr = new TextWindow;
win = * tWinPtr;
tWin = tWinPtr;
...
win.oops();
(*tWin).oops();
Xo1n nonLsoaa1enL, aepon1no, pyHae1, u1o nepeHennan win n snauenne, na ko1opoe
ykasmaae1 ykasa1enL tWin, a1o opno n 1o xe, aaxno noHnn1L, u1o npncaanaanne
nepeHenno win nsHennno 1nn snauennn. Hs-sa a1oro nepaoe o6pauenne k npouepype
oops() 6ype1 amsmaa1L He1op knacca Window, a 1o apeHn kak a1opoe He1op knacca
TextWindow.
12.1.2. PasHeeuue HakcuHanuuo c1a1uvecko
naHn1u
[pyroe peuenne npo6neHm: npn onncannn o6Lek1a napo ampenn1L HakcnHanLnm o6LeH
naHn1n, poc1a1ounm pnn nk6oro snauennn, ko1opoe Hoxe1 copepxa1Lcn a o6Lek1e
(nesaancnHo o1 1oro, o1nocn1cn ono k o6LnanennoHy knaccy nnn k ero nopknaccaH). D1o1
nopxop ananornuen 1oHy, u1o ncnonLsye1cn npn pasHeuennn nepekpmaakunxcn 1nnoa
pannmx a 1papnunonnmx nsmkax nporpaHHnpoaannn (sanncn c aapnan1aHn a nsmke
HackanL, o6Lepnnennn (union) a C). Hpn npncaanaannn 6ype1 neaosHoxno npncaon1L
snauenne, ne yHeuakueecn a naHn1n, o1aepenno nop nepeHennyk a npaao uac1n
onepa1opa npncaoennn. Hoa1oHy pnc. 12.1 6onee ne ak1yanen n npo6neHm, am1ekakune ns
nero, o1nmne ne aosnnkak1.
D1o, no-anpnHoHy, 6mno 6m npeanLnmH peuenneH, ecnn 6m ne opna HanenLkan npo6neHa:
pasHep nk6oro o6Lek1a ne nsaec1en, noka ne ckoHnnnnpoaana acn nporpaHHa uennkoH.
He npoc1o HopynL (unit a nsmke Object Pascal, qan a C++), no acn nporpaHHa ponxna 6m1L
o1ckannpoaana npexpe, ueH Hm cHoxeH onpepenn1L HakcnHanLnm pasHep nopknacca
pannoro knacca. D1o 1pe6oaanne nanne1cn c1onL orpannunaakunH, u1o nn opnn ns
ocnoanmx o6Lek1no-opnen1npoaannmx nsmkoa ne ncnonLsye1 pannm nopxop.
12.1.3. QuuaHuveckoe ampeneuue naHn1u
Tpe1n nopxop aoo6ue ne xpann1 snauenne o6Lek1a a c1eke. Hpn axope a npouepypy a
c1eke ampenne1cn naHn1L pnn ykasa1enn. 3nauennn copepxa1cn a ppyro o6nac1n pannmx
converted to PDF by BoJIoc
(kyue), ko1opan ne noppepxnaae1 npo1okon ampenennn naHn1n FIFO (nepam aouen
nocnepnn yuen), 1nnnunm pnn c1eka. HockonLky ace ykasa1enn nHek1 noc1onnnm n
qnkcnpoaannm pasHep, 1o ne aosnnkae1 npo6neH npn npncaanaannn snauennn nopknacca
nepeHenno, o6Lnanenno kak napknacc.
D1o1 nopxop ncnonLsye1cn a nsmkax Object Pascal, Smalltalk, Java n Objective-C, o ueH
nonLsoaa1enL yxe Hor porapa1Lcn no cxopc1ay o6Lek1oa n ykasa1ene a Object Pascal. Kak
pnn ykasa1ene, 1ak n pnn o6Lek1oa neo6xopnHo amsmaa1L c1anpap1nyk npouepypy new
pnn pasHeuennn naHn1n nepep o6pauenneH k o6Lek1aH. Ananornuno nonLsoaa1enL nano
amsmaae1 npouepypy free pnn ocao6oxpennn naHn1n, ampenenno o6Lek1y.
KpoHe 1pe6oaannn nanoro ampenennn naHn1n, npn 1akoH nopxope nHee1cn eue opna
npo6neHa onepa1op npncaanaannn 1ecno cansan c ceHan1nko ykasa1ene. Hpn
ncnonLsoaannn ykasa1ene npn npncaanaannn nepecmnae1cn ykasa1enL na snauenne, a ne
co6c1aenno snauenne, o6osnauaeHoe ykasa1eneH. PaccHo1pnH npnaepennyk nnxe
nporpaHHy, Hopennpykuyk 6yqep nop opno cnoao, ko1opm yc1anaannaae1cn n
onpaunaae1cn nonLsoaa1eneH:
type
intBuffer = object
value : integer;
end;
var
x, y : intBuffer;
begin
new(x); {coszais yep}
x.value:=5; writeln(x.value);
y:=x; {y ioi +e yep, uio w x}
y.value:=7; writeln(x.value);
end;
3aHe1nH, u1o akseHnnnpaHn a1oro knacca o6Lnanenm pae nepeHennme. Hpn amnonnennn
nporpaHHm nonLsoaa1enL, aepon1no, ypnan1cn, korpa nocnepnn onepa1op naneua1ae1
snauenne 7, a ne 5. Hpnunna: npn npncaanaannn x n y ne npoc1o nonyunnn opno snauenne,
onn c1ann ykasmaa1L na opno snauenne. D1a cn1yaunn nokasana na pnc. 12.2. CeHan1nka
ykasa1ene pnn o6Lek1oa a nsmke Object Pascal o1uac1n cHyuae1, nockonLky
anL1epna1nanm nopxop ceHan1nka konnpoaannn ncnonLsye1cn pnn acex ppyrnx 1nnoa
pannmx. Ecnn 6m x n y 6mnn c1pyk1ypaHn, 1o npncaanaanne y:=x npnaeno 6m k
konnpoaannk nnqopHaunn ns nepeHenno x a nepeHennyk y. HockonLky npn a1oH
cospak1cn pae pasnnunme konnn, 1o panLneune nsHenennn a nepeHenno y ne annnk1 na
x.

12.2. Hpncaanaanne
Kak a C++, 1ak n a Object Pascal ncnonLsyeHme HexannsHm ampenennn naHn1n annnk1 na
cHmcn onepaunn npncaanaannn. Hoa1oHy specL Hm onpepennH 1ounme snauennn a1oro
onepa1opa a paccHa1pnaaeHmx nsmkax. Kak 6mno o1Heueno a npepmpyueH paspene,
nHek1cn pae nn1epnpe1aunn onepaunn npncaanaannn.
converted to PDF by BoJIoc
CeHau1uka konupoaauun. B onepaunn npncaanaannn nonnoc1Lk konnpye1cn snauenne
cnpaaa, sa1eH ono npncaanaae1cn neao uac1n. Cnepoaa1enLno, paa snauennn nannk1cn
nesaancnHmHn, n nsHenenne opnoro ns nnx ne annne1 na ppyroe.
CeHau1uka ykasa1ene. Onepaunn npncaanaannn nsHenne1 c1onuyk cneaa ccmnky 1ak,
u1o ona ykasmaae1 na 1o xe, u1o n npaaan uac1L. (D1o1 nopxop nnorpa nasmaae1cn
npncaanaanneH ykasa1ene.) TeH caHmH pae nepeHennme ne 1onLko nHek1 opno snauenne,
no n ykasmaak1 na opnn n 1o1 xe o6Lek1. HsHenennn a a1oH o6Lek1e o1pasn1cn na
snauennn, nonyuaeHoH pasmHenoaanneH nk6oro ns payx ykasa1ene.
B neko1opmx nsmkax nporpaHHnpoaannn na6nkpae1cn koHnpoHncc Hexpy ceHan1nko
konnpoaannn n ceHan1nko ykasa1ene, xo1n a1o ne o1nocn1cn k nsmkaH, ko1opme Hm
paccHa1pnaaeH a a1o knnre. Hpen coc1on1 a ncnonLsoaannn ceHan1nkn ykasa1ene npn
npncaanaannn c nocnepykunH npeo6pasoaanneH snauennn a noayk c1pyk1ypy, ecnn ono
Hopnqnunpye1cn. Hpn 1akoH nopxope onepaunn npncaanaannn amnonnne1cn ouenL
aqqek1nano n snauenne ne Hoxe1 6m1L nsHeneno a pesynL1a1e rpy6o oun6kn npn
aaepennn nepeHenno-cnnonnHa. D1o1 He1op uac1o nasmaae1cn konnpoaanneH npn sanncn.
B o6ueH cnyuae, ecnn ncnonLsye1cn ceHan1nka ykasa1ene, 1o nsmkn nporpaHHnpoaannn
npepycHa1pnaak1 neko1opme cpepc1aa cospannn nc1nnno konnn. Onn1L xe, ceHan1nka
ykasa1ene, aoo6ue roaopn, ncnonLsye1cn uaue npn pasHeuennn o6Lek1oa uepes kyuy
(1o ec1L pnnaHnueckn), a ne uepes c1ek (aa1oHa1nueckn). Korpa npnHenne1cn ceHan1nka
ykasa1ene, snauennn poaonLno uac1o nepexnaak1 cao kon1ekc1, any1pn ko1oporo onn
6mnn cospanm.
O1nnune o6Lek1no-opnen1npoaannmx nsmkoa coc1on1 a ncnonLsoaannn pasnmx ceHan1nk
(nepao, a1opo nnn nx koH6nnaunn).
12.2.1. Hpucaauaauue a C++
Anropn1H, no yHonuannk ncnonLsyeHm a nsmke C++ pnn npncaanaannn nepeHenno
snauennn kakoro-nn6o knacca, coc1on1 a pekypcnanoH konnpoaannn coo1ae1c1aykunx
none pannmx. Opnako paspeuae1cn nepenasnaun1L onepa1op npncaanaannn c 1eH, u1o6m
nonyun1L nk6oe xenaeHoe pec1ane. D1a 1exnnka nanne1cn nac1onLko c1anpap1no, u1o
neko1opme koHnnnn1opm C++ ampak1 npepynpexpakuee coo6uenne, ecnn ncnonLsye1cn
npaanno npncaanaannn no yHonuannk.
Hpn nepeonpepenennn npncaanaannn ero nn1epnpe1aunn coc1on1 a cnepykueH. Onepa1op
npncaanaannn a1o He1op, onpepenennm pnn knacca a neao uac1n, amsmaaeHm c
apryHen1oH, c1onunH a npaao uac1n. PesynL1a1 Hoxe1 6m1L paaen void, ecnn anoxennoe
npncaanaanne neponyc1nHo, xo1n, kak npaanno, a kauec1ae pesynL1a1a nepepae1cn ccmnka
na o6Lek1 a neao uac1n. Cnepykun npnHep peHonc1pnpye1 npncaanaanne c1pok (specL
ono nepeonpepenne1cn c 1eH, u1o6m pae konnn opno c1pokn nHenn o6une cnHaonm):
String & String::operator = (String& right)
{
len = right.len; // xonwposais znwny cipoxw
buffer = right.buffer; // xonwposais yxasaiens na cipoxy
return (*this);
}
Tnnnunm nc1ounnk oun6ok naunnakuero nporpaHHnc1a na nsmke C++ a1o
ncnonLsoaanne opnoro n 1oro xe cnHaona paaenc1aa (=) pnn onepaunn npncaanaannn n pnn
onepaunn nnnunannsaunn. B c1anpap1noH C npncaanaanne npn o6Lnanennn a1o npoc1o
ypo6noe cnn1akcnueckoe cokpauenne. Tak u1o aqqek1 o1
int limit = 300;
iaxon +e, uio w oi
int limit;
limit = 300;
converted to PDF by BoJIoc
B nsmke C++ npncaanaanne npn o6Lnanennn Hoxe1 amsmaa1L nponsaonLnme konc1pyk1opm
n ne ncnonLsoaa1L npncaanaanne aoo6ue. TeH caHmH onepa1op 1nna
Complex x = 4;
wniepnpeiwpyeicn no cm-cny xax
Complex x(4);
Hpn nnnunannsaunn uac1o ncnonLsyk1cn ccmnkn; 1eH caHmH cn1yaunn nanoHnnae1
ceHan1nky ykasa1ene. HanpnHep, ecnn npen1nqnka1op s a1o o6Lek1 1nna String, 1o
cnepykuan koHanpa penae1 npen1nqnka1op t cnnonnHoH npen1nqnka1opa s (1ak u1o
nsHenenne a opno nepeHenno npnaopn1 k nsHenennk a ppyro).
... // wcnonssosanwe nepemennon s
String &t = s;
... // nepemenn-e t w s ieneps cc-naxicn na ozno snauenwe
HepeHennme-ccmnkn nan6onee uac1o npnHennk1cn pnn peannsaunn nepepaun napaHe1poa
no ccmnke npn amsoae npouepypm. D1o Hoxe1 paccHa1pnaa1Lcn kak pasnoanpnoc1L
npncaanaannn ykasa1ene, rpe napaHe1py npncaanaae1cn snauenne apryHen1a. Koneuno xe,
ceHan1nka ykasa1ene a C++ Hoxe1 6m1L ocyuec1anena uepes nepeHennme-ykasa1enn.
Hepecmnka napaHe1poa Hoxe1 npnaopn1L k npncaanaannk (npn npncaanaannn apryHen1aH
snauenn napaHe1poa). Hoa1oHy neypnan1enLno, u1o specL aosnnkak1 1e xe nanennn, u1o n
npn npncaanaannn. HanpnHep, paccHo1pnH onncannn anpa
class Base
{
public:
virtual void see();
};
class Derived
{
public:
virtual void see();
};
void f(Base);
void g(Base &);
Derived z;
f(z); g(z);
O6e qynkunn f() n g() ncnonLsyk1 a kauec1ae apryHen1a snauenne knacca Base, no
qynkunn g() onncmaae1 apryHen1 kak nepeHennyk-ccmnky. Ecnn amsmaae1cn qynkunn f() c
apryHen1oH, npnnapnexaunH k knaccy Derived, 1o kak uac1L amsoaa npouepypm apryHen1
npeo6pasye1cn (c ncnonLsoaanneH cpeskn), u1o6m cospa1L snauenne, npnnapnexauee k
knaccy Base. TeH caHmH ecnn any1pn qynkunn f() amsmaae1cn He1op see, 1o 6ype1
ncnonLsoaana anp1yanLnan qynkunn ns knacca Base. C ppyro c1oponm, a1o
npeo6pasoaanne ne nponcxopn1 npn nepepaue napaHe1poa a qynkunk g(). Hoa1oHy ecnn
He1op see amsmaae1cn ns qynkunn g(), 1o 6ype1 ncnonLsoaana anp1yanLnan qynkunn ns
knacca Derived. D1a pasnnua a nn1epnpe1aunn, ko1opan saancn1 1onLko o1 opnoro cnHaona a
saronoake qynkunn, nnorpa nasmaae1cn npo6neHo cpeskn.
Hsmk C++ nosaonne1 nepeonpepenn1L cnHaon npncaanaannn n am6npa1L HexannsH
nepecmnkn napaHe1poa (no ccmnke nnn no snauennk). D1o Hounme cpepc1aa, no onn Hory1
npnaec1n k neoxnpannmH nocnepc1annH. HanpnHep, cnHaon npncaanaannn, ncnonLsyeHm
npn nnnunannsaunn (xo1n a1o 1ouno 1ako xe snak =), ne nopaeprae1cn nsHenennk npn
neperpyske onepa1opa npncaanaannn. Xopouee o6Lncnenne npaannLnoro ncnonLsoaannn
Hounoro no1enunana onepaunn npncaanaannn, sanoxennoro a nsmke C++, pano a pa6o1ax
[Koenig 1989a, Koenig 1989b].
Konnn snauenn uac1o cospak1cn ncnonnnkue cnc1eHo nsmka C++ a kauec1ae
apeHennmx snauenn (npn aosapa1e snauenn qynkun, npn amuncnennn cnoxnmx
ampaxenn n 1. p.) nnn apryHen1oa npn amsoae npouepyp. HonLsoaa1enL Hoxe1 ynpaann1L
converted to PDF by BoJIoc
a1o pen1enLnoc1Lk, onpepennn konnpykun konc1pyk1op. ApryHen1oH konnpykuero
konc1pyk1opa nanne1cn ccmnka na napaHe1p 1oro xe 1nna, u1o n co6c1aenno knacc.
Cun1ae1cn xopoue npak1nko nporpaHHnpoaannn acerpa onpepenn1L konnpykun
konc1pyk1op.
class Complex
{
...
Complex (const Complex &source)
{
// npocio zynwpyei nonn wciounwxa
rl = source.rl;
im = source.im;
}
...
private:
double rl;
double im;
}
12.2.2. Hpucaauaauue a Object Pascal u Java
Kak Object Pascal, 1ak n Java ncnonLsyk1 ceHan1nky ykasa1ene pnn npncaanaannn
o6Lek1oa. B nsmke Object Pascal ne1 npepycHo1pennmx cnc1eHo HexannsHoa pnn cospannn
konnn o6Lek1a, noa1oHy npnnn1o onpepenn1L 6esapryHen1nm He1op copy, ko1opm cospae1
konnk nonyua1enn, ecnn 1akoe qynkunonnpoaanne nanne1cn xena1enLnmH. B nsmke Java
knacc acex o6Lek1oa Object onpepenne1 He1op clone, ko1opm cospae1 no6n1nyk konnk
nonyua1enn coo6uennn clone. Hopknaccm Hory1 nepeonpepenn1L a1o1 He1op. Tnn
aosapauaeHoro pesynL1a1a a a1oH He1ope onpepenen kak Object, 1ak u1o ponxno
ncnonLsoaa1Lcn npnaepenne 1nna pnn nonyuennn snauennn nyxnoro 1nna:
newBall = (Ball) aBall.clone();
3aHe1L1e, u1o a nsmke Object Pascal ceHan1nka ykasa1ene ncnonLsye1cn 1onLko pnn
o6Lek1oa. Bce ppyrne 1nnm pannmx (Haccnam, sanncn) ocyuec1annk1 ceHan1nky
konnpoaannn npn npncaanaannn. D1o uac1o npnaopn1 a cHyuenne naunnakuero
nporpaHHnc1a.
12.2.3. Hpucaauaauue a Smalltalk
Hsmk Smalltalk ncnonLsye1 ceHan1nky ykasa1ene npn npncaanaannn. Knacc Object,
ko1opm nanne1cn napknaccoH acex knaccoa, onpepenne1 paa He1opa konnpoaannn
o6Lek1oa. TeH caHmH konnpykuee npncaanaanne ponxno ncnonLsoaa1L koH6nnaunk
npncaanaannn n nepecmnkn konnpykuero coo6uennn. Onepa1op
x := y copy
cospae1 noam akseHnnnp, a 1ounoc1n noxoxn na o6Lek1 y, a ko1opoH nonn (nepeHennme
akseHnnnpa) ykasmaak1 na o6Lek1m, nannkunecn o6unHn c ananornunmHn nonnHn o6Lek1a
y. Hanpo1na, onepa1op
x := y deepCopy
cospae1 noam o6Lek1, ananornunm y, a ko1opoH nonn (nepeHennme akseHnnnpa)
nnnunannsnpoaanm konnnHn none y.
[pyrnHn cnoaaHn, He1op copy (nasmaaeHm 1akxe shallowCopy) penae1 nepeHennme
akseHnnnpa o6unHn c nepeHennmHn opnrnnana, a 1o apeHn kak He1op deepCopy konnpye1
ppyrne nepeHennme akseHnnnpa. HanpnHep, ecnn y a1o o6Lek1 c 1peHn nepeHennmHn a, b
n c, 1o konnn copy (nnn shallowCopy) akseHnnnpa y amrnnpn1 cnepykunH o6pasoH:
converted to PDF by BoJIoc

C ppyro c1oponm, He1op deepCopy cospae1 noame konnn nepeHennmx akseHnnnpa:

Co6c1aenno nepeHennme akseHnnnpa cospak1cn c ncnonLsoaanneH He1opa copy. Knaccm
nHek1 npaao nepeonpepenn1L nk6o ns He1opoa konnpoaannn copy, shallowCopy n
deepCopy, 1ak u1o pnn akseHnnnpoa neko1opmx knaccoa Hoxno nonyun1L nec1anpap1noe
noaepenne.
12.2.4. Hpucaauaauue a Objective-C
Hsmk Objective-C ncnonLsye1 ceHan1nky ykasa1ene pnn npncaanaannn o6Lek1oa. Konnn
o6Lek1a Hoxe1 6m1L nonyuena opnnH ns 1pex He1opoa: copy, shallowCopy n deepCopy,
ko1opme ananornunm opnonHennmH He1opaH, ncnonLsyeHmH a nsmke Smalltalk.
id x, y, z;
// ... onpezenenwe o+exia y
x = [ y copy ];
z = [ y deepCopy ];
12.3. Hpoaepka ua paaeuc1ao
Hopo6no onepaunn npncaanaannn, aonpoc o 1oH, nanne1cn nn opnn o6Lek1 akanaanen1nmH
ppyroHy o6Lek1y, nanne1cn cnoxnee, ueH a1o Hoxe1 nokasa1Lcn na nepam asrnnp. O1uac1n
1pypnoc1L a nonnHannn 1oro, u1o a 1ounoc1n snaun1 akanaanen1noc1L (npen1nunoc1L),
ananornuna npo6neHe pasroaopnoro nsmka. Ecnn k1o-1o cnpaunaae1: Hanne1cn nn
y1pennnn saespa aeuepne saespo? (Is the morning star the evening star?), o1ae1 c
nonnmH ocnoaanneH Hoxe1 6m1L kak pa, 1ak n ne1. Ecnn aonpoc c1on1 o cpaanennn
qnsnuecknx o6Lek1oa, o6osnauaeHmx a1nHn payHn 1epHnnaHn (a a o6onx cnyuanx peuL npe1
o nnane1e Benepa), 1o o1ae1oH, 6esycnoano, 6ype1 pa. C ppyro c1oponm, ecnn
cnpaunaakun xoue1 ysna1L, o6osnauae1 nn a pannoH nsmke 1epHnn y1pennnn saespa
o6Lek1m, nonannkunecn na aeuepneH ne6e, 1o o1ae1oH c1onL xe 6esycnoano 6ype1 ne1.
Hsyuenne ccmnok, snauenn n akanaanen1noc1n a ec1ec1aennoH nsmke peno 1pypnoe, n
Hm ne 6ypeH yrny6nn1Lcn a a1o1 aonpoc panLue. 3ann1epecoaannm un1a1enL Hoxe1
o6pa1n1Lcn k annsopy c BenmH PmuapeH a knnre Annca a 3asepkanLe nnn k ns6pannmH
pa6o1aH, un1npyeHmH a [Rosenberg 1971] n [Whorf 1956]. K cuac1Lk, paaenc1ao a nsmkax
nporpaHHnpoaannn o6muno xopouo qopHannsoaano, xo1n cpenano a1o no-pasnoHy a pasnmx
nsmkax.
Han6onee o6uee paspenenne o1paxae1 pasnnune Hexpy ceHan1nko ykasa1ene n
ceHan1nko konnpoaannn. Mnorne nsmkn ncnonLsyk1 akanaanen1noc1L ykasa1ene, korpa
pae ccmnkn na o6Lek1 cun1ak1cn akanaanen1nmHn, ecnn onn ykasmaak1 na opnn n 1o1 xe
o6Lek1. Ecnn Hm paccHo1pnH cnoaa y1pennnn saespa kak ykasa1enL na Benepy, 1o npn
converted to PDF by BoJIoc
1ako nn1epnpe1aunn y1pennnn saespa akanaanen1na aeuepne saespe. D1a qopHa
akanaanen1noc1n nnorpa nasmaae1cn akanaanen1noc1Lk o6Lek1oa.
3auac1yk nporpaHHnc1 nn1epecye1cn ne c1onLko 1eH, ykasmaak1 nn pae nepeHennme na
npen1nunm o6Lek1, ckonLko 1eH, o6napak1 nn paa o6Lek1a opnnakoamH snauenneH.
Hocnepnee o6muno 1pe6ye1cn, nanpnHep, npn cpaanennn 1ekc1oamx c1pok (pnc. 12.3). Ho
kak ponxno onpepenn1Lcn paaenc1ao snauenn? [nn uncen n 1ekc1oamx c1pok o6muno nop
paaenc1aoH nonnHae1cn no6n1noe coanapenne. Hpn 1ako nn1epnpe1aunn paa o6Lek1a
nannk1cn akanaanen1nmHn, ecnn nx 6n1oaoe npepc1aanenne a naHn1n opnnakoao.
[nn coc1aanmx o6Lek1oa apope sannce a nsmkax Pascal n C no6n1noe cpaanenne Hoxe1
okasa1Lcn nepoc1a1ounmH. ac1o 6nok naHn1n pnn 1aknx 1nnoa pannmx Hoxe1 aknkua1L
nyc1me yuac1kn, ko1opme ne nHek1 o1nouennn k snauennnH, xpannHmH a o6Lek1e.
HockonLky a1n nponyckn ne ponxnm yun1maa1Lcn npn onpepenennn paaenc1aa,
ncnonLsye1cn a1opo HexannsH, a nHenno noaneHen1noe paaenc1ao. Hpn noaneHen1noH
cpaanennn Hm npoaepneH conoc1aanneHme aneHen1m na coanapenne, npnHennn a1o npaanno
pekypcnano, noka ne ac1pe1n1cn aneHen1, o1nnunm o1 sanncn. B nocnepneH cnyuae
npnHenne1cn no6n1noe cpaanenne. Ecnn ace aneHen1m ypoane1aopnk1 npoaepke, pae
sanncn paccHa1pnaak1cn kak paanme ppyr ppyry. Ecnn kakne-nn6o paa aneHen1a ne
coanapak1, 1o sanncn ne paanm ppyr ppyry. Takoe o1nouenne paaenc1aa nnorpa nasmaae1cn
c1pyk1ypno akanaanen1noc1Lk.
Texnnka o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn npnanocn1 caon oco6ennoc1n a
npoaepky na paaenc1ao. HanpnHep, ecnn npn cpaanennn payx snauenn kak c1a1nuecknx
1nnoa onn okasmaak1cn paanmHn, 1o npn cpaanennn nx a kauec1ae pnnaHnuecknx 1nnoa a1o
ne o6nsa1enLno 1ak. Cnepye1 nn npn npoaepke na paaenc1ao npnnnHa1L a1o ao annHanne?
1o ecnn opnn ns 1nnoa onpepenne1 nonn, ko1opme o1cy1c1ayk1 a ppyroH? Hpo6neHa
coc1on1 1akxe a 1oH, u1o am6op nn1epnpe1aunn pnn amsmaaeHoro coo6uennn onpepenne1cn
nonyua1eneH. He1 rapan1nn, u1o 1akoe qynpaHen1anLnoe caoc1ao, kak koHHy1a1nanoc1L,
6ype1 coxpann1Lcn. Ecnn npen1nqnka1opm x n y npnnapnexa1 k pasnnunmH 1nnaH pannmx,
1o anonne Hoxe1 6m1L, u1o o1nouenne x=y cnpaaepnnao, a o1nouenne y=x ne1!
B opnoH acnek1e, opnako, npo6neHy paaenc1aa nerue peun1L, ueH paso6pa1Lcn c
ananornunmHn 1pypnoc1nHn npn npncaanaannn. Xo1n nocnepnee o6muno paccHa1pnaae1cn
kak uac1L cnn1akcnca n ceHan1nkn nsmka n a cnny a1oro Hoxe1 6m1L nensHenneHmH,
nporpaHHnc1 acerpa aonen cospa1L caon He1opm npoaepkn na coanapenne (aosHoxno, c
neckonLko ppyrnH cnn1akcncoH). TeH caHmH ne1 epnnoro nornueckoro cHmcna o1nouennn
paaenc1aa; ono Hoxe1 osnaua1L pasnme aeun pnn o6Lek1oa pasnmx knaccoa.

Puc. 12.3. Hpen1nunoc1L n paaenc1ao 1ekc1oamx c1pok
Texnnka o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn npnanocn1 caon oco6ennoc1n a
npoaepky na paaenc1ao. HanpnHep, ecnn npn cpaanennn payx snauenn kak c1a1nuecknx
1nnoa onn okasmaak1cn paanmHn, 1o npn cpaanennn nx a kauec1ae pnnaHnuecknx 1nnoa a1o
ne o6nsa1enLno 1ak. Cnepye1 nn npn npoaepke na paaenc1ao npnnnHa1L a1o ao annHanne?
1o ecnn opnn ns 1nnoa onpepenne1 nonn, ko1opme o1cy1c1ayk1 a ppyroH? Hpo6neHa
coc1on1 1akxe a 1oH, u1o am6op nn1epnpe1aunn pnn amsmaaeHoro coo6uennn onpepenne1cn
nonyua1eneH. He1 rapan1nn, u1o 1akoe qynpaHen1anLnoe caoc1ao, kak koHHy1a1nanoc1L,
6ype1 coxpann1Lcn. Ecnn npen1nqnka1opm x n y npnnapnexa1 k pasnnunmH 1nnaH pannmx,
1o anonne Hoxe1 6m1L, u1o o1nouenne x=y cnpaaepnnao, a o1nouenne y=x ne1!
B opnoH acnek1e, opnako, npo6neHy paaenc1aa nerue peun1L, ueH paso6pa1Lcn c
ananornunmHn 1pypnoc1nHn npn npncaanaannn. Xo1n nocnepnee o6muno paccHa1pnaae1cn
converted to PDF by BoJIoc
kak uac1L cnn1akcnca n ceHan1nkn nsmka n a cnny a1oro Hoxe1 6m1L nensHenneHmH,
nporpaHHnc1 acerpa aonen cospa1L caon He1opm npoaepkn na coanapenne (aosHoxno, c
neckonLko ppyrnH cnn1akcncoH). TeH caHmH ne1 epnnoro nornueckoro cHmcna o1nouennn
paaenc1aa; ono Hoxe1 osnaua1L pasnme aeun pnn o6Lek1oa pasnmx knaccoa.
12.3.1. Koaapuaun u kou1paapuaun
Hpn npoaepke na paaenc1ao uac1o nonesno nsHenn1L cnrna1ypy 1nna a He1opax nopknacca.
PaccHo1pnH knacc Shape (qnrypa) n paa ero nopknacca Triangle (1peyronLnnk) n Square
(kaappa1). Kaxe1cn pasyHnmH, u1o6m 1peyronLnnkn Hornn cpaannaa1Lcn 1onLko c
1peyronLnnkaHn, a kaappa1m ncknkun1enLno c kaappa1aHn. Coo1ae1c1aenno
nporpaHHnc1, aosHoxno, nannue1 onpepenenne knacca cnepykunH o6pasoH:
class Shape
{
public:
boolean equals (Shape)
{
return false;
}
...
}
class Triangle : public Shape
{
public:
boolean equals (Triangle);
...
}
class Square : public Shape
{
public:
boolean equals (Square);
...
}
3aHe1L1e, u1o apryHen1 qynkunn, npoaepnkue paaenc1ao, o1nnuae1cn pnn kaxporo ns
knaccoa. [nn popn1enLckoro knacca apryHen1 a1o npoc1o nk6an qnrypa, a 1o apeHn kak
pnn nopknaccoa on orpannuen 6onee cneunannsnpoaannmHn 1nnaHn. Hpn 1akoH
nepeonpepenennn He1opa ncnonLsyk1cn paa nonn1nn:
ApryHen1, ko1opm pnn pouepnero knacca pacunpne1cn po 6onee o6uero 1nna
pannmx, ueH a coo1ae1c1aykueH He1ope popn1enn, nasmaae1cn koaapnan1oH.
ApryHen1, ko1opm pnn pouepnero knacca orpannuen 6onee ysknH 1nnoH pannmx,
ueH a coo1ae1c1aykueH He1ope popn1enn, nasmaae1cn kon1paapnan1oH.
B a1oH npnHepe apryHen1 qynkunn npoaepkn na paaenc1ao nanne1cn kon1paapnan1oH.
Koaapnan1nme n kon1paapnan1nme apryHen1m uac1o okasmaak1cn ec1ec1aennmH peuenneH
sapaun, no ouenL nesnaun1enLnoe uncno nsmkoa nporpaHHnpoaannn noppepxnaae1 nx. Tak,
nn a opnoH ns paccHa1pnaaeHmx naHn nsmkoa onn ne npnHennk1cn. [anee Hm paccHo1pnH
neko1opme npnunnm 1akoro orpannuennn.
Hpexpe acero a1o kacae1cn o6cyxpaauerocn a npepmpyunx rnaaax npnnunna nopc1anoakn.
On npepnncmaae1, u1o Hm HoxeH nopc1aann1L akseHnnnpm pouepnero knacca aespe, rpe
ponyc1nHm akseHnnnpm popn1enLckoro knacca. Ho a1o1 npnnunn noppasyHeaae1, u1o
pouepnn knacc ponxen aocnpnnnHa1L no kpane Hepe ace 1e coo6uennn, ko1opme
ponyc1nHm pnn popn1enLckoro knacca. D1o snaun1, u1o paspeueno koaapnan1noe
nepeonpepenenne He1opoa (nockonLky pouepnn knacc cHoxe1 o6pa6a1maa1L 6onee
converted to PDF by BoJIoc
unpokn na6op apryHen1oa, ueH popn1enLckn), no kon1paapnan1noe nepeonpepenenne
ponxno 6m1L sanpeueno npaannaHn (nockonLky a 1akoH cnyuae pouepnn knacc 6ype1
6onee orpannunaakunH, ueH popn1enLckn). Opnako, kak nokasmaae1 pannm npnHep,
nHenno kon1paapnan1noe nepeonpepenenne 6onee 1nnnuno a peanLnmx sapauax.
(Ananornunoe paccyxpenne npnaopn1 k amaopy, u1o pnn 1nna aosapauaeHoro snauennn
uaue ponxno ac1peua1Lcn koaapnan1noe nepeonpepenenne.)
[nn aoo6paxaeHoro nsmka nporpaHHnpoaannn nonpo6yeH peun1L, kako cHmcn Hoxno
npnpa1L cpaanennk 1peyronLnnka n kaappa1a. HHek1cn paa aapnan1a:
Honck nopxopnuero He1opa ocnoaan ncknkun1enLno na nonyua1ene (1peyronLnnke),
u1o pae1 1peyronLnm He1op, 1pe6ykun 1peyronLnnk a kauec1ae apryHen1a.
Cnepoaa1enLno, ncnonLsoaanne apryHen1a-kaappa1a npnaepe1 k oun6ke koHnnnnunn.
Honck nopxopnuero He1opa ocnoaan na cnrna1ypax 1nna kak nonyua1enn, 1ak n
apryHen1a. HockonLky apryHen1 ne coo1ae1c1aye1 onpepenennk He1opa a knacce Triangle,
1o 6ype1 amsaan He1op knacca Shape.
BonLunnc1ao nporpaHHnc1oa cornacn1cn, u1o a1opan nn1epnpe1aunn kaxe1cn 6onee
ec1ec1aenno (xo1n ona ycnoxnne1 sapauy koHnnnn1opa). D1o, opnako, Hoxe1 npnaec1n k
neko1opo ny1annue. Hpepc1aanH ce6e nonnHopqnyk nepeHennyk 1nna Shape, ko1opan
Hoxe1 copepxa1L kak snauenne 1nna Triangle, 1ak n aennunny 1nna Square. HpepnonoxnH
cnauana, u1o a1a nonnHopqnan nepeHennan ncnonLsye1cn kak apryHen1 a cnepykue
nporpaHHe:
Triangle aTriangle;
Shape aShape;
aShape := aTriangle;
if aTriangle.equals(aTriangle) ... // sosspamaei true
if aTriangle.equals(aShape) ... // sosspamaei false!
Hepam amsoa qynkunn equals a pannoH npnHepe cansmaae1cn c He1opoH knacca Triangle n,
kak n oxnpae1cn, aosapauae1 snauenne true. B1opo onepa1op amsoae1 He1op knacca
Shape, nockonLky apryHen1 a1o ne 1peyronLnnk a nanoH anpe. BosapauaeHm pesynL1a1
paaen false, necHo1pn na 1o u1o qak1nueckoe snauenne apryHen1a paano (a
pec1an1enLnoc1n, npen1nuno) 1oHy xe 1peyronLnnky.
Ananornunan nekonrpyan1noc1L aosnnkae1, ecnn nonnHopqnan nepeHennan ncnonLsye1cn
kak nonyua1enL. HockonLky epnnc1aennm pasyHnm He1op, ko1opm specL Hoxno
amsaa1L, a1o He1op popn1enn, 1o cnepykun 1ec1 neoxnpanno pae1 naH snauenne false:
if aShape.equals(aTriangle) ... // sosspamaei false
HockonLky peannsaunn kak koaapnan1noro, 1ak n kon1paapnan1noro nepeonpepenennn
He1opoa cnoxna, a ceHan1nka 1yHanna, 1o nou1n ace o6Lek1no-opnen1npoaannme nsmkn
sanpeuak1 nsHenenne 1nna apryHen1oa pnn nepeonpepenneHmx He1opoa. Takan nonn1nka
nasmaae1cn 6esaapnan1no (novariance). 1o6m o6o1n ykasannoe orpannuenne,
nporpaHHnc1m uac1o ncnonLsyk1 nanyk npoaepky n npnaepenne 1nna, kak a1o cpenano a
cnepykueH npnHepe:
boolean Triangle.equals(Shape & aShape)
{
Triangle & right = dynamic_cast(aShape);
if (right)
{
... // cpasnwis zsa ipeyronsnwxa
}
else
return false;
}
converted to PDF by BoJIoc
B cna6o qopHe nsmk C++ paspeuae1 kon1paapnan1noe nepeonpepenenne. D1o cnyua,
korpa aosapauaeHoe snauenne noao qynkunn Hoxe1 6m1L nopknaccoH popn1enLckoro
knacca. To ec1L qynkunn-unen knacca Shape onncmaae1cn kak aosapauakuan snauenne
1nna Shape, a 1o apeHn kak nopknacc Triangle nepeonpepenne1 ee n sapae1 aosapauaeHoe
snauenne 1nna Triangle. Takoe ocna6nenne 6esaapnan1noro npaanna yc1panne1
neo6xopnHoc1L Hnorouncnennmx npnaepenn 1nnoa n ne npnaopn1 k oun6kaH, cansannmH c
1nnoH pannmx. HanpnHep, nepeHennme, onncannme c 1nnoH pannmx Shape, 6ypy1 n a caHoH
pene aosapaua1L Shape, paxe ecnn onn nannk1cn nonnHopqnmHn nepeHennmHn co
snauennnHn Triangle (a nocnepneH cnyuae aosapauaeHoe snauenne okaxe1cn nop1nna
Triangle).
K nsmkaH nporpaHHnpoaannn, ko1opme ponyckak1 koaapnaunk n kon1paapnaunk,
o1nocn1cn Eiffel [Rist 1995] n Sather.
12.3.2. Paaeuc1ao a Objective-C, Java u Object Pascal
B nsmkax Objective-C, Java n Object Pascal o6Lek1m acerpa (pnn Objective-C nou1n
acerpa) npepc1aanenm any1pennnH o6pasoH kak ykasa1enn. Heypnan1enLno, u1o cHmcn no
yHonuannk onepa1opa paaenc1aa (= a nsmke Object Pascal, == a nsmkax Objective-C n Java)
a1o npen1nunoc1L, 1o ec1L paaenc1ao ykasa1ene. [ae o6Lek1nme nepeHennme npn
1ec1npoaannn paanm, 1onLko ecnn onn ykasmaak1 na opnn n 1o1 xe o6Lek1.
Xo1n nn a opnoH ns a1nx nsmkoa nenLsn nepeonpepenn1L ac1poennm onepa1op,
c1anpap1no npak1nko nanne1cn aaepenne He1opoa, ko1opme o6ecneunaak1
anL1epna1nanoe nonn1ne paaenc1aa. Cnepykun npnHep nnnkc1pnpye1 npoaepky
paaenc1aa uepes He1op knacca Card. [ae nrpanLnme kap1m paccHa1pnaak1cn kak paanme,
ecnn onn o6napak1 opnnakoamHn Hac1Lk n panroH, paxe ecnn a1o ne npen1nunme kap1m:
function Card.equal(aCard : Card) : boolean;
begin
if (suitValue = aCard.suit) and
(rankValue = aCard.rank)
then equal := true
else equal := false;
end;
Hn opnn ns a1nx nsmkoa ne noppepxnaae1 koaapnan1noe nnn kon1paapnan1noe
nepeonpepenenne, o ueH coo6uae1 koHnnnn1op. B cn1yaunnx, korpa Hoxe1 6m1L nonesnmH
koaapnan1noe nepeonpepenenne (nanpnHep, npn npoaepke na paaenc1ao), ponxna
npoaopn1Lcn nanan npoaepka pnnaHnueckoro 1nna pannmx. B rnaae 10 Hm paccHa1pnaann
HexannsH 1ako npoaepkn.
class Triangle extends Shape
{
boolean equals (Shape aShape)
{
if (aShape instanceOf Triangle)
{
... // cpasnenwe ipeyronsnwxos
}
else return false;
}
}
12.3.3. Paaeuc1ao a Smalltalk
Hsmk Smalltalk pasnnuae1 npen1nunoc1L o6Lek1oa n nx paaenc1ao. Hpen1nunoc1L o6Lek1oa
npoaepne1cn c noHouLk ypaoennoro cnHaona paaenc1aa (==). Paaenc1ao o6Lek1oa
anannsnpye1cn c noHouLk opnokpa1noro cnHaona paaenc1aa (=) n paccHa1pnaae1cn kak
coo6uenne, nepecmnaeHoe neaoHy o6Lek1y. Ho yHonuannk cHmcn a1oro coo6uennn 1o1 xe
converted to PDF by BoJIoc
caHm, u1o n pnn npoaepkn na npen1nunoc1L. Opnako kaxpm knacc Hoxe1 nepeonpepenn1L
a1o1 cnHaon nponsaonLnmH o6pasoH. HanpnHep, knacc Array onpepenne1, u1o paaenc1ao
nHee1 Hec1o 1orpa, korpa o6Lek1 cnpaaa nanne1cn HaccnaoH 1o xe pnnnm, a
coo1ae1c1aykune aneHen1m Haccnaoa paanm.
To, u1o npoaepka paaenc1aa Hoxe1 6m1L nepeonpepenena nponsaonLnmH o6pasoH,
osnauae1: ne1 rapan1nn, u1o paaenc1ao cnHHe1pnuno. Mexpy cpaanenneH x=y n cpaanenneH
y=x ne1 cansn.
HockonLky Smalltalk a1o nsmk c pnnaHnuecknHn 1nnaHn pannmx, 1o nonn1nn
koaapnan1noro n kon1paapnan1noro nepeonpepenennn nHek1 HenLuyk snaunHoc1L pnn
nporpaHHnc1oa. TaH, rpe neo6xopnHo, Hory1 ncnonLsoaa1Lcn nanme npoaepkn qak1nueckoro
1nna snauennn.
12.3.4. Paaeuc1ao a C++
B nsmke C++ ne o6ecneunaae1cn cHmcn no yHonuannk pnn npoaepkn na paaenc1ao.
O1penLnme knaccm Hory1 nepeonpepenn1L onepa1op ==. Te xe caHme npaanna, ko1opme
ncnonLsyk1cn pnn cnn1nn paycHmcnennoc1n nepeonpepenneHmx qynkun, npnHennk1cn n
pnn neperpyxaeHmx onepa1opoa. D1o pae1 nnnksnk koaapnaunn n kon1paapnaunn. B
pec1an1enLnoc1n onn ne paspeuenm. HanpnHep, paccHo1pnH 1akoe onncanne knacca:
class A
{
public:
int i;
A(int x) {i = x;}
int operator== (A& x)
{
return i == x.i;
}
};
class B : public A
{
public:
int j;
B (int x, int y) : A(x) { j = y;}
int operator== (B& x)
{
return (i == x.i;) && (j == x.j);
}
};
Ecnn nepeHennme a n b a1o akseHnnnpm knaccoa A n B, 1o cpaanennn a==a n a==b
ncnonLsyk1 He1op knacca A, a cpaanenne b==b He1op knacca B. Bmpaxenne b==a
amsoae1 coo6uenne o6 oun6ke koHnnnnunn, nockonLky apryHen1 (a) ne coo1ae1c1aye1
onpepenennk onepa1opa paaenc1aa pnn knacca B. (D1o 6mno nepamH n nanHenee
nn1yn1nanmH aapnan1oH am6opa a npepmpyueH o6cyxpennn, korpa Hm nm1anncL npnnnca1L
cHmcn kon1paapnan1noHy He1opy.)
Bonee aaxno 1o, u1o ecnn nonnHopqnan nepeHennan (ko1opan a C++ ponxna 6m1L nn6o
ykasa1eneH, nn6o ccmnko) 1nna A na caHoH pene copepxn1 snauenne 1nna B, 1o onepa1op
cpaanennn ace paano cansmaae1cn c knaccoH A. D1o nponcxopn1 no1oHy, u1o paa
onpepenennn, nokasannme amue, coaepuenno pasnme qynkunn, 1ak u1o nnkakoro
nepeonpepenennn na caHoH pene ne nponcxopn1. D1o y1aepxpenne cnpaaepnnao, paxe ecnn
a nepaoH onpepenennn ncnonLsye1cn knkueaoe cnoao virtual.
12.4. Hpeo6pasoaauue 1unoa
converted to PDF by BoJIoc
[nn nsmkoa nporpaHHnpoaannn co c1a1nuecknHn 1nnaHn pannmx (apope C++ n Object
Pascal) nenLsn npncaanaa1L snauenne 1nna napknacca nepeHenno, o6Lnanenno kak
akseHnnnp nopknacca. 3nauenne, o ko1opoH koHnnnn1opy nsaec1no, u1o ono knacca Window,
ne Hoxe1 6m1L npncaoeno nepeHenno, onncanno c knaccoH TextWindow. Hpnunnm 1akoro
orpannuennn nou1n oueanpnm. Ecnn a1o ne 1ak, 1o onn paccHa1pnaak1cn a ynpaxnennnx 1
n 2 a1o rnaam.
TeH ne Henee a neko1opmx pepknx cnyuanx xena1enLno napyun1L a1o npaanno. aue acero
1akan cn1yaunn aosnnkae1, korpa nporpaHHnc1 pononnn1enLno snae1, u1o snauenne, xo1n n
copepxn1cn a nepeHenno 1nna napknacca, na caHoH pene nanne1cn akseHnnnpoH 6onee
cneunannsnpoaannoro knacca. Torpa Hoxno (xo1n a1o n ne npnae1c1aye1cn) nepexn1pn1L
cnc1eHy npoaepkn 1nnoa pannmx.
B C++ n Objective-C Hm npopenmaaeH a1o1 Haneap c ncnonLsoaanneH konc1pykunn nsmka C,
nasmaaeHo npnaepenneH 1nna pannmx. Hpnaepenne 1nna sac1aanne1 koHnnnn1op
npeo6pasoaa1L snauenne ns opnoro 1nna pannmx a ppyro. Han6onee uac1o a1o1 nopxop
ncnonLsye1cn a cnyuae ykasa1ene, korpa ocyuec1anne1cn 1onLko nornueckan saHena, a ne
qnsnueckoe npeo6pasoaanne.
Mm nponnnkc1pnpyeH npnaepenne 1nna a aapnan1e nrpm a 6nnLnpp, ko1opan
npepnonarae1cn nepenncanno na nsmk C++. BHec1o 1oro u1o6m sac1aann1L kaxpm
akseHnnnp uapa Ball copepxa1L ykasa1enL, ncnonLsyeHm a cansnoH cnncke, Hm onpepennH
o6o6uennm knacc Link cnepykunH o6pasoH:
class Link
{
protected:
Link *link;
public:
Link * next();
void setLink(Link * elem);
};
void Link::setLink(Link *elem)
{
link = elem;
}
Link * Link::next()
{
return link;
}
Knacc Ball cpenaeH nopknaccoH knacca Link. HockonLky He1op setLink nacnepye1cn o1
napknacca, ero ne1 neo6xopnHoc1n noa1opn1L. Opnako nHee1cn npo6neHa c nacnepyeHmH
He1opoH next. On y1aepxpae1, u1o aosapauae1 akseHnnnp knacca Link, a ne Ball. TeH ne
Henee Hm snaeH, u1o o6Lek1 na caHoH pene npnnapnexn1 knaccy Ball, nockonLky a1o
epnnc1aennme o6Lek1m, ko1opme Hm noHeuaeH a cnncok. Hoa1oHy Hm nepenncmaaeH knacc
Ball c 1eH, u1o6m nepeonpepenn1L He1op next, n ncnonLsyeH npnaepenne 1nna pnn 1oro,
u1o6m nsHenn1L 1nn aosapauaeHoro snauennn:
class Ball : public Link
{
...
public:
...
Ball * next();
...
};
Ball * Ball::next()
{
return dynamic_cast(Link::next());
converted to PDF by BoJIoc
}
3aHe1L1e, u1o He1op next ne onncan kak anp1yanLnm: nenLsn nsHenn1L 1nn aosapauaeHoro
snauennn anp1yanLno qynkunn. Baxno noHnn1L, u1o a nsmke C++ a1o npnaepenne 1nna
6ype1 sakonnmH 1onLko pnn ykasa1ene, a ne pnn co6c1aenno o6Lek1oa (cH. ynpaxnenne 2
a1o rnaam). Oynkunn dynamic_cast nanne1cn uac1Lk cnc1eHm RTTI (Run-Time Typing
Information npen1nqnkaunn 1nna ao apeHn amnonnennn), ko1opan 6mna onncana a
rnaae 10. Hpnaepenne 1nnoa uepes RTTI ponxno ncnonLsoaa1Lcn aHec1o 6onee pannnx
cnn1akcnuecknx qopH, nockonLky nenpaannLnoe npnaepenne 1nnoa pannmx nanne1cn
c1anpap1nmH nc1ounnkoH oun6ok a nporpaHHax.
B nsmke Object Pascal sapec1aoaana ananornunan npen. HockonLky o6Lek1m
paccHa1pnaak1cn any1pennnH o6pasoH kak ykasa1enn, 1o a1o npeo6pasoaanne Hoxe1
npnHenn1Lcn k o6Lek1aH nk6oro 1nna, a ne 1onLko k ykasa1ennH. Hsmk Object Pascal
o6ecneunaae1 npoaepky knacca o6Lek1a ao apeHn amnonnennn, 1ak u1o ace coHnn1enLnme
npnaepennn 1nna ponxnm 6m1L amnanenm c noHouLk nano npoaepkn po npncaanaannn.
var
x : TextWindow;
y : Window;
begin
...
if Member(y, TextWindow)
then x := TextWindow(y)
else writeln('illegal window assignment');
...
end;
B nsmke Java nepeHennan, ko1opan copepxn1 snauenne, npnnapnexauee nopknaccy
npnnncannoro e knacca, Hoxe1 6m1L npnaepena k 1nny nopknacca. Opnako 1akne
npeo6pasoaannn npoaepnk1cn ao apeHn amnonnennn nporpaHHm, n ecnn pesynL1a1
nenpaannen, 1o aos6yxpae1cn ncknkun1enLnan cn1yaunn. Ecnn nporpaHHnc1 ee ne xenae1,
1nn snauennn ponxen npoaepn1Lcn c noHouLk onepa1opa instanceOf, kak a1o penae1cn a
cnepykueH npnHepe:
Ball aBall;
WhiteBall wBall;
if (aBall instanceOf WhiteBall)
wBall = (WhiteBall) aBall;
else
...
Ynpaxueuun
1. O6Lncnn1e, noueHy a nsmkax co c1a1nuecknHn 1nnaHn pannmx (apope C++ n Object
Pascal) nenLsn npncaanaa1L snauenne 1nna napknacca nepeHenno, onncanno kak
akseHnnnp nopknacca. To ec1L koHanpm apope nnxecnepykunx npnaopn1 k coo6uennk
koHnnnn1opa o6 oun6ke:
2. TextWindow X;
3. Window Y;
4. ...
X := Y;
5. HpepnonoxnH, u1o a C++ He1op pacnpepenennn naHn1n pa6o1ae1 1ak, kak onncano a
paspene 12.1. O6Lncnn1e, kakne Hory1 aosnnkny1L npo6neHm, ecnn nonLsoaa1enL nm1ae1cn
o6o1n orpannuenne, onncannoe a ynpaxnennn 1, ny1eH npnaepennn 1nnoa pannmx,
ncnonLsyn koHanpy npncaanaannn apope
x = (TextWindow) Y;
converted to PDF by BoJIoc
6. Hpnaepn1e npnHep na Object Pascal nnn C++, ko1opm nnnkc1pnpye1, noueHy pnn
onpepenennn pasHepa o6Lek1a c noHouLk He1opa ns paspena 12.1 ponxna
npocHa1pnaa1Lcn acn nporpaHHa uennkoH, a ne 1onLko o1penLnm qan.
7. O6Lncnn1e, noueHy npn ycnoann co6nkpennn npnnunna nopc1anoakn aosapauaeHoe
snauenne pnn He1opa pouepnero knacca ne Hoxe1 6m1L 6onee o6unH, ueH y popn1enLckoro
knacca.
8. Hokaxn1e, u1o Hoxno onpepenn1L nsmk, ananornunm Object Pascal, ko1opm ne
ncnonLsye1 ceHan1nky ykasa1ene npn npncaanaannn. [pyrnHn cnoaaHn, onnun1e anropn1H
pnn onepaunn npncaanaannn a nsmke nporpaHHnpoaannn, ko1opm peannsye1 ynpaanenne
naHn1Lk, onncannoe a paspene 12.1, no ne npnaopn1 k 1oHy, u1o pae nepeHennme
ykasmaak1 na opno Hec1o a naHn1n nocne onepaunn npncaanaannn. Kak am pyHae1e, noueHy
paspa6o1unkn nsmka Object Pascal ne ncnonLsoaann aau anropn1H npncaanaannn?
Fnaaa 13

Muoxec1aeuuoe uacnepoaauue
Bmue ao apeHn naunx paccyxpenn Hm npepnonarann, u1o knacc nacnepye1 1onLko o1
opnoro popn1enLckoro knacca. Xo1n a1a cn1yaunn nanne1cn, 6esycnoano, 1nnnuno, 1eH ne
Henee 6maak1 cnyuan, korpa neko1opan a6c1pakunn nornueckn am1ekae1 ns payx (nnn
6onee) nesaancnHmx nc1ounnkoa. Ecnn am npepc1aanne1e ce6e knaccm kak ananorn
ka1eropn, kak Hm penann a rnaae 1, n nm1ae1ecL onnca1L ce6n a 1epHnnax rpynn, k
ko1opmH npnnapnexn1e, 1o aecLHa aepon1no, u1o am noc1pon1e Hnoro nenepecekakunxcn
knaccnqnkaun. HanpnHep, n o1eu pe6enka, npoqeccop, rpaxpannn CLA. Hn opna ns
a1nx ka1eropn ne nanne1cn co6c1aennmH nopHnoxec1aoH ppyro.
Eue npnHep. Be1 sannHae1cn xypoxec1aenno nenko. Ee Hm o1nocnH k knaccy Potter. Ee
cocepka Maprape1 pncye1 nop1pe1m, ona PortraitPainter. To1 1nn xnaonncn, ko1opmH ona
sannHae1cn, o1nnuen o1 peHecna Hona: on Hannp (HousePainter). O6muno Hm
paccHa1pnaaeH opnonanpaanennoe nacnepoaanne kak cnoco6 cneunannsaunn (a pannoH
npnHepe Potter a1o uac1nm cnyua xypoxnnka Artist). Opnako Hnoxec1aennoe
nacnepoaanne cnepye1 paccHa1pnaa1L kak koH6nnnpoaanne (nop1pe1nc1 PortraitPainter
a1o 1aopueckn uenoaek Artist n xypoxnnk Painter, kak a1o nokasano na pnc. 13.1).

Puc. 13.1. Hacnepoaanne kak koH6nnnpoaanne
13.1. KoHnnekcume vucna
Mm nponnnkc1pnpyeH 1pypnoc1n, aosnnkakune npn opnnounoH nacnepoaannn, na 6onee
konkpe1noH npnHepe. B nsmke Smalltalk knacc Magnitude onpepenne1 nekn npo1okon pnn
o6Lek1oa c onpepenenno Hepo: onn Hory1 cpaannaa1Lcn ppyr c ppyroH no aennunne
1
.
HanpnHep, o1penLnme cnHaonm (akseHnnnpm knacca Char) cpaannaak1cn no caoe
any1penne kopnpoake (ckaxeH, ASCII). Bonee 1papnunonnm knacc cpaannHmx
o6Lek1oa uncna, 1o ec1L akseHnnnpm knacca Number a 1epHnnax Smalltalk. HoHnHo
cpaanennn, akseHnnnpm knacca Number noppepxnaak1 amnonnenne apnqHe1nuecknx
onepaun (cnoxenne, yHnoxenne n 1. p.). D1n onepaunn ne nHek1 cHmcna pnn o6Lek1oa
converted to PDF by BoJIoc
knacca Char. B Smalltalk nHee1cn neckonLko 1nnoa uncen: uenme (Integer), ppo6nme
(Fraction), aeuec1aennme (Float). ac1L nopoxpaeHo nepapxnn knaccoa nokasana na
pnc. 13.2.

Puc. 13.2. ac1L nepapxnn knaccoa Smalltalk
HpepnonoxnH 1enepL, u1o Hm po6aanneH knacc Complex, ko1opm npepc1aanne1 co6o
a6c1pakunk koHnnekcnoro uncna. ApnqHe1nueckne onepaunn, necoHnenno, onpepenenm
pnn koHnnekcnmx uncen. PasyHno cpena1L Complex nopknaccoH knacca Number, 1ak u1o
apnqHe1nka nacnepye1cn n nepeonpepenne1cn. Hpo6neHa coc1on1 a 1oH, u1o cpaanenne
payx koHnnekcnmx uncen a1o neu1o paycHmcnennoe. KoHnnekcnme uncna npoc1o ne
cpaannHm Hexpy co6o.
H1ak, Hm nHeeH cnepykune orpannuennn:
Knacc Char ponxen 6m1L nopknaccoH knacca Magnitude, no ne nopknaccoH knacca
Number.
Knacc Integer ponxen 6m1L nopknaccoH kak knacca Magnitude, 1ak n knacca Number.
Knacc Complex ponxen 6m1L nopknaccoH knacca Number, no ne nopknaccoH knacca
Magnitude.
HeaosHoxno ypoane1aopn1L aceH a1nH ycnoannH c noHouLk nepapxnn opnnounoro
nacnepoaannn. HHee1cn neckonLko anL1epna1nanmx peuenn panno npo6neHm:

1
Mepa a1o neu1o 6onLuee, ueH npoc1o cnoco6noc1L cpaannaa1Lcn ppyr c ppyroH,
a sauac1yk n npoc1o neu1o o1nnunoe o1 o1nouennn 6onLueHenLue (nanpnHep,
pnn paccHa1pnaaeHmx aa1opoH panee koHnnekcnmx uncen xapak1epnc1nka Hepm
onpepenena, a o1nouenne cpaanennn ne1). HpnHeu. nepea.
1. Cpena1L knacc Complex nopknaccoH knacca Number, ko1opm a caok ouepepL
nanne1cn nopknaccoH knacca Magnitude. 3a1eH nepeonpepenn1L He1opm, nHekune
o1nouenne k cpaanennk akseHnnnpoa a knacce Complex, c 1eH, u1o6m npn nx
amsoae renepnpoaanocL coo6uenne o6 oun6ke. D1o cospanne nopknacca c
orpannuenneH, kak onncano a rnaae 7. Xo1n 1akoe peuenne n ne nanne1cn
aneran1nmH, ono nnorpa nan6onee uenecoo6pasno, ecnn aau nsmk
nporpaHHnpoaannn ne noppepxnaae1 Hnoxec1aennoe nacnepoaanne.
2. He ncnonLsoaa1L nacnepoaanne aoo6ue. Onpepenn1L sanoao kaxpm He1op ao acex
knaccax Char, Integer, Complex n 1. p. D1o peuenne nnorpa nasmaae1cn
pekoHnosnune nepapxnueckoro pepeaa. Ec1ec1aenno, ono yc1panne1 ace
npenHyuec1aa nacnepoaannn, onncannme a rnaae 7, nanpnHep, Hnorokpa1noe
ncnonLsoaanne kopa n rapan1npoaannoc1L nn1epqecoa. [nn nsmkoa
nporpaHHnpoaannn co c1a1nuecknHn 1nnaHn pannmx (1aknx, kak C++ nnn Object
Pascal) a1o1 cnoco6 sanpeuae1 nonnHopqnme o6Lek1m: nanpnHep, nenLsn cospa1L
converted to PDF by BoJIoc
nepeHennyk, ko1opan copepxana 6m nponsaonLnm nsHepnHm o6Lek1 nnn uncno
nponsaonLnoro 1nna.
3. HcnonLsoaa1L uac1L nepapxnn nacnepoaannn n nHn1npoaa1L oc1aauykcn ae1aL.
HanpnHep, Hoxno noHec1n1L ace uncna a knacc Number, a pnn kaxporo nsHepnHoro
o6Lek1a (cnHaona nnn uncna) sapa1L onepaunk cpaanennn.
4. Cpena1L paa knacca Magnitude n Number nesaancnHmHn ppyr o1 ppyra n a cnny
a1oro no1pe6oaa1L, u1o6m knacc Integer nacnepoaan o1 nnx o6onx npn sapannn
caoc1a (pnc. 13.3). Knacc Float 6ype1 ananornunmH o6pasoH nacnepoaa1L kak o1
Number, 1ak n o1 Magnitude.

Puc. 13.3. Hepapxnn Hnoxec1aennoro nacnepoaannn pnn koHnnekcnmx uncen
Baxnm HoHen1 a anL1epna1naax 2 n 3 a1o 1o, u1o onn a ropaspo 6onLue c1enenn
npnaneka1enLnm pnn nsmkoa nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx
(Objective-C, Smalltalk). B nsmkax C++ nnn Object Pascal onpepenenne 1oro, kakne nHenno
1nnm nannk1cn nsHepnHmHn nnn cpaannHmHn, ampaxae1cn a 1epHnnax knaccoa. A
nHenno o6Lek1 nsHepnH, ecnn on Hoxe1 6m1L npncaoen nepeHenno, o6Lnanenno c
knaccoH Magnitude. C ppyro c1oponm, a nsmkax Smalltalk n Objective-C o6Lek1 nanne1cn
nsHepnHmH, ecnn on nonnHae1 coo6uennn, o1nocnunecn k cpaanennk o6Lek1oa,
nesaancnHo o1 1oro, a kakoH Hec1e nepapxnn knaccoa on naxopn1cn. TeH caHmH, u1o6m
sac1aan1L koHnnekcnme uncna asanHopec1aoaa1L c ppyrnHn o6Lek1aHn, paxe ecnn onn ne
nHek1 o6unx knaccoa-npepkoa, Hoxe1 ncnonLsoaa1Lcn 1exnnka paono pncne1uepnsaunn
(cH. pa6o1y [Ingalls 1986] nnn paspen 18.2.3).
O knacce, ko1opm nacnepye1 o1 payx nnn 6onee popn1enLcknx knaccoa, roaopn1, u1o on
nopoxpen Hnoxec1aennmH nacnepoaanneH. Mnoxec1aennoe nacnepoaanne a1o Hounoe n
nonesnoe caoc1ao nsmka nporpaHHnpoaannn, no ono cospae1 Hnoro nsoupennmx n
cnoxnmx npo6neH npn peannsaunn. Hs paccHa1pnaaeHmx naHn nsmkoa 1onLko C++
noppepxnaae1 Hnoxec1aennoe nacnepoaanne, xo1n neko1opme nccnepoaa1enLckne aepcnn
Smalltalk 1akxe o6napak1 a1nH caoc1aoH. B panno rnaae Hm 6ypeH nsyua1L neko1opme ns
npenHyuec1a n npo6neH, cansannmx c Hnoxec1aennmH nacnepoaanneH.
13.2. Bcnnmaamue Heum
B1opo npnHep nponnnkc1pnpye1 Hnorne HoHen1m, ko1opme neo6xopnHo nHe1L a anpy,
korpa am paccHa1pnaae1e Hnoxec1aennoe nacnepoaanne. On apoxnoanen 6n6nno1eko pnn
cospannn rpaqnuecknx nonLsoaa1enLcknx nn1epqecoa, ko1opan cansana c o6Lek1no-
opnen1npoaannmH nsmkoH Eiffel [Meyer 1988a, Meyer 1988b]. B a1o cnc1eHe Henk
onncmaak1cn kak knacc Menu. DkseHnnnpm knacca Menu noppepxnaak1 1akne caoc1aa,
kak uncno nynk1oa Henk, cnncok koHanp n 1. p. Oynkunonnpoaanne, cansannoe c Henk,
noppasyHeaae1 cnoco6noc1L o1o6paxa1L Henk (1o ec1L ce6n) na rpaqnueckoH akpane n
am6npa1L opnn ns ero nynk1oa (pnc. 13.4).
converted to PDF by BoJIoc

Puc. 13.4. CRC-kap1ouka pnn knacca Menu
Kaxpm aneHen1 (nynk1) Henk npepc1aanne1 co6o akseHnnnp knacca MenuItem.
DkseHnnnpm copepxa1 1ekc1 aneHen1a, ccmnky na popn1enLckoe Henk n onncanne koHanpm,
ko1opyk napo amnonnn1L npn am6ope a1oro nynk1a Henk (pnc. 13.5).

Puc. 13.5. CRC-kap1ouka pnn knacca MenuItem
TnnnunmH cpepc1aoH nonLsoaa1enLckoro rpaqnueckoro nn1epqeca nannk1cn
Hnoroypoaneame Henk (a neko1opmx cnc1eHax onn nasmaak1cn kackapnmHn Henk),
ko1opme 1pe6yk1cn, korpa aneHen1 Henk copepxn1 neckonLko anL1epna1nanmx koHanp.
HanpnHep, nynk1 rnaanoro Henk pnn aHynn1opa 1epHnnana Hoxe1 nHe1L nHn sapa1L
onunn. Korpa a1o1 nynk1 (nopHenk) am6pan nonLsoaa1eneH, amcaeunaae1cn a1opoe Henk,
ko1opoe nosaonne1 am6npa1L ns na6opa nHekunxcn onun (1eHnm/cae1nm qon n 1. p.).
Mnoroypoaneaoe Henk onpepenenno npnnapnexn1 knaccy Menu. Ono copepxn1 1y xe
nnqopHaunk, u1o n knacc Menu, n ponxno aec1n ce6n nopo6nmH o6pasoH. C ppyro
c1oponm, ono 1ak xe, necoHnenno, nanne1cn aneHen1oH MenuItem, 1ak kak copepxn1 nHn n
cnoco6no amnonnn1L koHanpy (amaec1n cao o6pas na akpan), korpa coo1ae1c1aykun
nynk1 am6pan a popn1enLckoH Henk. Tpe6yeHoe noaepenne Hoxe1 6m1L poc1nrny1o c
HnnnHanLnmHn ycnnnnHn, ecnn Hm paspeunH knaccy WalkingMenu nacnepoaa1L o1 o6onx
popn1ene. HanpnHep, korpa acnnmaakueHy Henk 1pe6ye1cn amnonnn1L pec1ane no
uenuky Hmun (ynacnepoaannoe o1 knacca MenuItem), ono amaopn1 na akpan caoe
copepxnHoe (amsmaan rpaqnueckn He1op, ynacnepoaannm o1 knacca Menu).
converted to PDF by BoJIoc
Kak n a cnyuae c opnnounmH nacnepoaanneH, npn ncnonLsoaannn Hnoxec1aennoro
nacnepoaannn aaxno nHe1L a anpy ycnoane 6m1L akseHnnnpoH. B naueH npnHepe
Hnoxec1aennoe nacnepoaanne onpaapano, nockonLky nHee1 cHmcn kaxpoe ns y1aepxpenn:
nopHenk ec1L Henk n nopHenk ec1L nynk1 Henk. Korpa o1nouenne 6m1L
akseHnnnpoH ne amnonneno, Hnoxec1aennoe nacnepoaanne Hoxe1 ncnonLsoaa1Lcn
nenpaannLno. HanpnHep, neaepno onncmaa1L aa1oHo6nnL kak nopknacc payx knaccoa:
Ho1op n kopnyc. Touno 1ak xe knacc n6nounm nnpor ne cnepye1 amaopn1L ns
knaccoa nnpor n n6noko. Oueanpno, u1o n6nounm nnpor ec1L nnpor, no on ne ec1L
n6noko.
Korpa Hnoxec1aennoe nacnepoaanne ncnonLsye1cn npaannLnmH o6pasoH, nponcxopn1
1onkoe, no 1eH ne Henee aaxnoe nsHenenne ao asrnnpe na nacnepoaanne. Hn1epnpe1aunn
ycnoann 6m1L akseHnnnpoH npn opnnounoH nacnepoaannn paccHa1pnaae1 nopknacc kak
cneunannsnpoaannyk qopHy ppyro ka1eropnn (popn1enLckoro knacca). Hpn
Hnoxec1aennoH nacnepoaannn knacc nanne1cn koH6nnaune neckonLknx pasnnunmx
xapak1epnc1nk co caonHn nn1epqecaHn, coc1onnnnHn n 6asoamH noaepenneH,
cneunannsnpoaannmH pnn paccHa1pnaaeHoro cnyuan. Onepaunn sanncn n noncka o6Lek1oa
a nekoH xpannnnue (ckaxeH, na xec1koH pncke) npepc1aannk1 1nnnunm npnHep. ac1o
a1n pec1ann peannsoaanm kak uac1L noaepennn, cansannoro c onpepenennmH knaccoH 1nna
Persistence nnn Storable. 1o6m npnpa1L 1akyk cnoco6noc1L nponsaonLnoHy knaccy, Hm
npoc1o po6aanneH Storable k cnncky npepkoa knacca.
Koneuno xe, aaxno pasnnua1L Hexpy nacnepoaanneH o1 nesaancnHmx nc1ounnkoa n
noc1poenneH ns nesaancnHmx koHnonen1, u1o nnnkc1pnpye1cn na npnHepe aa1oHo6nnn n
Ho1opa.
13.3. QaycHmcneuuoc1u uHeu
ac1o aosnnkakuee sa1pypnenne npn Hnoxec1aennoH nacnepoaannn coc1on1 a 1oH, u1o
nHena Hory1 ncnonLsoaa1Lcn pnn o6osnauennn 6onee ueH opno onepaunn. 1o6m
nponnnkc1pnpoaa1L a1o, Hm paccHo1pnH eue pas HopenL kap1ouno nrpm. HpepnonoxnH,
u1o yxe nHee1cn a6c1pakunn kap1ouno konopm CardDeck, ko1opan o6ecneunaae1
napnexauyk qynkunonanLnoc1L: 1acoaanne konopm (He1op shuffle), am6op o1penLno
kap1m (He1op draw
1
) n 1. p., no rpaqnka npn a1oH ne peannsoaana. HpepnonoxnH panee,
u1o ppyroe Hnoxec1ao knaccoa o6ecneunaae1 noppepxky o6o6uennmx rpaqnuecknx
o6Lek1oa. Onn copepxa1 none pannmx (1ouky na nnockoc1n) n, kpoHe 1oro, anp1yanLnm
He1op c nHeneH draw pnn rpaqnueckoro o1o6paxennn caHnx ce6n.
HporpaHHnc1 peuae1 cospa1L knacc noao a6c1pakunn GraphicalDeck, ko1opan nacnepye1
kak o1 knacca CardDeck, 1ak n o1 GraphicalObject. Hcno, u1o konuen1yanLno knacc
GraphicalDeck nanne1cn konopo kap1 CardDeck n 1eH caHmH nornueckn amaopn1cn ns nee.
GraphicalDeck nanne1cn 1akxe rpaqnuecknH o6Lek1oH GraphicalObject. Epnnc1aennan
nenpnn1noc1L a1o paonoe snauenne koHanpm draw.
Kak o1Heuae1 Meep [Meyer 1988a], npo6neHa opnosnauno kpoe1cn a pouepneH knacce, a ne
a popn1ennx. KoHanpa draw nHee1 nepaycHmcnennoe snauenne pnn kaxporo ns
popn1enLcknx knaccoa, korpa onn paccHa1pnaak1cn nsonnpoaanno. Cnoxnoc1L coc1on1 a nx
koH6nnnpoaannn. HockonLky saraospka aosnnkae1 na ypoane pouepnero knacca, 1o n
peuenne ponxno 6m1L napeno specL xe. B pannoH cnyuae pouepnn knacc o6nsan npnnn1L
peuenne, kak yc1pann1L paycHmcnennoc1L neperpyxennoro nHenn.

1
3pecL ncnonLsye1cn 1o1 qak1, u1o a anrnnckoH nsmke rnaron draw kpoHe
snauennn pncoaa1L nHee1 1akxe n Haccy ppyrnx snauenn (a anrno-pycckoH
cnoaape B. K. Mknnepa nx cnncok sannHae1 nou1n pae kononkn 1ekc1a), cpepn
ko1opmx nHek1cn o16pacmaa1L, am1acknaa1L n 1nny1L xpe6n. HpnHeu.
nepea.
converted to PDF by BoJIoc
Peuenne npo6neHm o6muno aknkuae1 koH6nnaunk nepenHenoaannn n nepeonpepenennn.
Hop nepeonpepenenneH Hm nonnHaeH nsHenenne a amnonnneHo onepaunn nnn koHanpe,
kak a1o nponcxopn1 npn Hopnqnkaunn a nopknacce anp1yanLnoro He1opa. Hop
nepenHenoaanneH Hm npoc1o noppasyHeaaeH cHeny nHenn He1opa 6es nsHenennn ero
qynkunonnpoaannn. B cnyuae konopm kap1 GraphicalDeck nporpaHHnc1 Hoxe1 npnnnca1L
He1opy draw sapauy pncoaannn rpaqnueckoro o6pasa, a npouecc am1acknaannn kap1m ns
konopm nepenHenoaa1L a drawCard.
13.3.1. Hacnepoaauue vepes o6ux npepkoa
Bonee cnoxnan npo6neHa aosnnkae1, ecnn nporpaHHnc1 xoue1 ncnonLsoaa1L paa knacca,
nHekune o6uero popn1enn. HpepnonoxnH, u1o nporpaHHnc1 paspa6a1maae1 na6op knaccoa
pnn no1okoa aaopa/amaopa. Ho1ok pannmx a1o o6o6uenne nonn1nn qana. DneHen1m
nepaoro Hory1 6m1L 6onee c1pyk1ypnpoaanm. HanpnHep, uac1o ncnonLsyk1cn no1okn uenmx
uncen nnn no1okn uncen c nnaaakue 1ouko. Knacc InStream o6ecneunaae1 npo1okon pnn
axopnmx no1okoa. HonLsoaa1enL Hoxe1 o1kpm1L axopno no1ok ny1eH npncoepnnennn ero k
qany pannmx, am6pa1L ouepepno aneHen1 ns no1oka n 1. p. Knacc OutStream
o6ecneunaae1 noxoxyk qynkunonanLnoc1L pnn amxopnmx no1okoa. O6a knacca nacnepyk1
o1 o6uero popn1enn c nHeneH Stream. HnqopHaunn, ko1opan ykasmaae1 na co6c1aenno
qan pannmx, npnuyuncn nop Hacko no1oka, copepxn1cn a popn1enLckoH knacce.

Puc. 13.6. Ipaq Hnoxec1aennoro nacnepoaannn
TenepL npepnonoxnH, u1o nonLsoaa1enL xoue1 cospa1L koH6nnnpoaannm no1ok
aaopa/amaopa InOutStream (pnc. 13.6). HHee1 cHmcn o6Lnan1L ero no1oHkoH n no1oka
aaopa, n no1oka amaopa. HepenHenoaanne (cH. npepmpyun paspen) nosaonne1 npnnn1L
peuenne no noaopy nk6o qynkunn, onpepenenno opnoapeHenno a knaccax InStream n
OutStream. Ho u1o pena1L co caoc1aaHn, nacnepyeHmHn o1 o6uero npapopn1enn Stream?
Tpypnoc1L coc1on1 a 1oH, u1o pepeao nacnepoaannn a1o nanpaanennm rpaq, a ne npoc1o
pepeao (cH. pnc. 13.6). Ecnn He1opm a1o epnnc1aennoe, u1o nacnepye1cn o1 o6uero
popn1enLckoro knacca, 1o Hoxe1 6m1L ncnonLsoaana onncannan amue 1exnnka paspeuennn
npo1naopeun. Ho ecnn popn1enLckn knacc onpepenne1 1akxe n nonn pannmx (nanpnHep,
ykasa1enL na qan), 1o nHek1cn paa aapnan1a. Xo1nH nn Hm nHe1L pae konnn none
pannmx nnn 1onLko opny? Ananornunan npo6neHa aosnnkae1, ecnn npapopn1enLckn knacc
ncnonLsye1 konc1pyk1opm nnn nopo6nme nH cpepc1aa nnnunannsaunn, ko1opme ponxnm
amsmaa1Lcn 1onLko opnaxpm. B cnepykueH paspene Hm onnueH, kak c a1o npo6neHo
cnpaanne1cn nsmk C++.
13.4. Muoxec1aeuuoe uacnepoaauue a C++
Mm nponnnkc1pnpyeH ncnonLsoaanne Hnoxec1aennoro nacnepoaannn a C++, pa6o1an nap
ne6onLunH npnHepoH. HpepnonoxnH, u1o pnn npexnero npoek1a nporpaHHnc1 paspa6o1an
na6op knaccoa pnn Hannnynnun co cansnmHn cnnckaHn (nnc1nnr 13.1). A6c1pakunn cnncka
6mna pas6n1a na pae uac1n: knacc Link noppepxnaae1 ykasa1enn na aneHen1m cnncka, a
knacc LinkedList sanoHnnae1 nauano cnncka. Ocnoano cHmcn cansnoro cnncka
po6aanenne noamx aneHen1oa. Cansnme cnnckn npepoc1aannk1 1akxe aosHoxnoc1L
converted to PDF by BoJIoc
amnonnn1L neko1opyk qynkunk nap kaxpmH caonH aneHen1oH. Oynkunn nepepae1cn a
kauec1ae apryHen1a. O6a a1n pec1ann noppepxnaak1cn npouepypaHn a knacce Link.
Ruc1uur 13.1. Knaccm peannsaunn cansnmx cnnckoa
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;
}
void add(Link *n)
{
if (next) next->add(n);
else setLink(n);
}
void onEachDo(void f(Link *))
{
f(this);
if (next) next->onEachDo(f);
}
};


Mm o6pasyeH cneunannsnpoaannme cnnckn uepes onpepenenne nopknaccoa Link. HanpnHep,
knacc IntegerLink a nnc1nnre 13.2 cnyxn1 pnn noppepxkn cnnckoa uenmx uncen. nc1nnr
13.2 copepxn1 1akxe kopo1kyk nporpaHHy, ko1opan nokasmaae1, kak ncnonLsye1cn a1a
a6c1pakunn pannmx.
TenepL npepnonoxnH, u1o pnn noaoro npoek1a 1o1 xe caHm nporpaHHnc1 ponxen
paspa6o1a1L knacc Tree (ppeaoanpnan c1pyk1ypa). Hocne neko1oporo pasHmunennn on
o6napyxnaae1, u1o pepeao Hoxno npepc1aan1L ce6e kak coaokynnoc1L cansnmx cnnckoa. Ha
kaxpoH ypoane pepeaa nonn cansn ykasmaak1 na nopae1an (pepeaLn, npnnapnexaune k
opnoHy ypoank). Opnako kaxpm ysen ykasmaae1 1akxe na cansnm cnncok, ko1opm
converted to PDF by BoJIoc
npepc1aanne1 co6o ero no1oHkoa. Pncynok 13.7 nnnkc1pnpye1 a1y c1pyk1ypy. 3pecL
naknonnme c1penkn o6osnauak1 ykasa1enn na no1oHkoa, a ropnson1anLnme coepnnennn
nopae1ae.
TeH caHmH ysen pepeaa o1nocn1cn n k knaccy LinkedList (nockonLky on copepxn1 ykasa1enL
na cnncok caonx no1oHkoa), n k knaccy Link (nockonLky on copepxn1 ykasa1enL na caok
nopae1aL). B nsmke C++ Hm o6osnauaeH Hnoxec1aennoe nacnepoaanne, npoc1o nepeuncnnn
nHena napknaccoa, paspennn nx sann1mHn (nepeuncnenne cnepye1 nocne paoe1ounn cpasy
sa nHeneH knacca npn ero onncannn). Kak n a cnyuae opnnounoro nacnepoaannn, kaxpoHy
knaccy ponxno npepuec1aoaa1L knkueaoe cnoao (public nnn private), ko1opoe onpepenne1
npaanno anpnHoc1n. B nnc1nnre 13.3 nokasan knacc Tree, ko1opm nacnepye1 o1 knaccoa
Ruc1uur 13.2. Y1ounenne knacca 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));
list.add(new IntegerLink(17));
list.add(new IntegerLink(32));
list.onEachDo(display);
}


Puc. 13.7. [epeao kak coaokynnoc1L cansnmx cnnckoa


converted to PDF by BoJIoc
Ruc1uur 13.3. HpnHep Hnoxec1aennoro nacnepoaannn
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 *))
{
/* cnauana opaoiais noiomxa */
if (elements) elements->onEachDo(f);
/* saiem cen */
f(this);
/* noiom nepeniw x nozseisnm */
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);
}

Link n LinkedList c knkueamH cnoaoH public. Ysnm pepeaa Tree copepxa1 ykasa1enn na
no1oHkoa, a 1akxe uenouncnennme snauennn.
TenepL neo6xopnHo cnpaan1Lcn c npo6neHo neopnosnaunoc1n. Hpexpe acero nHee1cn
paycHmcnennoc1L a nHenn add (po6aan1L), ko1opan o1paxae1 paoc1aennoc1L
npnnncmaaeHoro eHy snauennn. [nn pepeaa ec1L paa cHmcna onepaunn po6aan1L:
npncoepnnn1L ysen-no1oHok n nopopn1L ysen-nopae1aL. Hepaoe o6ecneunaae1cn qynkune
add knacca LinkedList, a1opoe qynkune add knacca Link. Hocne neko1oporo
pasHmunennn nporpaHHnc1 peuae1 oc1aan1L qynkunk add a cHmcne po6aan1L ysen-
no1oHok, no opnoapeHenno aaopn1 pae noame qynkunn, nHena ko1opmx o1paxak1 uenL
onepaunn (po6aan1L nopae1aL n po6aan1L no1oHka).
3aHe1L1e, u1o ace 1pn qynkunn no cyuec1ay npoc1o nepenHenoaannme c1apme. Onn ne
po6aannk1 noaoro qynkunonnpoaannn, a npoc1o nepepak1 ynpaanenne panee
converted to PDF by BoJIoc
onpepenennmH qynkunnH. Heko1opme o6Lek1no-opnen1npoaannme nsmkn (nanpnHep, Eiffel)
nosaonnk1 nonLsoaa1enk aaopn1L nopo6noe nepenHenoaanne 6es cospannn noao qynkunn.
[aycHmcnennoc1L a He1ope onEachDo nanne1cn 6onee cnoxno. 3pecL npaannLnoe pec1ane
coc1on1 a amnonnennn ckaosnoro npoxopa no aceH ysnaH pepeaa. Hpouecc naunnae1cn c
npocHo1pa ysnoa-no1oHkoa, sa1eH aosapauae1cn a ncxopnm ysen, a sa1eH nepexopn1 k
nopae1anH (ko1opme, ec1ec1aenno, ocyuec1annk1 pekypcnanm npoxop yxe no caonH
no1oHkaH). To ec1L pec1ane nanne1cn koH6nnaune He1opoa 6asoamx knaccoa Link n
LinkedList, kak a1o nokasano a nnc1nnre 13.4.
HepenHenoaanne apeHn o1 apeHenn okasmaae1cn neo6xopnHmH ns-sa nepeceuennn nonn1n
nacnepoaannn n napaHe1pnuecko neperpyskn. Korpa a C++ ncnonLsye1cn neperpyxennoe
nHn, 1o cnepaa amsmaae1cn HexannsH nacnepoaannn pnn noncka kon1ekc1a, a ko1opoH
onpepenena qynkunn. 3a1eH 1nnm napaHe1poa anannsnpyk1cn pnn cnn1nn paycHmcnennoc1n
a npepenax pannoro kon1ekc1a. HpepnonoxnH, u1o ec1L paa knacca A n B, pnn kaxporo ns
ko1opmx onpepenen He1op display, no y He1opoa pasnme apryHen1m (nnc1nnr 13.4).
HonLsoaa1enL cun1ae1, u1o 1ak kak a1n paa He1opa pasnnuak1cn no cnncky napaHe1poa,
pouepnn knacc Hoxe1 nacnepoaa1L o1 payx popn1ene n nHe1L poc1yn k o6onH He1opaH. K
coxanennk, specL nacnepoaannn nepoc1a1ouno. Korpa nonLsoaa1enL amsmaae1 He1op
display c uenouncnennmH apryHen1oH, koHnnnn1op ne Hoxe1 npnnn1L peuenne,
ncnonLsoaa1L nn qynkunk ns knacca A (ko1opan coo1ae1c1aye1 1nny apryHen1a) nnn xe ns
knacca B (ko1opan ac1peuae1cn nepao npn sanoxennoH a C++ anropn1He noncka; pnn ee
amsoaa apryHen1 6ype1 npnaepen o1 1nna integer k 1nny double). K cuac1Lk, koHnnnn1op
acerpa npepynpexpae1 o nopo6nmx cnyuanx. Opnako npepynpexpenne ampae1cn a 1ouke
amsoaa He1opa, a ne npn onncannn knacca.
Bmxop a 1oH, u1o6m nepeonpepenn1L o6a He1opa pnn pouepnero knacca C, kak a1o nokasano
a nnc1nnre 13.4. Mm ns6exnH konkypenunn Hexpy nacnepoaanneH n neperpysko a
o6onx cnyuanx nonck konuae1cn a knacce C, rpe pnn koHnnnn1opa yxe ncno, u1o
napaHe1pnueckan neperpyska ncnonLsye1cn naHepenno.
Ruc1uur 13.4. BsanHopec1ane nacnepoaannn n neperpyskn
class A
{
public:
void virtual display(int i)
{
printf("in A %d\n", i);
}
};
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);
converted to PDF by BoJIoc
}
};
main()
{
C c;
c.display(13);
c.display(3.14);
}

B npepmpyueH paspene Hm onncann 1pypnoc1L, ko1opan aosnnkae1, korpa knacc nacnepye1
o1 payx popn1ene, nHekunx o6uero npepka. D1a npo6neHa 6mna nponnnkc1pnpoaana na
npnHepe knaccoa InStream n OutStream, kaxpm ns ko1opmx nacnepoaan o1 o6uero knacca
Stream. Ecnn Hm xo1nH, u1o6m nopoxpaeHm knacc nacnepoaan 1onLko opny konnk none
pannmx, onpepenennmx a knacce Stream, 1o npoHexy1ounme knaccm InStream n OutStream
ponxnm onpepenn1L, u1o nx nacnepoaanne o1 o6uero popn1enLckoro knacca nanne1cn
anp1yanLnmH. Knkueaoe cnoao virtual nokasmaae1, u1o napknacc Hoxe1 nonann1Lcn 6onee
opnoro pasa a nopknaccax, nopoxpaeHmx ns onpepenneHoro knacca, no npn a1oH nyxno
oc1aann1L 1onLko opny ero konnk. nc1nnr 13.5 nokasmaae1 1ako aapnan1 onncannn pnn
a1nx ue1mpex knaccoa.
Ruc1uur 13.5. HpnHep anp1yanLnoro nacnepoaannn
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
{
...
};

Tako nopxop, ncnonLsoaannm a nsmke C++, nenLsn npnsna1L coaepuennmH (kak na a1o
ykasmaae1 Meep), nockonLky konqnnk1 nHen aosnnkae1 na ypoane pouepnnx knaccoa, a
peuenne (knkueaoe cnoao virtual pnn o6uero npepka) sa1parnaae1 popn1enLckne knaccm.
To ec1L anp1yanLnoe npepnasnauenne o6ue uac1n saknapmaae1cn a popn1ennx, a ne a
koH6nnnpoaannoH knacce.
B pepknx cnyuanx xena1enLno cospaaa1L pae konnn nacnepyeHmx none pannmx. HanpnHep,
n rpaqnueckne o6Lek1m, n konopm nrpanLnmx kap1 Hory1 6m1L ocnoaanm na cansnmx
cnnckax, noa1oHy o6a knacca amaopn1cn ns knacca Link. HockonLky a1n paa cnncka
nannk1cn nesaancnHmHn, onn o6a ponxnm copepxa1Lcn a koH6nnnpoaannoH knacce
GraphicalDeck. B 1ako cn1yaunn knkueame cnoaa virtual onyckak1cn xenaeHm
pesynL1a1 6ype1 poc1nrny1. Opnako aaxno rapan1npoaa1L, u1o aosnnkun konqnnk1 nHen
ne amsoae1 oun6ouno nn1epnpe1aunn.
Knkueame cnoaa, onpepennkune anpnHoc1L, nHek1 npaao o1nnua1Lcn a pasnmx
popn1enLcknx knaccax. Cnepoaa1enLno, anp1yanLnm nacnepnnk Hoxe1 6m1L nopoxpen c
converted to PDF by BoJIoc
pasnnunmHn a1pn6y1aHn, nanpnHep public n protected. B 1akoH cnyuae nan6onee xec1kn
ypoaenL saun1m (a naueH npnHepe protected) nrnopnpye1cn n ncnonLsye1cn Henee
orpannun1enLnan ka1eropnn.
Korpa konc1pyk1opm onpepenenm a neckonLknx napknaccax, aaxen nopnpok amnonnennn
popn1enLcknx konc1pyk1opoa n, cnepoaa1enLno, ouepepnoc1L nnnunannsaunn none
pannmx. HonLsoaa1enL Hoxe1 ynpaann1L a1nH, amsmaan nenocpepc1aenno konc1pyk1opm
popn1enLcknx knaccoa any1pn konc1pyk1opa no1oHkoa. HanpnHep, a nnc1nnre 13.6
nonLsoaa1enL nano ykasmaae1, u1o npn nnnunannsaunn knacca C konc1pyk1op knacca B
amsmaae1cn nepamH, 1o ec1L po amsoaa konc1pyk1opa knacca A. Hopnpok amsoaa
konc1pyk1opoa annne1 na nnnunannsaunk.
Hcknkuenne ns a1oro npaanna coc1aannk1 anp1yanLnme 6asoame knaccm. Onn acerpa
nnnunannsnpyk1cn nnuL opnn pas amsoaoH 6esapryHen1noro konc1pyk1opa (ecnn
o6pauenne k neHy ne ocyuec1anne1cn nonLsoaa1eneH, 1ako konc1pyk1op amsmaae1cn
cnc1eHo). D1o nponcxopn1 po kako 6m 1o nn 6mno ppyro nnnunannsaunn. B
nnc1nnre 13.6 nnnunannsaunn npn cospannn noaoro aneHen1a knacca C 6ype1
nponsaopn1Lcn a 1akoH nopnpke: nnnunannsnpye1cn knacc D c noHouLk 6esapryHen1noro
konc1pyk1opa, sa1eH knacc B n nakoneu knacc A. [aa kaxyunxcn amsoaa konc1pyk1opa
knacca D any1pn konc1pyk1opoa knaccoa A n B ne nHek1 nnkakoro aqqek1a, nockonLky
ykasano, u1o popn1enLckn knacc anp1yanen.
Ecnn 1pe6ye1cn, u1o6m pnn konc1pyk1opa 6asoaoro knacca sapaaanncL apryHen1m, knacc C
Hoxe1 sakonnmH o6pasoH sapa1L nyxnme snauennn paxe 1orpa, korpa D ne
Ruc1uur 13.6. Konc1pyk1opm npn Hnoxec1aennoH nacnepoaannn
class D
{
public:
D()
{ ... }
D(int i)
{ ... }
D(double d)
{ ... }
};
class A : virtual D
{
public:
A() : D(7)
{ ... }
};
class B : virtual D
{
public:
B() : D(3.14)
{ ... }
};
class C : public A, public B
{
public:
C() : B(), A()
{ ... }
};

nanne1cn nenocpepc1aennmH npepkoH knacca C. D1o epnnc1aennan cn1yaunn, korpa any1pn
knacca paspeueno ncnonLsoaa1L konc1pyk1op ppyroro knacca, ko1opm ne nanne1cn
converted to PDF by BoJIoc
nenocpepc1aenno npepuec1aykunH popn1eneH. To ec1L konc1pyk1op knacca C Hoxe1 6m1L
sanncan cnepykunH o6pasoH:
C() : D(12), B(), A() { ... }
Konc1pyk1opm pnn anp1yanLnmx 6asoamx knaccoa ponxnm amsmaa1Lcn nepamHn, 1o ec1L po
konc1pyk1opoa neanp1yanLnmx npepkoa.
Bnp1yanLnme He1opm, onpepenennme a anp1yanLnmx 6asoamx knaccoa, 1akxe Hory1 6m1L
nc1ounnkoH npo6neH. HpepnonoxnH, u1o kaxpm ns ue1mpex knaccoa a nnc1nnre 13.5
o6napae1 He1opoH c nHeneH initialize(). On onpepenen kak anp1yanLnm a knacce Stream n
nepeonpepenne1cn a kaxpoH ns nocnepykunx 1pex knaccoa. Me1opm initialize() a knaccax
InStream n OutStream amsmaak1 Stream::initialize() n, kpoHe 1oro, amnonnnk1 neko1opyk
cneunqnueckyk pnn kaxporo ns knaccoa nnnunannsaunk.
TenepL paccHo1pnH He1op initialize() pnn knacca InOutStream. On ne Hoxe1 amsaa1L o6a
ynacnepoaannmx He1opa InStream::initialize() n OutStream::initialize() 6es 1oro, u1o6m ne
amsaa1L paaxpm He1op Stream::initialize(). Hoa1opnoe o6pauenne k He1opy
Stream::initialize(), aepon1no, 6ype1 nHe1L no6ounme aqqek1m. Cnoco6 ns-6exa1L a1o
npo6neHm: nepennca1L Stream::initialize() 1ak, u1o6m on onpepennn, 6mna nn yxe
ocyuec1anena nnnunannsaunn. [pyro aapnan1: nepeonpepenn1L He1opm knaccoa InStream
n OutStream, u1o6m onn ne amsmaann He1op knacca Stream. B nocnepneH cnyuae knacc
InOutStream ponxen a nanoH anpe o6paua1Lcn k npouepype nnnunannsaunn kaxporo ns
1pex knaccoa.
13.5. Muoxec1aeuuoe uacnepoaauue a Java
Hsmk Java ne noppepxnaae1 Hnoxec1aennoe nacnepoaanne knaccoa, no peannsye1
Hnoxec1aennoe nacnepoaanne nn1epqecoa. Knacc Hoxe1 ykasa1L, u1o on noppepxnaae1
neckonLko pasnnunmx nn1epqecoa. HanpnHep, opnn nn1epqec Hoxe1 1pe6oaa1L
sanoHnnannn pannmx na pncke, a ppyro onpepenn1L npo1okon caHoo1o6paxennn
o6Lek1oa. 3anoHnnaeHm rpaqnueckn o6Lek1 6ype1 noppepxnaa1L o6a a1n nn1epqeca:
class graphicalObject implements Storable, Graphical
{
// ...
}
B 1o apeHn kak knaccm ne Hory1 nacnepoaa1L o1 payx n 6onee knaccoa (pacunpnn nx),
nn1epqecaH a1o paspeueno. Mm nHeeH npaao onpepenn1L nn1epqec pnn sanoHnnaeHmx
rpaqnuecknx o6Lek1oa cnepykunH o6pasoH:
interface GraphicalObject extends Storable, Graphical
{
// ...
}
Ru1epa1ypa pnn panuuemero v1euun
Kpn1nka Hnoxec1aennoro nacnepoaannn ac1peuae1cn y Cakknnena [Sakkinen 1988a].
YnoHnny1an pa6o1a nanne1cn cokpauenno apan1npoaanno aepcne ero Ph. D.
pnccep1aunn [Sakkinen 1992]. O6Lncnenne Hnoxec1aennoro nacnepoaannn a nsmke C++
pae1cn Dnnnc [Ellis 1990].
Ynpaxueuun
1. Hpnaepn1e paa npnHepa Hnoxec1aennoro nacnepoaannn a cn1yaunnx, ne cansannmx
c koHnLk1epaHn.
2. B pa6o1e [Wiener 1989] onncan npak1nueckn npnHep Hnoxec1aennoro
nacnepoaannn a C++. Onpepenen knacc IntegerArray, ko1opm nacnepye1 o1 payx
converted to PDF by BoJIoc
knaccoa Array n Integer. Kak am pyHae1e, nanne1cn nn a1o xopounH npnHepoH
Hnoxec1aennoro nacnepoaannn? O6ocny1e cao o1ae1.
3. Mopnqnunpy1e onpepenenne knacca Tree 1ak, u1o6m on Hor 6m1L ncnonLsoaan kak
paonunoe pepeao. O6ecneuL1e cpepc1aa pnn noncka nnn nsHenennn neaoro nnn
npaaoro no1oHka nk6oro ysna. Kakne npepnonoxennn aaH 1pe6yk1cn?
4. O6o6un1e aauy pa6o1y nap ynpaxnenneH 3 1ak, u1o6m cospa1L nonckoaoe paonunoe
pepeao. Ono copepxn1 cnncok uenmx uncen co cnepykunH caoc1aoH: snauenne a
kaxpoH ysne 6onLue, ueH snauennn a neao nopae1an, n HenLue nnn paano
snauennnH a npaao nopae1an.
5. O6cypn1e anp1yanLnoe nacnepoaanne a nsmke C++ c 1oukn spennn npnnunnoa
Hapnaca o Hacknpoake nnqopHaunn.
Fnaaa 14

HonuHopqusH
Cnoao nonnHopqnsH rpeueckoro nponcxoxpennn n osnauae1 npn6nnsn1enLno Hnoro qopH
(poly Hnoro, morphos qopHa). Cnoao morphos nHee1 o1nouenne k rpeueckoHy 6ory cna
Mopqek (Morphus), ko1opm Hor nann1Lcn cnnunH nkpnH a nk6o qopHe, a kako 1onLko
noxenae1, n, cnepoaa1enLno, 6mn aonc1nny nonnHopqnmH. B 6nonornn nonnHopqnme
anpm a1o 1e (nanopo6ne Homo Sapiens), ko1opme xapak1epnsyk1cn nannuneH pasnnunmx
qopH nnn pacuae1ok. B xnHnn nonnHopqnme coepnnennn Hory1 kpnc1annnsoaa1Lcn no
kpane Hepe a payx pasnnunmx qopHax (nanpnHep, yrnepop nHee1 pae kpnc1annnueckne
qopHm rpaqn1a n anHasa).
14.1. HonuHopqusH a nsmkax nporpaHHupoaauun
B nsmkax nporpaHHnpoaannn nonnHopqnm o6Lek1 a1o cyunoc1L (nepeHennan, apryHen1
qynkunn), xpannuan ao apeHn amnonnennn nporpaHHm snauennn pasnnunmx 1nnoa.
HonnHopqnme qynkunn a1o 1e qynkunn, ko1opme nHek1 nonnHopqnme apryHen1m.
14.1.1. HonuHopqume qyukuu a puuaHuveckux
nsmkax
HonnHopqnme qynkunn o1nocn1enLno nerko coc1aann1L a nsmkax nporpaHHnpoaannn c
pnnaHnuecknHn 1nnaHn pannmx (Lisp, Scheme a qynkunonanLno napapnrHe, Smalltalk a
o6Lek1no-opnen1npoaanno napapnrHe). Cnepykun npnHep nnnkc1pnpye1 He1op nsmka
Smalltalk c nHeneH silly, ko1opm a saancnHoc1n o1 apryHen1a x aosapauae1: (x+1), ecnn
x uenoe uncno, o6pa1nyk aennunny, ecnn x ppo6L, 1ekc1 a o6pa1noH nopnpke, ecnn
x 1ekc1oaan c1poka, n cneunanLnoe snauenne nil ao acex oc1anLnmx cnyuanx:
silly: x " rnynenmwn nonwmopn-n meioz "
(x isKindOf: Integer) ifTrue: [ x + 1 ].
(x isKindOf: Fraction) ifTrue: [ x reciprocal ].
(x isKindOf: String) ifTrue: [ x reversed ].
nil
HonnHopqnsH ac1peuae1cn n a nsmkax co c1pornH kon1poneH 1nnoa pannmx. Ero nan6onee
pacnpoc1panennan qopHa a c1anpap1nmx nsmkax nporpaHHnpoaannn a1o neperpyska. Tak,
cnHaon + osnauae1 cnoxenne n uenmx n aeuec1aennmx uncen. Mm 6ypeH paccHa1pnaa1L
a1o1 anp nonnHopqnsHa a cnepykueH noppaspene.
converted to PDF by BoJIoc
Hoame qynkunonanLnme nsmkn nporpaHHnpoaannn (nanpnHep, ML [Milner 1990])
paspeuak1 ncnonLsoaa1L pasnoanpnoc1L nonnHopqnsHa, nasmaaeHyk napaHe1pnuecknH
nonnHopqnsHoH. Hpn a1oH nopxope napaHe1p Hoxe1 6m1L onncan 1onLko uac1nuno
nanpnHep, cnncok ns T, rpe 1nn pannmx T oc1ae1cn neonpepenennmH. D1o nosaonne1
onpepenn1L qynkunn, onepnpykune co cnnckaHn. Takne qynkunn Hory1 amsmaa1Lcn pnn
cnnckoa nponsaonLnoro 1nna. Ananornunme caoc1aa poc1ynnm a neko1opmx o6Lek1no-
opnen1npoaannmx nsmkax uepes o6o6uennme qynkunn nnn ua6nonm.
B o6ueH cnyuae nonnHopqnsH a o6Lek1no-opnen1npoaannmx nsmkax o1paxae1 npnnunn
nopc1anoakn. To ec1L nonnHopqno o6Lek1no-opnen1npoaanno nepeHenno paspeueno
xpann1L a ce6e snauenne, o1nocnueecn k ee o6LnanennoHy 1nny pannmx nnn xe k nk6oHy
ero nop1nny.
14.1.2. A6c1pakuu uuskoro u amcokoro ypoaue
Opnn ns cnoco6oa anannsa nonnHopqnsHa paccHo1pe1L ero c 1oukn spennn a6c1pakun
nnskoro n amcokoro ypoane. A6c1pakunn nnskoro ypoann a1o 6asoaan onepaunn
(nanpnHep, nap c1pyk1ypo pannmx), ko1opan noc1poena noaepx ne6onLuoro konnuec1aa
HexannsHoa. A6c1pakunn amcokoro ypoann o1paxae1 nnannpoaanne 6onee o6uero
xapak1epa (nanpnHep, anropn1H cop1npoakn nnn nsHenenne pasHepa okna), korpa
onpepenne1cn o6un nopxop, 6es konkpe1nsaunn pe1ane.
Anropn1Hm o6muno onncmaak1cn a6c1pakunnHn amcokoro ypoann. Hx qak1nueckan
peannsaunn 1akxe ponxna 6m1L a6c1pakune amcokoro ypoann, ocnoaanno, opnako, na
c1pyk1ypax nnskoro ypoann. HpnaepeH npnHep. Ha amcokoH ypoane pekypcnanm anropn1H,
amuncnnkun pnnny cnncka, sanncmaae1cn cnepykunH o6pasoH:
function length (list) -> integer
begin
if list.link is nil
then
return 1
else
return 1 + length(list.link)
end
Oak1nueckan peannsaunn anropn1Ha na 1papnunonnoH nsmke nporpaHHnpoaannn (1akoH,
kak Pascal) no1pe6ye1 konkpe1no cneunqnkaunn ne 1onLko 1ex pannmx, ko1opme
ncnonLsyk1cn a anropn1He (none link), no n acex caoc1a, ko1opme axopn1 a c1pyk1ypy
pannmx, no ne sapec1aoaanm a anropn1He. TeH caHmH anropn1H ponxen 6m1L nepenncan
na nsmke Pascal cnepykunH o6pasoH:
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;
D1a qynkunn Hoxe1 6m1L ncnonLsoaana pnn amuncnennn pnnnm cansnoro cnncka,
coc1aanennoro ns uenmx uncen, no ona nenpnropna pnn onpepenennn pnnnm cansnoro
cnncka aeuec1aennmx uncen. Bmuncnenne pnnnm nk6oro neuenouncnennoro cnncka
no1pe6ye1 nonnoro nepeonpepenennn c1pyk1ypm pannmx.
converted to PDF by BoJIoc
BonLunnc1ao nporpaHH coc1on1 ns a6c1pakun kak amcokoro, 1ak n nnskoro ypoane.
Paspa6o1unk, snakoHm c nonn1nnHn c1pyk1ypnoro nporpaHHnpoaannn n a6c1pakunnHn
pannmx, cpasy xe onpepenn1 a6c1pakunn nnskoro ypoann n aonno1n1 nx 1ak, u1o6m onn ne
saancenn o1 konkpe1noro npnHenennn. To ec1L a6c1pakunn nnskoro ypoann a1o
nnc1pyHen1m, ko1opme Hory1 nepenocn1Lcn ns opnoro npoek1a a ppyro. C ppyro c1oponm,
a 1papnunonnmx nsmkax nporpaHHnpoaannn a6c1pakunn amcokoro ypoann ponxna
6asnpoaa1Lcn na konkpe1no c1pyk1ype pannmx. Hoa1oHy 1pypno nepenocn1L a6c1pakunn
amcokoro ypoann ns opnoro npoek1a a ppyro. Hepenocn1cn 1onLko o6uan npen. Kak
cnepc1ane, paxe npoc1eune a6c1pakunn amcokoro ypoann (amuncnenne pnnnm cnncka nnn
nonck a 1a6nnue onpepenennoro snauennn) sauac1yk nepenncmaak1cn sanoao pnn kaxporo
noaoro npnnoxennn.
Cnna nonnHopqnsHa coc1on1 a 1oH, u1o on nosaonne1 sanncmaa1L anropn1Hm amcokoro
ypoann nnuL opnaxpm n sa1eH noa1opno nx ncnonLsoaa1L c pasnnunmHn a6c1pakunnHn
nnskoro ypoann. [axe o1nocn1enLno cnoxnme anropn1Hm Hory1 6m1L sanncanm a anpe
cxeHm nnn ua6nona n npnHenn1Lcn ao Hnornx npnnoxennnx. Mm 6ypeH o6cyxpa1L ua6nonm
a nocnepykunx rnaaax, korpa paccHo1pnH Hexannky nonnHopqnsHa.
14.2. Pasuoaupuoc1u nonuHopqusHa
B o6Lek1no-opnen1npoaannmx nsmkax nporpaHHnpoaannn nonnHopqnsH ec1ec1aennoe
cnepc1ane:
o1nouennn 6m1L akseHnnnpoH;
HexannsHa nepecmnkn coo6uenn;
nacnepoaannn;
npnnunna nopc1anoakn.
Opno ns aaxneunx poc1onnc1a o6Lek1no-opnen1npoaannoro nopxopa coc1on1 a
aosHoxnoc1n koH6nnnpoaannn a1nx cpepc1a. B pesynL1a1e nonyuae1cn 6ora1m na6op
1exnnuecknx npneHoa coaHec1noro n Hnorokpa1noro ncnonLsoaannn kopa.
nc1m nonnHopqnsH nHee1 Hec1o, korpa opna n 1a xe qynkunn npnHenne1cn k apryHen1aH
pasnnunmx 1nnoa. B cnyuae unc1oro nonnHopqnsHa ec1L opna qynkunn (1eno kopa) n
neckonLko ee nn1epnpe1aun. [pyran kpanoc1L na6nkpae1cn, korpa nHee1cn Hnoxec1ao
pasnnunmx qynkun (1o ec1L 1en kopa) c opnnH nHeneH. Takan cn1yaunn nasmaae1cn
neperpysko nnn nonnHopqnsHoH ad hoc. Mexpy a1nHn payHn nonkcaHn nexa1
nepeonpepenneHme n o1noxennme He1opm
1
.

1
Onn1L-1akn o1He1nH, u1o cornacne a cpepe coo6uec1aa nporpaHHnc1oa
o1nocn1enLno ncnonLsyeHo 1epHnnonornn aecLHa neaennko. HanpnHep, a pa6o1ax
[Horovitz 1984], [Marcotty 1987], [MacLennan 1987] n [Pinson 1988] 1epHnn
nonnHopqnsH onpepenne1cn 1ak, u1o on npn6nnsn1enLno coo1ae1c1aye1 nonn1nk,
nasmaaeHoHy a panno knnre neperpysko. B pa6o1ax [Sethi 1989] n [Meyer
1988a], a 1akxe a cpepe nkpe, sannHakunxcn qynkunonanLnmH
nporpaHHnpoaanneH [Wikstrom 1987], [Milner 1990], a1o1 1epHnn pesepanpye1cn
pnn o6osnauennn 1oro, u1o specL nasmaae1cn unc1mH nonnHopqnsHoH. [pyrne xe
aa1opm ncnonLsyk1 a1o1 1epHnn pnn o6osnauennn opnoro-payx nnn acex
HexannsHoa nonnHopqnsHa, paccHa1pnaaeHmx a panno rnaae. [aa sakonuennmx,
no sanyrnaakunx ns6m1koH 1exnnuecknx noppo6noc1e anannsa Hory1 6m1L
napenm a pa6o1ax [Cardelli 1985] n [Danforth 1988].

converted to PDF by BoJIoc
14.3. HonuHopqume nepeHeuume
3a ncknkuenneH cnyuaea neperpyskn nonnHopqnsH a o6Lek1no-opnen1npoaannmx nsmkax
nporpaHHnpoaannn aosHoxen 1onLko sa cue1 cyuec1aoaannn nonnHopqnmx nepeHennmx.
HonnHopqnan nepeHennan Hnoronnka: ona copepxn1 snauennn, o1nocnunecn k pasnnunmH
1nnaH pannmx. HonnHopqnme nepeHennme peannsyk1 npnnunn nopc1anoakn. [pyrnHn
cnoaaHn, xo1n pnn 1ako nepeHenno nHee1cn oxnpaeHm 1nn pannmx, qak1nueckn 1nn
Hoxe1 6m1L nop1nnoH oxnpaeHoro 1nna.
B nsmkax c pnnaHnuecknH cansmaanneH (Smalltalk, Objective-C) ace nepeHennme nannk1cn
no1enunanLno nonnHopqnmHn (nk6an nepeHennan Hoxe1 copepxa1L snauenne nk6oro
1nna). B a1nx nsmkax o1 1nna 1pe6ye1cn 1onLko nekn na6op oxnpaeHmx pec1an.
HanpnHep, Haccna a1o 1o, u1o no nnpekcy noc1aanne1 snauenne. HnmHn cnoaaHn,
nonLsoaa1enL Hoxe1 onpepenn1L cao co6c1aennm 1nn pannmx (ckaxeH, paspexennm
Haccna), n ecnn onepaunn nnpekcnpoaannn onpepenenm c 1eHn xe nHenaHn, 1o noam 1nn
pannmx Hoxe1 ncnonLsoaa1Lcn a yxe cyuec1aykueH anropn1He.
B nsmkax co c1a1nuecknHn 1nnaHn pannmx (1aknx, kak C++, Java, Object Pascal n Objective-
C npn ncnonLsoaannn c1a1nuecknx onncann) cn1yaunn neHnoro cnoxnee. Mm o1Heuann, u1o
a1n nsmkn paccHa1pnaak1 cospanne nopknaccoa kak nopoxpenne nop1nnoa pannmx.
HonnHopqnsH cyuec1aye1 a a1nx nsmkax 6naropapn pasnnunk Hexpy peknapnpoaannmH
(c1a1nuecknH) knaccoH nepeHenno n qak1nuecknH (pnnaHnuecknH) knaccoH snauennn,
ko1opoe copepxn1cn a nepeHenno. Kak Hm o1He1nnn a rnaae 10, a1o poc1nrae1cn uepes
o1nouenne 6m1L nopknaccoH. HepeHennan Hoxe1 copepxa1L snauenne o6Lnanennoro
1nna nnn nk6oro ero nop1nna.
B nsmkax Object Pascal n Java a1o cnpaaepnnao pnn acex nepeHennmx, onncannmx c 1nnoH
pannmx object. B C++ n Objective-C c ncnonLsoaanneH c1a1nuecknx onncann nonnHopqnme
nepeHennme cyuec1ayk1 1onLko kak ykasa1enn n ccmnkn. Onn1L xe, kak yxe 6mno
o1Heueno a rnaae 10, korpa ykasa1enn ne ncnonLsyk1cn, pnnaHnueckn knacc nepeHenno
acerpa npnaopn1cn k ee c1a1nueckoHy knaccy.
XopounH npnHepoH nonnHopqno nepeHenno nanne1cn Haccna allPiles a kap1ounoH
nacLnnce ns rnaam 8. Maccna 6mn onncan kak copepxaun snauennn 1nna CardPile, no na
caHoH pene on xpann1 snauennn, npnnapnexaune nopknaccaH popn1enLckoro knacca.
Coo6uenne (nanpnHep, nokasannoe nnxe coo6uenne display), nepepaaaeHoe k aneHen1y
a1oro Haccnaa, amnonnne1 He1op, cansannm c pnnaHnuecknH 1nnoH nepeHenno, a ne co
c1a1nuecknH knaccoH.
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. Heperpyska
Mm roaopnH, u1o nHn qynkunn neperpyxeno, ecnn nHek1cn paa (n 6onee) kopa, cansannme
c a1nH nHeneH. 3aHe1L1e, u1o neperpyska nanne1cn o6nsa1enLno uac1Lk nepeonpepenennn
He1opoa, ko1opoe paccHa1pnaanocL a rnaae 11 (n 6ype1 anannsnpoaa1Lcn cnoaa a
converted to PDF by BoJIoc
cnepykueH paspene), no a1n paa 1epHnna ne npen1nunm, n neperpyska Hoxe1 nponcxopn1L
6es nepeonpepenennn.
Hpn neperpyske nonnHopqnmH nanne1cn nHn qynkunn ono Hnorosnauno. Eue opnn
cnoco6 npepc1aan1L ce6e neperpysky n nonnHopqnsH: aoo6pasn1e, u1o ec1L epnnan
a6c1pak1nan qynkunn, ko1opan amsmaae1cn c apryHen1aHn pasnnunoro 1nna, a qak1nueckn
amnonnneHm kop saancn1 o1 1nna apryHen1oa. To1 qak1, u1o koHnnnn1op uac1o Hoxe1
onpepenn1L npaannLnyk qynkunk na a1ane koHnnnnunn (a nsmkax co c1pornH kon1poneH
1nnoa pannmx) n, cnepoaa1enLno, crenepnpoaa1L 1onLko nyxnm kop a1o npoc1o
on1nHnsaunn.
14.4.1. Heperpyska a peanuuo xusuu
B rnaae 1 Hm ac1pe1nnncL c cn1yaune, korpa neperpyska aosnnkna 6es nepeonpepenennn.
HoHnn1e, n xo1en cpena1L ckpnpns Hoe 6a6yuke n nocna1L e uae1m na penL poxpennn?
Opno aosHoxnoe peuenne coc1onno a 1oH, u1o6m nepepa1L coo6uenne sendFlowersTo
xosnke uae1ounoro Harasnna. Cornacno ppyroHy nnany 1o xe caHoe coo6uenne cnepoaano
nocna1L Hoe xene. Kak xosnka uae1ounoro Harasnna, 1ak n Hon xena ponxnm 6mnn
nonn1L coo6uenne, n o6e c1ann 6m kak-1o pec1aoaa1L, u1o6m nonyun1L xenaeHm
pesynL1a1. B onpepenennoH cHmcne n Hory pyHa1L o coo6uennn sendFlowersTo kak o6
opno qynkunn, nonnHaeHo kak Hoe xeno, 1ak n xosnko uae1ounoro Harasnna.
Opnako onn 6ypy1 ncnonLsoaa1L pasnnunme anropn1Hm a caonx pec1annx.
3aHe1L1e, u1o a pannoH npnHepe ne1 nacnepoaannn (a cnepoaa1enLno, n nepeonpepenennn).
Hepam o6un napknacc pnn xosnkn uae1ounoro Harasnna n Hoe xenm a1o ka1eropnn
Human (uenoaek). Ho pearnpoaa1L na coo6uenne sendFlowersTo caoc1aenno ne aceH
nkpnH. K npnHepy, Ho pan1nc1, ko1opm necoHnenno nanne1cn uenoaekoH, aoo6ue ne
noHe1 pannoe coo6uenne.
14.4.2. Heperpyska u npuaepeuue 1una
B kauec1ae npnHepa 6onee 6nnskoro k nsmkaH nporpaHHnpoaannn, paccHo1pnH paspa6o1ky
6n6nno1ekn knaccoa, npepc1aannkunx c1pyk1ypm pannmx o6uero anpa. Pnp c1pyk1yp
(Hnoxec1aa, na6opm, cnoaapn, ouepepn c yue1oH npnopn1e1oa) Hoxe1 6m1L ncnonLsoaan
pnn xpanennn coaokynnoc1n aneHen1oa. [nn kaxpo c1pyk1ypm 6ype1 onpepenen He1op add,
ko1opm po6aanne1 k ne noam aneHen1.
H1ak, pae coaepuenno pasnnunme qynkunn ncnonLsyk1cn pnn amnonnennn ceHan1nueckn
ananornunmx pec1an nap pasnnunmHn 1nnaHn pannmx. Takan cn1yaunn uac1o ac1peuae1cn
a nporpaHHnpoaannn, n ne 1onLko o6Lek1no-opnen1npoaannoH. Tnnnunm npnHep
neperpyska onepa1opa cnoxennn +. Kop, renepnpyeHm koHnnnn1opoH npn cnoxennn
uenmx uncen, uac1o papnkanLnmH o6pasoH o1nnuae1cn o1 kopa c nnaaakue 1ouko.
HporpaHHnc1, opnako, pyHae1 o6 a1nx onepaunnx kak o epnno cyunoc1n qynkunn
cnoxenne.
B a1oH npnHepe aaxno o1He1n1L, u1o specL nponcxopn1 ne 1onLko neperpyska.
CeHan1nueckn o1penLnan onepaunn npnaepenne 1nna 1akxe o6muno accounnpye1cn c
apnqHe1nuecknHn pec1annHn. Hpnaepenne nponcxopn1, korpa snauenne opnoro 1nna
npeo6pasye1cn a snauenne ppyroro. Ecnn paspeuenm apnqHe1nueckne pec1ann co
cHeuannmHn onepanpaHn, 1o cnoxenne payx snauenn Hoxe1 nn1epnpe1npoaa1Lcn
neckonLknHn cnoco6aHn:
HHek1cn ue1mpe pasnnunme qynkunn, ko1opme coo1ae1c1ayk1 onepaunnH uenoe +
uenoe, uenoe + aeuec1aennoe, aeuec1aennoe + uenoe, aeuec1aennoe +
aeuec1aennoe. B a1oH cnyuae ec1L neperpyska, no ne1 npnaepennn 1nna.
Ec1L pae pasnnunme qynkunn: uenoe + uenoe n aeuec1aennoe +
aeuec1aennoe. [nn onepaun uenoe + aeuec1aennoe n aeuec1aennoe +
converted to PDF by BoJIoc
uenoe uenoe snauenne npnaopn1cn k aeuec1aennoHy. B 1akoH cnyuae na6nkpak1cn
koH6nnaunn neperpyskn n npnaepennn 1nna.
Ec1L 1onLko opna qynkunn cnoxennn: aeuec1aennoe + aeuec1aennoe. Bce
apryHen1m npnaopn1cn k 1nny pannmx aeuec1aennoe uncno. B a1oH cnyuae ne1
neperpyskn, a ec1L 1onLko npnaepenne 1nnoa.
14.4.3. Heperpyska ue noppasyHeaae1 cxopc1ao
B onpepenennn neperpyskn coaepuenno ne noppasyHeaae1cn, u1o qynkunn, cansannme c
neperpyxennmH nHeneH, nHek1 kakoe-nn6o ceHan1nueckoe cxopc1ao. PaccHo1pnH,
nanpnHep, nacLnnc ns rnaam 8. Me1op draw ncnonLsoaancn pnn 1oro, u1o6m napncoaa1L na
akpane kap1y. B ppyroH npnnoxennn He1op draw 6ype1 npnHenn1Lcn k c1onke kap1, 1onLko
na a1o1 pas on 6ype1 pasmrpmaa1L aepxnkk kap1y konopm. D1o1 He1op draw paxe
o1panenno ne noxox c 1oukn spennn ceHan1nkn na He1op draw, onpepenennm pnn opno
kap1m, n 1eH ne Henee onn nHek1 o6uee nHn.
3aHe1L1e, u1o pannan neperpyska opnoro n 1oro xe nHenn nesaancnHmHn n ne nHekunHn
o1nouenne ppyr k ppyry snauennnHn ne o6nsa1enLno nanne1cn nnoxnH c1nneH
nporpaHHnpoaannn. Kak npaanno, a1o ne anocn1 ny1annum. Ha caHoH pene am6op
kopo1koro, ncnoro n snaunHoro nHenn (apope add, draw n 1. p.) snaun1enLno ynyuuae1 n
o6neruae1 ncnonLsoaanne o6Lek1no-opnen1npoaannmx koHnonen1. Hpoue sanoHnn1L, u1o
am po6aanne1e aneHen1 uepes He1op add, a ne acnoHnna1L u1o-nn6ypL apope
addNewElement nnn amsmaa1L npouepypy Set_Module_Addition_Method.
Bce o6Lek1no-opnen1npoaannme nsmkn, ko1opme Hm paccHa1pnaaeH, paspeuak1
ncnonLsoaa1L He1opm c opnnakoamHn nHenaHn a ne cansannmx Hexpy co6ok knaccax. B
a1oH cnyuae npnanska neperpyxennoro nHenn nponsaopn1cn sa cue1 nnqopHaunn o knacce,
k ko1opoHy o1nocn1cn nonyua1enL coo6uennn. TeH ne Henee a1o ne osnauae1, u1o Hory1
6m1L nanncanm qynkunn nnn He1opm, ko1opme npnnnHak1 nponsaonLnme apryHen1m.
Hpnpopa nsmkoa C++ n Object Pascal, ocyuec1annkunx c1porn kon1ponL 1nnoa pannmx,
no-npexneHy 1pe6ye1 onncannn acex nHen.
14.4.4. HapaHe1puveckan neperpyska
[pyro c1nnL neperpyskn, npn ko1opoH npouepypaH (qynkunnH, He1opaH) a opnoH n 1oH xe
kon1ekc1e paspeuae1cn ncnonLsoaa1L coaHec1no opno nHn, a paycHmcnennoc1L cnnHae1cn
sa cue1 anannsa uncna n 1nnoa apryHen1oa, nasmaae1cn napaHe1pnuecko neperpysko. Ona
npncy1c1aye1 a C++ n Java, a 1akxe a neko1opmx pnpek1nanmx nsmkax (nanpnHep, Ada) n
ao Hnornx nsmkax, ocnoaannmx na qynkunonanLno napapnrHe. Mm yxe anpenn npnHepm
1ako neperpyskn pnn qynkun-konc1pyk1opoa. C++ nosaonne1 nk6oHy He1opy, qynkunn,
npouepype nnn onepa1opy 6m1L napaHe1pnueckn neperpyxennmHn, konL ckopo apryHen1m
1akoam, u1o am6op Hoxe1 6m1L nponsaepen opnosnauno na a1ane koHnnnnunn. (Hpn
aa1oHa1nueckoH npnaepennn 1nna nanpnHep, o1 cnHaonoa character k uenmH uncnaH
integer nnn o1 uenmx integer k uncnaH c nnaaakue 1ouko float anropn1H,
ncnonLsyeHm pnn paspeuennn neopnosnaunoc1n a nHenn neperpyxenno qynkunn,
c1anoan1cn ouenL cnoxnmH. Bonee noppo6nan nnqopHaunn Hoxe1 6m1L napena a pa6o1ax
[Ellis 1990] n [Stroustrup 1986].)
Heperpyska npncy1c1aye1 ao acex ppyrnx qopHax nonnHopqnsHa, ko1opme Hm
paccHa1pnaaeH: nepeonpepenenne, o1noxennme He1opm, unc1m nonnHopqnsH. Ona 1akxe
uac1o nonesna npn cyxennn konuen1yanLnoro npoc1panc1aa, 1o ec1L npn yHenLuennn
konnuec1aa nnqopHaunn, ko1opyk neo6xopnHo noHnn1L nporpaHHnc1y. ac1o a1a sa6o1a o
naHn1n nporpaHHnc1a ne Henee aaxna, ueH cnnxenne 1pe6oaann k naHn1n koHnLk1epa,
poc1nraeHoe npn coaHec1no ncnonLsyeHoH kope.

converted to PDF by BoJIoc
14.5. Hepeonpepeneuue
B rnaae 11 Hm onncann Hexannky nepeonpepenennn He1opoa (y1ounenne n saHeuenne) a
pasnnunmx paccHa1pnaaeHmx naHn o6Lek1no-opnen1npoaannmx nsmkax nporpaHHnpoaannn.
Hoa1oHy ne 6ypeH noa1opn1Lcn. BcnoHnnH, opnako, cnepykune cyuec1aennme aneHen1m
a1o 1exnnkn. B opnoH ns knaccoa (o6muno a a6c1pak1noH napknacce) nHee1cn
onpepenennm pnn konkpe1noro coo6uennn o6un He1op, ko1opm nacnepye1cn n
ncnonLsye1cn nopknaccaHn. Opnako no kpane Hepe a opnoH nopknacce onpepenen He1op c
1eH xe nHeneH. D1o nepekpmaae1 poc1yn k o6ueHy He1opy pnn akseHnnnpoa pannoro
nopknacca (nnn xe a cnyuae y1ounennn penae1 poc1yn k He1opy Hnoroc1ynenua1mH). Mm
roaopnH, u1o a1opo He1op nepeonpepenne1 nepam.
Hepeonpepenenne uac1o nponcxopn1 npospauno pnn nonLsoaa1enn knacca, n, kak n a cnyuae
neperpyskn, pae qynkunn npepc1aannk1cn ceHan1nueckn kak opna cyunoc1L.
14.5.1. Hepeonpepeneuue a knacce Magnitude
Hn1epecnmH npnHepoH nepeonpepenennn He1opoa nanne1cn knacc Magnitude a cnc1eHe Little
Smalltalk. Magnitude a1o a6c1pak1nm napknacc, ko1opm nHee1 peno c aennunnaHn,
o6napakunHn no kpane Hepe uac1nunmH (ecnn ne nonnmH) ynopnpounaanneH. ncna
a1o nan6onee xapak1epnm npnHep o6Lek1oa, o6napakunx aennunno, xo1n apeHn n pa1a
1akxe Hory1 6m1L ynopnpouenm, paano kak n cnHaonm, 1oukn na payxHepno kooppnna1no
nnockoc1n, cnoaa a cnoaape n 1. p.
B knacce Magnitude uec1L o1nouenn cpaanennn onpepennk1cn cnepykunH o6pasoH:
<= 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
3aHe1L1e, u1o onpepenennn unknnunm: kaxpoe ns nnx saancn1 o1 neko1opmx ppyrnx. Kak
Hoxno ns6exa1L 6eckoneunoro unkna npn amsoae kakoro-nn6o konkpe1noro cpaanennn?
O1ae1 coc1on1 a 1oH, u1o nopknaccm knacca Magnitude ponxnm nepeonpepenn1L no kpane
Hepe opno ns uec1n coo6uenn cpaanennn. Mm oc1aanneH a kauec1ae ynpaxnennn pnn
un1a1enn npoaepky 1oro, u1o ecnn nepeonpepenenm coo6uennn = n nn6o <:, nn6o >=, 1o
oc1aaunecn onepa1opm ne npnaepy1 k 6eckoneunoHy unkny.
Hepeonpepenenne He1opoa anocn1 cao aknap a coaHec1noe ncnonLsoaanne kopa,
nockonLky akseHnnnpm knaccoa, ko1opme ne nepeonpepennk1 pannm He1op, Hory1
ncnonLsoaa1L opny konnk opnrnnanLnoro kopa. TonLko a 1ex cnyuanx, korpa He1op ne
nopxopn1, cospae1cn anL1epna1nanm qparHen1 kopa. Bes nepeonpepenennn He1opoa 6mno
6m neo6xopnHo pnn acex nopknaccoa cospaaa1L nx co6c1aennme He1opm pnn pearnpoaannn
na coo6uenne, paxe ecnn 6onLunnc1ao He1opoa npen1nuno.
HonLsoaa1enn nsmka C++ ponxnm 6m1L ocaepoHnenm o 1onkoH ceHan1nueckoH pasnnunn
Hexpy nepeonpepenennnHn anp1yanLnoro n neanp1yanLnoro He1opoa. Mm 6ypeH o6cyxpa1L
a1o 6onee noppo6no a paspene 14.9.

converted to PDF by BoJIoc
14.6. O1noxeuume He1opm
O1noxennm He1op (nnorpa nasmaaeHm a6c1pak1nmH He1opoH, a a C++ unc1o
anp1yanLnmH He1opoH) Hoxe1 paccHa1pnaa1Lcn kak o6o6uenne nepeonpepenennn. B o6onx
cnyuanx noaepenne popn1enLckoro knacca nsHenne1cn pnn no1oHka. [nn o1noxennoro
He1opa, opnako, noaepenne npoc1o ne onpepeneno. k6an nonesnan pen1enLnoc1L sapae1cn
a pouepneH knacce.
Opno ns npenHyuec1a o1noxennmx He1opoa nanne1cn unc1o konuen1yanLnmH: nporpaHHnc1
Hoxe1 Hmcnenno napenn1L nyxnmH pec1aneH a6c1pakunk ckonL yropno amcokoro ypoann.
HanpnHep, pnn reoHe1pnuecknx qnryp Hm HoxeH onpepenn1L He1op draw, ko1opm nx
pncye1: 1peyronLnnk Triangle, okpyxnoc1L Circle n kaappa1 Square. Mm onpepennH
ananornunm He1op n pnn popn1enLckoro knacca Shape. Opnako 1ako He1op na caHoH pene
ne Hoxe1 amnonnn1L nonesnyk pa6o1y, nockonLky a knacce Shape npoc1o ne1 poc1a1ouno
nnqopHaunn pnn pncoaannn uero 6m 1o nn 6mno. TeH ne Henee npncy1c1ane He1opa draw
nosaonne1 cansa1L qynkunonanLnoc1L (pncoaanne) 1onLko opnn pas c knaccoH Shape, a ne
aaopn1L 1pn nesaancnHme konuenunn pnn nopknaccoa Square, Triangle n Circle.
HHee1cn n a1opan, 6onee ak1yanLnan npnunna ncnonLsoaannn o1noxennmx He1opoa. B
o6Lek1no-opnen1npoaannmx nsmkax nporpaHHnpoaannn co c1a1nuecknHn 1nnaHn pannmx
(C++, Object Pascal) nporpaHHnc1 nHee1 npaao nocna1L coo6uenne o6Lek1y, 1onLko ecnn
koHnnnn1op Hoxe1 onpepenn1L, u1o pec1an1enLno nHee1cn He1op, ko1opm coo1ae1c1aye1
cenek1opy coo6uennn. HpepnonoxnH, u1o nporpaHHnc1 xoue1 onpepenn1L nonnHopqnyk
nepeHennyk knacca Shape, ko1opan 6ype1 a pasnnunme HoHen1m apeHenn copepxa1L
qnrypm pasnnunoro 1nna. D1o ponyc1nHo a coo1ae1c1ann c npnnunnoH nopc1anoakn. TeH ne
Henee koHnnnn1op paspeun1 ncnonLsoaa1L He1op draw pnn nepeHenno, 1onLko ecnn on
cHoxe1 rapan1npoaa1L, u1o coo6uenne 6ype1 pacnosnaaa1Lcn a knacce nepeHenno.
Hpncoepnnenne He1opa draw k knaccy Shape aqqek1nano o6ecneunaae1 1akyk rapan1nk,
paxe ecnn He1op draw pnn knacca Shape na caHoH pene nnkorpa ne amnonnne1cn.
14.7. Huc1m nonuHopqusH
Mnorne aa1opm pesepanpyk1 nonn1ne nonnHopqnsH (nnn unc1m nonnHopqnsH) pnn
cn1yaun, korpa opna qynkunn ncnonLsye1cn c pasnmHn na6opaHn apryHen1oa, n 1epHnn
neperpyska pnn cnyuan, korpa onpepeneno neckonLko qynkun c opnnH nHeneH1. D1n
1epHnnm ne orpannuenm ncknkun1enLno o6Lek1no-opnen1npoaannmH nopxopoH. HanpnHep,
a nsmkax Lisp n ML nerko nannca1L qynkunn, ko1opme o6pa6a1maak1 cnnckn c pasnnunmHn
aneHen1aHn. Takne qynkunn nannk1cn nonnHopqnmHn, nockonLky 1nn apryHen1a
nensaec1en npn onpepenennn qynkunn. HonnHopqnme qynkunn a1o opna ns nan6onee
Hounmx o6Lek1no-opnen1npoaannmx 1exnnk nporpaHHnpoaannn. Onn nosaonnk1 epnnoxpm
nnca1L kop na amcokoH ypoane a6c1parnpoaannn n sa1eH npnHenn1L ero a konkpe1no
cn1yaunn. O6muno nporpaHHnc1 amnonnne1 nopronky kopa c noHouLk nocmnkn
pononnn1enLnmx coo6uenn nonyua1enk, ncnonLsykueHy He1op. D1n pononnn1enLnme
coo6uennn uac1o ne cansanm c knaccoH na ypoane a6c1pakunn nonnHopqnoro He1opa. Onn
nannk1cn anp1yanLnmHn He1opaHn, ko1opme onpepennk1cn pnn knaccoa 6onee nnskoro
ypoann.
Cnepykun npnHep noHoxe1 nponnnkc1pnpoaa1L a1y konuenunk. Kak Hm o1He1nnn a
paspene 14.5, nocanuennoHy nepeonpepenennk He1opoa, knacc Magnitude a nsmke
Smalltalk a1o a6c1pak1nm napknacc, ko1opm nHee1 peno c ynopnpouennmHn
aennunnaHn. PaccHo1pnH He1op c nHeneH between:and:, npnaepennm nnxe:
between: low and: high
" nposepwis, naxozwicn nw nonyuaiens "
" me+zy zsymn xpannwmw iouxamw "
( low <= self ) and: ( self <= high )
D1o1 He1op onpepenen a knacce Magnitude n npoaepne1 (kak a1o ckasano a koHHen1apnn),
naxopn1cn nn nonyua1enL Hexpy sapannmHn 1oukaHn. Hpoaepka ocyuec1anne1cn nocmnko
converted to PDF by BoJIoc
coo6uennn HenLue nnn paano nnxne rpannue c nonyua1eneH a kauec1ae apryHen1a n
nepepaue 1oro xe coo6uennn nonyua1enk c aepxne rpannue a kauec1ae apryHen1a (a
nsmke Smalltalk ace onepa1opm nn1epnpe1npyk1cn kak coo6uennn). TonLko ecnn o6a a1n
ampaxennn pak1 true, cun1ae1cn, u1o nonyua1enL nonapae1 a sapannm nn1epaan.
Hocne 1oro kak o6Lek1y nocnano coo6uenne between:and: c payHn apryHen1aHn,
qak1nueckoe amnonnenne saancn1 o1 konkpe1noro cHmcna coo6uennn HenLue nnn paano.
[annoe coo6uenne, xo1n ono n onpepeneno pnn knacca Magnitude, nepeonpepenne1cn a
6onLunnc1ae nopknaccoa. [nn uenouncnennmx snauenn cHmcn coo6uennn cpaann1L
uenme uncna. TeH caHmH coo6uenne between:and: Hoxe1 ncnonLsoaa1Lcn pnn npoaepkn
nonapannn uenoro uncna a sapannm nn1epaan. [nn snauennn c nnaaakue 1ouko ace
nponcxopn1 ananornuno:
anInteger between: 7 and: 11
aFloat between: 2.7 and: 3.5
[nn cnHaonoa coo1nouenne HenLue nnn paano cpaannaae1 ASCII kopm. Coo1ae1c1aenno
coo6uenne between:and: npoaepne1, nexn1 nn cnHaon a nn1epaane Hexpy payHn ppyrnHn
cnHaonaHn. HanpnHep, u1o6m ysna1L, nanne1cn nn cnHaon aChar c1pouno 6ykao, Hm
HoxeH ncnonLsoaa1L cnepykuee ampaxenne (nekceHa $a o6osnauae1 a nsmke Smalltalk
cnHaon a):
aChar between: $a and: $z
[nn 1ouek Points cpaanenne HenLue nnn paano aosapauae1 true, ecnn nonyua1enL
pacnonoxen amue n neaee apryHen1a (1o ec1L n nepaan n a1opan kooppnna1m nonyua1enn
ypoane1aopnk1 coo1nouennk HenLue nnn paano npn cpaanennn c coo1ae1c1aykunHn
kooppnna1aHn 1oukn-apryHen1a). Points 6asoame o6Lek1m nsmka Smalltalk. Onn
konc1pynpyk1cn ns uenmx uncen c noHouLk onepa1opa @. ncno-nonyua1enL c1anoan1cn
nepao kooppnna1o, a uncno-apryHen1 a1opo. 3aHe1L1e, u1o onpepenenne coo1nouennn
< (HenLue) pnn 1ouek pae1 nnuL uac1nunoe ynopnpounaanne. He ace 1oukn nannk1cn
consHepnHmHn. TeH ne Henee ampaxenne
aPoint between: 2@4 and: 12@14
pae1 true, ecnn 1ouka aPoint nexn1 a npnHoyronLnnke c kooppnna1aHn (2,4) pnn neaoro
aepxnero yrna n (12,14) pnn npaaoro nnxnero yrna.
Baxnm HoHen1 specL a1o 1o, u1o ao acex cnyuanx ncnonLsye1cn 1onLko opnn He1op
between:and:. On nanne1cn nonnHopqnmH n pa6o1ae1 c apryHen1aHn Hnornx 1nnoa. B
kaxpoH cnyuae nepeonpepenenne coo6uenn, amsmaaeHmx nonnHopqno nopnporpaHHo
(coo6uennn HenLue nnn paano), npncnoca6nnaae1 kop k konkpe1nmH o6c1on1enLc1aaH.
B rnaae 18 Hm ac1pe1nH Hnoro noamx npnHepoa nonnHopqnmx nopnporpaHH, korpa 6ypeH
o6cyxpa1L ua6nonm.
14.8. O6o6euume qyukuu u ma6noum
Coaepuenno ppyro 1nn nonnHopqnsHa o6ecneunaae1cn sa cue1 1ak nasmaaeHmx
o6o6uennmx qynkun, ko1opme a nsmke C++ nasmaak1cn ua6nonaHn. ApryHen1oH
o6o6uenno qynkunn (knacca) nanne1cn 1nn, ko1opm ncnonLsye1cn npn ee (ero)
napaHe1pnsaunn. Ananornn c o6munmHn qynkunnHn oueanpna: nocnepnne peannsyk1
neo6xopnHm anropn1H 6es sapannn konkpe1nmx uncnoamx snauenn. 1o6m
nponnnkc1pnpoaa1L nonn1ne o6o6uenno qynkunn, aepneHcn k nauany a1o rnaam. TaH Hm
o1He1nnn, u1o npo6neHa c nsmkaHn co c1pornH kon1poneH 1nnoa pannmx coc1on1 a 1oH, u1o
onn ne paspeuak1 cospaaa1L 1nn apope Linked List of X (cansannm cnncok ns o6Lek1oa X),
rpe npen1nqnka1op X a1o nensaec1nm 1nn pannmx. O6o6uennme qynkunn o6ecneunaak1
1akyk aosHoxnoc1L.
[nn o6o6uennmx qynkun nnn knaccoa apryHen1 a1o 1nn pannmx. On Hoxe1
ncnonLsoaa1Lcn any1pn onpepenennn knacca, kak ecnn 6m on yxe 6mn onpepenen, xo1n
nnkakne caoc1aa a1oro 1nna pannmx ne nsaec1nm koHnnnn1opy npn cun1maannn onncannn
converted to PDF by BoJIoc
knacca. [anee npn onpepenennn konkpe1noro o6Lek1a napaHe1p-1nn cansmaae1cn c
peanLnmH 1nnoH pannmx. HanpnHep, cansnm cnncok Hoxe1 6m1L onncan a nsmke C++
cnepykunH o6pasoH:
template class List
{
public:
void add(T);
T firstElement();
// nonn zann-x
T value;
List * nextElement;
};
B a1oH npnHepe npen1nqnka1op T ncnonLsye1cn kak o6osnauenne 1nna. Kaxpm akseHnnnp
knacca List copepxn1 snauenne 1nna T n ykasa1enL na cnepykun aneHen1 cnncka.
Oynkunn-unen add po6aanne1 noam aneHen1 a cnncok. Hepam aneHen1 a cnncke
aosapauae1cn qynkune firstElement.
1o6m cospa1L akseHnnnp knacca, nonLsoaa1enL ponxen o6ecneun1L snauenne 1nna pannmx
pnn napaHe1pa T. Cnepykune koHanpm cospak1 cnncok uenmx uncen n cnncok uncen c
nnaaakue 1ouko:
List aList;
List bList;
Oynkunn (aknkuan qynkunn-unenm knacca) 1akxe Hory1 nHe1L onncannn-ua6nonm. Hnxe
npnaopn1cn onncanne qynkunn, ko1opan naxopn1 konnuec1ao aneHen1oa a cnncke
nesaancnHo o1 ero 1nna.
template int length (List & aList)
{
if (aList == 0) return 0;
return 1 + length(aList.nextElement);
}
B C++ qynkunn-ua6nonm nn1encnano ncnonLsyk1cn a c1anpap1no 6n6nno1eke ua6nonoa,
ko1opan onncmaae1cn a rnaae 16.
14.9. HonuHopqusH a pasnuvumx nsmkax
B a1oH paspene o6cyxpak1cn HexannsHm peannsaunn nonnHopqnsHa a pasnnunmx nsmkax
nporpaHHnpoaannn.
14.9.1. HonuHopqusH a C++
HonnHopqnsH uac1o nanne1cn nc1ounnkoH sa1pypnenn pnn nsyuakunx C++. Hoa1oHy
oc1anoanHcn na a1oH aonpoce noppo6nee.
HonuHopqume nepeHeuume
Kak Hm o1He1nnn a rnaae 10, a nsmke C++ nc1nnnme nonnHopqnme nepeHennme aosnnkak1
1onLko npn ncnonLsoaannn ykasa1ene nnn ccmnok. Korpa nac1onue nepeHenno (1o ec1L
ne ykasa1enk n ne ccmnke) npncaanaae1cn snauenne 1nna nopknacca, 1o pnnaHnueckn
knacc snauennn amnyxpenno npnaopn1cn 1ak, u1o6m coanapa1L co c1a1nuecknH 1nnoH
nepeHenno.
Opnako npn ncnonLsoaannn ykasa1ene nnn ccmnok snauenne coxpanne1 cao
pnnaHnueckn 1nn. 1o6m nonn1L a1o1 npouecc, paccHo1pnH cnepykune paa knacca:
class One
converted to PDF by BoJIoc
{
public:
virtual int value()
{
return 1;
}
};
class Two : public One
{
public:
virtual int value()
{
return 2;
}
};
Knacc One onncmaae1 anp1yanLnm He1op, ko1opm aosapauae1 snauenne 1. D1o1 He1op
nepeonpepenne1cn a knacce Two na He1op, aosapauakun snauenne 2.
Onpepennk1cn cnepykune qynkunn:
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());
}
D1n qynkunn ncnonLsyk1 a kauec1ae apryHen1a snauenne knacca One, ko1opoe nepepae1cn
coo1ae1c1aenno no snauennk, uepes ykasa1enL n uepes ccmnky. Hpn amnonnennn a1nx
qynkun c apryHen1oH knacca Two pnn nepao qynkunn napaHe1p npeo6pasye1cn k knaccy
One, n a pesynL1a1e 6ype1 naneua1ano snauenne 1. [ae ppyrne qynkunn ponyckak1
nonnHopqnm apryHen1. B o6onx cnyuanx nepepannoe snauenne coxpann1 cao
pnnaHnueckn 1nn pannmx, n naneua1ano 6ype1 snauenne 2.
Bup1yanuuoe u ueaup1yanuuoe nepeonpepeneuue
Hpnaopnun a saHeua1enLc1ao acnek1 nepeonpepenennn He1opoa a nsmke C++ a1o
pasnnua Hexpy nepeonpepenenneH anp1yanLnoro n neanp1yanLnoro He1opoa. Kak Hm
o1Heuann a rnaae 11, knkueaoe cnoao virtual ne nanne1cn neo6xopnHmH pnn 1oro, u1o6m
nponcxopnno nepeonpepenenne. Opnako ceHan1nueckn cHmcn cnnLno Henne1cn a
saancnHoc1n o1 1oro, ncnonLsye1cn a1o cnoao nnn ne1. Ecnn ypann1L knkueaoe cnoao virtual
ns onncannn He1opa a knacce One a npepmpyueH npnHepe (paxe ecnn ero coxpann1L a
knacce Two), 1o pesynL1a1 1 6ype1 naneua1an pnn acex 1pex qynkun.
Bes knkueaoro cnoaa virtual pnnaHnueckn 1nn nepeHenno (paxe pnn ykasa1ene n
ccmnok) nrnopnpye1cn, korpa nepeHennan ncnonLsye1cn kak nonyua1enL coo1ae1c1aykuero
coo6uennn.
converted to PDF by BoJIoc
Eue 6onLuee saHeua1enLc1ao aosnnkae1, ecnn nporpaHHnc1 nm1ae1cn nepeonpepenn1L
anp1yanLnyk qynkunk a nopknacce, no npn a1oH ykasmaae1 (aosHoxno, no oun6ke) ppyro
1nn apryHen1oa. HanpnHep, popn1enLckn knacc copepxn1 onncanne
virtual void display (char *, int);
Hopknacc nm1ae1cn nepeonpepenn1L He1op:
virtual void display (char *, short);
HockonLky cnnckn apryHen1oa pasnnuak1cn, 1o a1opoe onpepenenne ne pacnosnae1cn kak
nepeonpepenenne. D1o npnaopn1 k 1oHy, u1o anp1yanLnoe nepeonpepenenne
paccHa1pnaae1cn kak o6munoe (neanp1yanLnoe) onpepenenne He1opa. Hoa1oHy, nanpnHep,
npn amsoae a qopHe popn1enLckoro 1nna 6ype1 am6npa1Lcn nepam He1op, a ne a1opo.
Takne oun6kn upesamuano 1pypnoynoanHm, nockonLky o6e qopHm sanncn ponyc1nHm n
napen1Lcn na pnarnoc1nky koHnnnn1opa ne npnxopn1cn.
Bosnnkae1 ec1ec1aennm aonpoc: noueHy paspeuenm o6e qopHm sanncn n noueHy ne
acnkoe nepeonpepenenne anp1yanLno? HHek1cn no kpane Hepe paa npaaponopo6nmx
o6Lncnennn. Hepaoe coc1on1 a 1oH, u1o sa pepknHn ncknkuennnHn neanp1yanLnan qopHa
nepeonpepenennn a1o nHenno 1o, u1o xoue1 nporpaHHnc1, n o6nsa1enLnan anp1yanLnoc1L
6mna 6m 1onLko noHexo. B1opoe n 6onee y6epn1enLnoe coo6paxenne o1nocn1cn k
aqqek1nanoc1n. Bnp1yanLnoe nepeonpepenenne acerpa 6onee sa1pa1no c 1oukn spennn
amnonnennn nporpaHHm, ueH neanp1yanLnoe. Hpnnunn, npoxopnun kpacno nn1Lk uepes
aecL C++, coc1on1 a 1oH, u1o ecnn kakoe-1o caoc1ao nsmka ne aoc1pe6oaano nnn xe
ncnonLsye1cn 1onLko pnn konkpe1no npo6neHm, 1o nporpaHHnc1 ne ponxen
pacnnaunaa1Lcn (na a1ane amnonnennn) sa ero cyuec1aoaanne. Coo1ae1c1aenno, ecnn
anp1yanLnoe nacnepoaanne ne 1pe6ye1cn nnn xe ncnonLsye1cn nnuL a cneunanLnoH cnyuae,
nnkaknx pononnn1enLnmx naknapnmx pacxopoa 6m1L ne ponxno. TonLko a 1ex cnyuanx,
korpa nporpaHHnc1 a nanoH anpe y1aepxpae1, u1o eHy 1pe6ye1cn anp1yanLnan qynkunn,
nopknkuae1cn pononnn1enLnan napc1poka.
HapaHe1puveckan neperpyska
Hsmk C++ nosaonne1 neckonLknH qynkunnH nHe1L opno nHn any1pn nk6oro kon1ekc1a po
1ex nop, noka cnnckn apryHen1oa qynkun pasnnuak1cn a poc1a1ouno c1enenn, u1o6m
koHnnnn1op nepaycHmcnenno onpepennn, kakyk nHenno qynkunk naHepeaak1cn amsaa1L.
Takan cn1yaunn, kak npaanno, aosnnkae1 npn ncnonLsoaannn neckonLknx konc1pyk1opoa
pnn opnoro n 1oro xe knacca, kaxpm ns ko1opmx nHee1 cao na6op apryHen1oa. Opnako
1aknH o6pasoH Hory1 onncmaa1Lcn nk6me qynkunn, He1opm nnn onepaunn.
Hpaanna pnn cnn1nn paycHmcnennoc1n c neperpyxennmx qynkun nannk1cn poaonLno
1onknHn, a oco6ennoc1n ecnn paspeueno aa1oHa1nueckoe npnaepenne 1nnoa pannmx. Opnn
ns nan6onee aaxnmx npnnunnoa, ko1opme neo6xopnHo noHnn1L: npn noncke qynkunn
koHnnnn1op npocHa1pnaae1 nan6onee yskyk o6nac1L anpnHoc1n, a ko1opo onpepeneno nHn
qynkunn, n sa1eH a npepenax a1o o6nac1n nue1 nopxopnuyk qynkunk, ocnoamaancL na
1nne apryHen1oa. To ec1L opna qynkunn Hoxe1 ckpm1L ppyryk c 1eH xe nHeneH,
onpepenennyk a 6onee unpoko o6nac1n anpnHoc1n.
O1noxeuume He1opm a C++
B nsmke C++ o1noxennm He1op (ko1opm specL nasmaae1cn unc1o anp1yanLnmH He1opoH)
ponxen 6m1L onncan a nanoH anpe c knkueamH cnoaoH virtual. Teno o1noxennoro He1opa ne
onpepenne1cn, aHec1o a1oro qynkunn npncaanaae1cn snauenne 0:
class Shape
{
public:
...
virtual void draw() = 0;
converted to PDF by BoJIoc
...
};
KoHnnnn1op ne paspeuae1 nonLsoaa1enk cospaaa1L akseHnnnp knacca, ko1opm copepxn1
unc1o anp1yanLnme He1opm. Hopknaccm ponxnm a1n He1opm nepeonpepenn1L.
Hepeonpepenenne unc1o anp1yanLnoro He1opa ponxno nponso1n npn onncannn ero
no1oHkoa, pnn ko1opmx cospak1cn peanLnme o6Lek1m.
O6o6euume qyukuu u ma6noum
O6o6uennme qynkunn n knaccm a nsmke C++ peannsyk1cn c noHouLk knkueaoro cnoaa
template (ua6non). HpnHep knacca-ua6nona 6mn npnaepen amue. Knaccm-ua6nonm n
qynkunn-ua6nonm nn1encnano ncnonLsyk1cn c1anpap1no 6n6nno1eko ua6nonoa nsmka
C++, ko1opyk Hm o6cyxpaeH a rnaae 16.
14.9.2. HonuHopqusH a Java
Hsmk Java noppepxnaae1 kak nepapxnk nopknaccoa (c knkueamH cnoaoH extends), 1ak n
nepapxnk nop1nnoa (c knkueamH cnoaoH interfaces). HepeHennme Hory1 6m1L o6Lnanenm
nnn uepes knacc, nnn uepes nn1epqec. Bce nepeHennme nannk1cn nonnHopqnmHn.
HepeHennan, onncannan kak knacc, Hoxe1 copepxa1L snauennn, o1nocnunecn k nk6oHy
nopknaccy. HepeHennan, o6Lnanennan kak nn1epqec, Hoxe1 xpann1L snauennn nk6oro
knacca, ko1opm peannsye1 a1o1 nn1epqec.
O1noxennme He1opm peannsyk1cn a nsmke Java uepes knkueaoe cnoao abstract. Me1opm,
onncannme kak abstract, ne nHek1 1ena; onn okanunaak1cn 1ouko c sann1o. A6c1pak1nme
He1opm ponxnm nepeonpepenn1Lcn a nopknaccax. Knacc, ko1opm aknkuae1 a ce6n
a6c1pak1nm He1op, ponxen a caok ouepepL 6m1L onncan kak a6c1pak1nm. He
paspeuae1cn cospaaa1L akseHnnnpm a6c1pak1nmx knaccoa.
abstract class shape
{
// nw+e zon+no nepeonpezenniscn
public abstract draw();
// ...
}
class triangle extends shape
{
public draw()
{
// napwcosais ipeyronsnwx
}
// ...
}
Hn1epecnmH caoc1aoH nsmka Java nanne1cn Hopnqnka1op final, ko1opm a neko1opoH
cHmcne npo1naononoxen knkueaoHy cnoay abstract. Knacc nnn He1op, onncannm kak final,
ne Hoxe1 nopoxpa1L nopknaccm nnn nepeonpepenn1Lcn.
14.9.3. HonuHopqusH a Object Pascal
HonuHopqume nepeHeuume
B nsmke Object Pascal ace nepeHennme no1enunanLno nonnHopqnm npn nenanoH
npepnonoxennn, u1o nopknaccm npepc1aannk1 co6o nop1nnm. Bce nepeHennme xpann1
snauenne nnn o6Lnanennoro knacca, nnn ero nopknacca.
converted to PDF by BoJIoc
O1noxeuume He1opm a Object Pascal
Kak Hm o1He1nnn a rnaae 7, aepcnn Apple n Borland nsmka Object Pascal o1nnuak1cn 1eH,
kak onn ykasmaak1 na nepeonpepenenne He1opa. B Object Pascal aepcnn Apple knkueaoe
cnoao override noHeuae1cn a onncanne He1opa pouepnero knacca. Bepcnn Delphi 1pe6ye1
knkueamx cnoa override n virtual a onncannn He1opa a popn1enLckoH knacce.
Hsmk Object Pascal aepcnn Apple ne noppepxnaae1 o1noxennme He1opm. Onn peannsyk1cn a
anpe npouepypm, renepnpykue coo6uenne o6 oun6ke:
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;
B nsmke Delphi Pascal He1op Hoxe1 6m1L o6Lnanen kak o1noxennm c knkueamH cnoaoH
abstract, cnepykunH sa knkueamH cnoaoH virtual (nnn dynamic) npn onncannn a
popn1enLckoH knacce. [nn a6c1pak1noro He1opa ne sapae1cn 1eno. B o1nnune o1 C++ Hoxno
cospa1L o6Lek1, knacc ko1oporo nHee1 ace eue ne nepeonpepenennme a6c1pak1nme He1opm.
To ec1L nsmk Delphi noppepxnaae1 a6c1pak1nme He1opm, no ne a6c1pak1nme knaccm.
type
class TShape
procedure draw; virtual; abstract;
...
end;
class TTriangle (TShape)
procedure draw; override;
...
end;
14.9.4. HonuHopqusH a Objective-C
HonuHopqume nepeHeuume
Hpn onncannn c knkueamH cnoaoH id ace nepeHennme a Objective-C nonnHopqnm n noa1oHy
Hory1 copepxa1L nk6oe snauenne. Hpn onncannn c konkpe1nmH knaccoH nepeHennme nHek1
ace caoc1aa (xopoune n ne ouenL) nepeHennmx nsmka C++.
O1noxeuume He1opm a Objective-C
He 1pe6ye1cn cneunanLnoro ykasannn pnn onncannn o1noxennoro He1opa a nsmke Objective-
C. 1o6m noHouL a cospannn 1aknx He1opoa, a knacce Object onpepenne1cn coo6uenne
subclassResponsibility (ko1opoe 1eH caHmH poc1ynno pnn acex o6Lek1oa). Ono npoc1o
neua1ae1 c1poky, nokasmaakuyk, u1o amnonnneHoe pec1ane ponxno 6m1L
nepeonpepeneno a nopknacce.
converted to PDF by BoJIoc
O1noxennm He1op draw pnn knacca Shape Hoxe1 6m1L sanncan, k npnHepy, cnepykunH
o6pasoH:
@implementation Shape : Object
...
- draw { return [ self subclassResponsibility ]; }
...
@end
14.9.5. HonuHopqusH a Smalltalk
HonuHopqume nepeHeuume
HockonLky Smalltalk a1o nsmk c pnnaHnuecknHn 1nnaHn pannmx, ace nepeHennme
nannk1cn nonnHopqnmHn. Onn Hory1 xpann1L nk6oe snauenne.
O1noxeuume He1opm a Smalltalk
B nsmke Smalltalk ne 1pe6ye1cn cneunanLnoro ykasannn, u1o6m onnca1L o1noxennm He1op.
B knacce Object onpepeneno coo6uenne subclassResponsibility (1eH caHmH poc1ynnoe nk6mH
ppyrnH o6Lek1aH). Ono neua1ae1 c1poky, nokasmaakuyk, u1o pnn neko1oporo nopknacca
amsmaae1cn pec1ane, ko1opoe ponxno 6m1L nepeonpepeneno.
O1noxennm He1op draw pnn knacca Shape Hoxe1 6m1L sanncan cnepykunH o6pasoH:
draw
" zouepnwe xnacc- zon+n- nepeonpezennis oioi meioz "
self subclassResponsibility
14.10. 3qqek1uauoc1u u nonuHopqusH
HporpaHHnpoaanne acerpa caopn1cn k koHnpoHnccy. B uac1noc1n, nonnHopqnsH
noppasyHeaae1 koHnpoHncc Hexpy npoc1o1o paspa6o1kn n ncnonLsoaannn, un1aeHoc1Lk
kopa n aqqek1nanoc1Lk. B snaun1enLno c1enenn aqqek1nanoc1L yxe 6mna naHn
paccHo1pena: amncnnnocL, u1o no1epn a aqqek1nanoc1n ne c1onL aennkn. Opnako 6mno 6m
nenpoc1n1enLnmH ne npnnnHa1L nonnoc1Lk ao annHanne a1o1 HoHen1.
Oynkunn (nopo6nan He1opy between:and:, onncannoHy a npepmpyueH paspene), ko1opan ne
snae1 1nn caoero apryHen1a, apnp nn 6ype1 c1onL xe aqqek1nana, kak qynkunn,
anapekuan nonno nnqopHaune. Cpaann1enLnm 1ec1 Hoxe1 o6napyxn1L nnuL neckonLko
pononnn1enLnmx koHanp na nsmke acceH6nepa a cnyuae, ecnn apryHen1oH nanne1cn uenoe
uncno. Ecnn xe apryHen1 a1o o6Lek1-1ouka, 1o 1pe6yk1cn ropaspo 6onee
npoponxn1enLnme pec1ann. TeH ne Henee npenHyuec1aa 6mc1po paspa6o1kn,
caHocornacoaannoro noaepennn npnnoxennn n aosHoxnoc1L Hnorokpa1no ncnonLsoaa1L
nporpaHHnm kop o6muno snaua1 6onLue, ueH ne6onLune no1epn a aqqek1nanoc1n.
Ynpaxueuun
1. Kak am pyHae1e, cnepye1 nn paccHa1pnaa1L snauenne nil a nsmke Pascal, nnn
ananornunyk aennunny NULL a C kak nonnHopqnm o6Lek1? O6ocny1e cao o1ae1.
2. Kakne eue onepaunn (sa ncknkuenneH apnqHe1nuecknx) o6muno nannk1cn
neperpyxennmHn a 1papnunonnmx nsmkax nporpaHHnpoaannn (Pascal n C)?
3. Hpoaepn1e 1paccnpoaky He1opoa n knaccoa npn amuncnennn ampaxennn:
anInteger between: 7 and: 11
converted to PDF by BoJIoc
4. HpepnonoxnH, u1o a nsmke Smalltalk nHek1cn paa knacca: n6nokn Apple n
anenLcnnm Orange, ko1opme nannk1cn nopknaccaHn qpyk1oa Fruit. Kako
HnnnHanLnm o6LeH kopa no1pe6ye1cn pnn cpaanennn n6nok n anenLcnnoa?
Fnaaa 15

Yve6um npuHep:
kou1euepume
knaccm
Hou1n ace ne1pnananLnme koHnLk1epnme nporpaHHm 6asnpyk1cn na npoc1mx c1pyk1ypax
pannmx, 1aknx kak cansnme cnnckn, c1ekn, ouepepn, pepeaLn, Hnoxec1aa, cnoaapn.
HockonLky a1n c1pyk1ypm nannk1cn 1nnnunmHn, xopouo 6mno 6m nHe1L nx a kauec1ae
Hnorokpa1no ncnonLsyeHmx koHnonen1oa. Ha caHoH pene Hoxno cospa1L 1akne koHnonen1m,
no aosnnkakune npn a1oH cnoxnoc1n uac1o ouenL snaun1enLnm. Hoa1oHy paspa6o1ka
Hnorokpa1no ncnonLsyeHmx kon1enepnmx knaccoa nanne1cn xopounH yue6nmH npnHepoH.
On nnnkc1pnpye1, kak caoc1aa nsmka nporpaHHnpoaannn annnk1 na c1nnL paspa6o1kn, a
1akxe peHonc1pnpye1 neko1opme poc1onnc1aa n orpannuennn o6Lek1no-opnen1npoaannmx
He1opoa.
[anee Hm paccHo1pnH 1pn 1ecno cansannmx aonpoca:
Moxno nn ckonc1pynpoaa1L Hnorokpa1no ncnonLsyeHyk a6c1pakunk kon1enepa
pannmx o6uero nasnauennn, ko1opan ne saancn1 o1 1nna xpannHmx aneHen1oa n 1eH
caHmH nepenocnHa ns opnoro npoek1a a ppyro?
[onxen nn 1ako kon1enep copepxa1L pannme 1onLko opnoro 1nna (1ak
nasmaaeHme opnopopnme kon1enepm) nnn ypac1cn noc1pon1L kon1enep,
copepxaun snauennn pasnnunoro 1nna (pasnopopnme kon1enepm)?
Moxno nn paspeun1L nonLsoaa1enk kon1enepa pannmx poc1yn k xpannHmH a neH
aneHen1aH, rapan1npyn npn a1oH saun1y o1 ypanennn n Hacknpoaky any1penne
peannsaunn kon1enepa?
15.1. Hcnonusoaauue 1papuuouumx nopxopoa
1o6m yanpe1L npo6neHy a nepcnek1nae, Hm ponxnm cnepaa paccHo1pe1L, kak c1pyk1ypm
pannmx o6muno peannsyk1cn a 1papnunonnmx nsmkax nporpaHHnpoaannn (ckaxeH, C n
Pascal). HcnonLsyeH cansnm cnncok uenmx uncen kak npnHep HopennpyeHo a6c1pakunn. B
nsmke Pascal cansnm cnncok o6pasye1cn ns sannce payx 1nnoa. Hepam 1nn a1o nauano
cnncka, ko1opm copepxn1 ykasa1enL na nepam aneHen1:
type
List = record
firstLink : Link;
end;
Hauano (ronoaa) cnncka Hoxe1 6m1L pasHeueno c1a1nueckn, nockonLky pasHep 1pe6yeHo
naHn1n (a nHenno epnnc1aennm ykasa1enL) oc1ae1cn noc1onnnmH ao apeHn amnonnennn.
B1opo 1nn sannce ncnonLsye1cn, u1o6m xpann1L caHn snauennn. Kaxpm ysen Link
copepxn1 opno uenoe uncno n ykasa1enL na cnepykun aneHen1 cnncka:
type
Link = record
value : integer;
nextElement : Link;
converted to PDF by BoJIoc
end;
DneHen1m ponxnm pasHeua1Lcn n ypann1Lcn pnnaHnueckn, xo1n 1akne noppo6noc1n
cnepye1 cnpn1a1L o1 nonLsoaa1enn. D1o poc1nrae1cn c noHouLk paspa6o1kn qynkun,
ko1opme po6aannk1 snauenne a nauano cnncka, aosapauak1 nepam aneHen1 cnncka,
ypannk1 ero n 1. p.
procedure addToList(var aList : List,
newVal : integer);
(* zoasnnei nos-n onemeni s cnwcox *)
var
newLink : Link;
begin
(* coszais w npownwuwanwswposais nos-n onemeni *)
new(newLink);
newLink.value := newVal;
(* nomeciwis ero s nauano cnwcxa *)
newLink.nextElement := aList.firstLink;
aList.firstLink := newLink;
end;
function firstElement (var aList : List) : integer;
(* yzannei ws cnwcxa w sosspamaei neps-n onemeni *)
var
firstNode : Link;
begin
firstNode := aList.firstLink;
firstElement := firstNode^.value;
aList.firstLink := firstNode^.nextElement;
dispose(firstNode);
end;
Inaanoe specL ne pe1ann peannsaunn cansnoro cnncka (nx Hoxno na1n a nk6oH yue6nnke
no c1pyk1ypaH pannmx), no aosHoxnoc1n Hnorokpa1noro ncnonLsoaannn. HpepnonoxnH, u1o
nporpaHHnc1 peannsoaan a6c1pakunk cansnoro cnncka, npnaepennyk amue. TenepL on
xoue1 ncnonLsoaa1L napnpy co cansnmH cnnckoH uenmx uncen cansnm cnncok
aeuec1aennmx uncen.
Hpo6neHa coc1on1 a 1oH, u1o nsmk nporpaHHnpoaannn cnnukoH c1poro npoaepne1 1nnm
pannmx. Tnn integer, ncnonLsyeHm pnn snauennn xpannHoro a cnncke, nanne1cn
neo1LeHneHo coc1aano uac1Lk onncannn. Epnnc1aennm cnoco6 aaec1n noam 1nn
pannmx a1o cospa1L coaepuenno noayk c1pyk1ypy RealLink, noayk c1pyk1ypy nauana
cnncka RealList n nopnporpaHHm pnn poc1yna k a1nH c1pyk1ypaH pannmx.
Heu1o apope sannce c aapnan1nmHn nonnHn (1nn pannmx union a nsmke C) Hoxe1 noHouL
ncnonLsoaa1L opny n 1y xe a6c1pakunk cnncka pnn xpanennn kak uenmx, 1ak n
aeuec1aennmx uncen. B pec1an1enLnoc1n aapnan1nme sanncn nosaonnk1 onpepenn1L
pasnopopnm cnncok, ko1opm copepxn1 n uenme uncna, n uncna c nnaaakue 1ouko. Ho
aapnan1nme sanncn a1o 1onLko uac1L peuennn npo6neHm. HenLsn onpepenn1L qynkunk,
ko1opan aosapauae1 aapnan1nyk sanncL, 1ak u1o no-npexneHy 1pe6ye1cn cospaaa1L
o1penLnme qynkunn pnn nonyuennn nepaoro aneHen1a cnncka. Bonee 1oro, aapnan1nan
sanncL nHee1 1onLko koneunm na6op ponyc1nHmx anL1epna1nanmx aapnan1oa. 1o ecnn
pnn cnepykuero npoek1a no1pe6ye1cn coaepuenno noam 1nn cnncka (nanpnHep, c
1ekc1oamHn c1pokaHn)?
TenepL paccHo1pnH npo6neHy poc1yna k nponsaonLnoHy aneHen1y 6es ypanennn
npepuec1aykunx eHy aneHen1oa ns kon1enepa. Tnnnunm unkn, ko1opm neua1ae1
snauennn ns cnncka, amrnnpn1 npnHepno 1ak:
var
aList : List; (* opaai-saem-n cnwcox *)
converted to PDF by BoJIoc
p : Link; (* yxasaiens, wcnonssyem-n s uwxne *)
begin
...
p := aList.firstLink;
while (p <> nil) do
begin
writeln(p.value);
p := p.nextElement;
end;
...
3aHe1L1e, u1o pnn npoxoxpennn unkna neo6xopnHo 6mno aaec1n pononnn1enLnyk
nepeHennyk, nasaannyk specL p. Ona ponxna npnnapnexa1L 1nny pannmx Link, ko1opm
Hm naHepeaanncL saHacknpoaa1L. Touno 1ak xe caH unkn 1pe6ye1 poc1yna k nonnH
nepeHenno Link, ko1opme Hm 1akxe ne xo1enn 6m o1kpmaa1L.
H1ak, kak Hm anpnH, a 1papnunonnmx nsmkax nporpaHHnpoaannn c kon1poneH 1nnoa ne1
cpepc1a, neo6xopnHmx pnn cospannn n o6pa6o1kn kon1enepoa, ko1opme 6mnn 6m nc1nnno
Hnorokpa1no ncnonLsyeHmHn.
15.2. Kou1euepm a puuaHuveckux nsmkax
Cospanne Hnorokpa1no ncnonLsyeHmx a6c1pakun kon1enepoa nponcxopn1 naHnoro npoue
a nsmkax nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx (Smalltalk, Objective-C). Ha
caHoH pene 1akne nsmkn o6muno yxe copepxa1 6onLuo na6op ro1oamx a6c1pakun
pannmx, 1eH caHmH ocao6oxpan nporpaHHnc1a o1 neo6xopnHoc1n cospaaa1L kon1enepm.
Kak Hm anpenn amue npn o6cyxpennn aonpoca o apeHenn cansmaannn, a nsmkax
nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx snanne o 1nne xpann1 a ce6e
snauenne, a ne nepeHennan, c noHouLk ko1opo ocyuec1anne1cn poc1yn k snauennk.
HanpnHep, naua a6c1pakunn cansnoro cnncka Hoxe1 6m1L onpepenena uepes cnepykune
c1pyk1ypm nsmka 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
O snauennn, noHeuaeHoH a 1akyk c1pyk1ypy, nsaec1no, u1o ono 1nna id (1o ec1L 1nna
pannmx o6Lek1). Ananornuno snauenne, nsanekaeHoe ns cnncka, 1oxe 1nna id, no ono
Hoxe1 6m1L npncaoeno nk6o o6Lek1no nepeHenno, nockonLky 1aknH nepeHennmH
paspeuae1cn xpann1L o6Lek1 nponsaonLnoro 1nna.
1o6m cospa1L noam cnncok, nporpaHHnc1 nocmnae1 coo6uenne new qa6pnke o6Lek1oa a
knacce List:
id aList
...
aList = [ List new ];
converted to PDF by BoJIoc
1o6m noHec1n1L snauenne a cnncok, nporpaHHnc1 ncnonLsye1 coo1ae1c1aykuyk qynkunk-
unen:
[ aList addToList: aValue ];
Onepaunn co cnnckoH ne 1pe6yk1 nnkaknx pononnn1enLnmx snann o 1nne snauenn,
ko1opme copepxa1cn a cnncke, sa ncknkuenneH 1oro, u1o a1o o6Lek1m:
@ implementation List
- (void) addList: newElement
/* zoaswis s cnwcox nos-n onemeni */
{
id newLink;
newLink = [ Link new ];
[ newLink setValue: newElement link: firstLink ];
firstLink = newLink;
}
- id firstElement
/* yzanwis w sepnyis neps-n onemeni cnwcxa */
{
id result;
result = [ firstLink value ];
firstLink = [firstLink nextElement ];
return result;
}
@ end
AnanornunmH o6pasoH a nsmkax nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx
Hoxno o6pa6a1maa1L n1epaunn 6es 1oro, u1o6m amc1aann1L na o6ospenne any1pennkk
c1pyk1ypy kon1enepoa. Mm onnueH pae 1akne 1exnnkn: opna ncnonLsye1cn a Smalltalk, a
ppyran noaceHec1no npnHenne1cn a pasnoo6pasnmx nsmkax nporpaHHnpoaannn.
Mm o1He1nnn panee, u1o a nsmke Smalltalk onepa1opm Hory1 6m1L crpynnnpoaanm a
konc1pykunk, nasmaaeHyk block. Ona ao HnoroH ananornuna qynkunn. Hopo6no qynkunn,
6nok Hoxe1 nHe1L cnncok apryHen1oa. C1anpap1nm cnoco6 amnonnennn n1epaunn a nsmke
Smalltalk a1o nepepa1L 6nok kak apryHen1 aHec1e c coo6uenneH c1pyk1ype, k ko1opo
ocyuec1anne1cn poc1yn. Unkn, ko1opm neua1ae1 snauennn cnncka, Hoxe1 6m1L sanncan
cnepykunH o6pasoH:
aList do: [ :ele S ele print ]
Knacc cnncka npoc1o nepecmnae1 6nok knaccy aneHen1a. Kaxpm aneHen1 amsmaae1 6nok c
ncnonLsoaanneH caoero 1ekyuero snauennn, n sa1eH nepecmnae1 6nok cnepykueHy
aneHen1y.
linkDo: aBlock
" s-nonnwis nox, nepezais ero cnezyxmemy onemeniy cnwcxa "
aBlock value: value.
nextLink notNil
ifTrue: [ nextLink linkDo: aBlock ]
Hpn 1akoH nopxope ypae1cn nponsaopn1L pasnoo6pasnme n1epaunn, npnueH ace 6es
nokasa c1pyk1ypm cnncka.
B nsmke Objective-C n ppyrnx o6Lek1no-opnen1npoaannmx nsmkax peuenne sapaun o6
n1epaunnx 6ype1 neHnoro 6onee cnoxnmH ns-sa o1cy1c1ann 6nokoa.



converted to PDF by BoJIoc
Ruc1uur 15.1. H1epa1op a nsmke 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 ] )
currentLink = [ currentLink nextElement ];
}
@end

C1anpap1no anL1epna1nao nanne1cn cospanne acnoHora1enLnoro o6Lek1a, nasmaaeHoro
n1epa1opoH (iterator). D1o1 o6Lek1 noc1aanne1cn paspa6o1unkoH kon1enepnoro knacca
(nanpnHep, cansnoro cnncka). Epnnc1aennoe nasnauenne 1akoro o6Lek1a o6ecneun1L
poc1yn k aneHen1aH kon1enepa (opnn aneHen1 sa opno o6pauenne) 6es nokasa any1penne
c1pyk1ypm cnncka. O6muno n1epa1op copepxn1 ykasa1enL, c ko1opmH nponsaopn1cn
aceaosHoxnme Hannnynnunn. nc1nnr 15.1 nnnkc1pnpye1, kak Hoxno onpepenn1L n1epa1op
pnn a6c1pakunn cansnoro cnncka.
H1epa1op o6muno onpepenne1cn caHnH cnnckoH kak peakunn na coo6uenne. HoaneHen1nm
unkn nponsaopn1cn cnepykunH o6pasoH:
id aList; /* o+nsnenwe cnwcxa */
id itr; /* o+nsnenwe wiepaiopa */
for (itr = [ aList iterator ]; ! [ itr atEnd ];
[itr advance ])
print( [ itr value ] );
3aHe1L1e, u1o xo1n unkn no1pe6oaan o6Lnanennn pononnn1enLno nepeHenno-n1epa1opa,
ee ncnonLsoaanne ne noppasyHeaae1 snanne any1penne c1pyk1ypm cansnoro cnncka.
erkoc1L, c ko1opo konc1pynpyk1cn n o6pa6a1maak1cn a6c1pakunn pannmx, a1o opnn ns
ocnoanmx peknaHnmx nosynroa nsmkoa c pnnaHnuecknHn 1nnaHn. Kon1enepm nHek1
coaepuenno o6un anp n Hory1 paxe copepxa1L pasnopopnme na6opm pannmx pasnnunmx
1nnoa. K coxanennk, 1ako amnrpmu pae1cn nepapoH. Kak Hm o1He1nnn panee, cyuec1aye1
pnneHHa Hexpy nerkoc1Lk a ncnonLsoaannn n aqqek1nanoc1Lk amnonnennn. HporpaHHm na
pnnaHnuecknx nsmkax pepko amnonnnk1cn c1onL xe aqqek1nano, kak a nsmkax c 6onee
c1pornH kon1poneH 1nnoa.

converted to PDF by BoJIoc
15.3. Kou1euepm a nsmkax co c1poruH kou1poneH 1una
pauumx
Mm nepexopnH 1enepL k paccHo1pennk 1oro, kak kon1enepnme knaccm konc1pynpyk1cn a
nsmkax co c1pornH kon1poneH 1nna pannmx (Object Pascal, C++). Ec1L Hnenne, u1o npnnunn
nopc1anoakn caH no ce6e o6ecneunaae1 peuenne npo6neHm kon1enepnmx knaccoa a 1aknx
nsmkax. Cornacno rnaae 6 npnnunn nopc1anoakn y1aepxpae1, u1o nepeHenno, ko1opan
o6Lnanena c onpepenennmH 1nnoH, Hoxno na caHoH pene npncaon1L snauenne nop1nna.
Hpnnunn nopc1anoakn na caHoH pene po neko1opo c1enenn o6neruae1 peuenne naunx
npo6neH, no paneko ne acex.
1o6m ncnonLsoaa1L nopc1anoaky, Hm npexpe acero ponxnm cospa1L knacc, ko1opm 6m
6mn popn1eneH acero, u1o Hm saxo1nH xpann1L a naue c1pyk1ype pannmx. HasoaeH a1o1
rnno1e1nueckn knacc ListElement. 3a1eH cospapnH a6c1pakunk cnnckoa c aneHen1aHn.
nc1nnr 15.2 nokasmaae1, kak a1o Hoxno cpena1L a Object Pascal.
O6Lek1m, xpannunecn a cnncke, ponxnm 6m1L onncanm kak nopknaccm knacca ListElement.
Coo1ae1c1aenno Hm ne HoxeH noc1pon1L cnncok c uenmHn nnn aeuec1aennmHn uncnaHn,
noka ne nopopnH a1n 1nnm pannmx ns knacca ListElement. O6muno a1o ne ouenL cepLesnan
npo6neHa. Ha caHoH pene Hm nonyunnn pasnopopnm cnncok co snauennnHn pasnnunoro
1nna, ecnn 1onLko ace onn nannk1cn nopknaccaHn 6asoaoro knacca ListElement.
Hac1onuan npo6neHa aosnnkae1, korpa Hm xo1nH cpena1L u1o-nn6o c aneHen1oH,
nsaneuennmH ns cnncka. Kon1ponL 1nnoa pannmx, ko1opm onpepenne1 pesynL1a1 kak
npnnapnexaun knaccy ListElement, ac1ae1 na naueH ny1n. Mm ponxnm o1Henn1L
nopc1anoaky pouepnero 1nna pannmx aHec1o popn1enLckoro 1nna. HpepnonoxnH, k
npnHepy, u1o Hm cospann paa nopknacca pnn knacca ListElement. Hopknacc WhiteBall
npepc1aanne1 6enme uapm, a nopknacc BlackBall uepnme. Mm nHeeH cnncok uapoa n
xo1nH nsaneuL nepam aneHen1 cnncka, npncaona ero snauenne nepeHenno 1nna WhiteBall.
Ruc1uur 15.2. Onncanne kon1enepnoro knacca na nsmke 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);
(* zoasnnei snauenwe x nauany cnwcxa *)
begin
(* none csnsw zon+no yxas-sais na iexymwn onemeni *)
newValue.next := firstLink;
(* wsmenwis cc-nxy s nepsom onemenie cnwcxa *)
firstLink := newValue;
end;
function firstValue : ListElement;
(* wcxnxuwis ws cnwcxa neps-n onemeni w sepnyis ero *)
var
first : ListElement;
begin
first := firstLink;
firstValue := firstLink;
firstLink := first.next;
end;
converted to PDF by BoJIoc

Mm roaopnnn npn o6cyxpennn cansmaannn, u1o specL na caHoH pene nHek1cn paa HoHen1a,
ko1opme, opnako, a neko1opmx o6Lek1no-opnen1npoaannmx nsmkax coepnnenm:
MoxeH nn Hm onpepenn1L 1nn snauennn, nonyuennoro ns cnncka?
Ecnn pa, 1o nosaonn1 nn koHnnnn1op amnonnn1L npncaanaanne 6esonacno c 1oukn
spennn coxpanennn 1nna?
BcnoHnnH, u1o a Object Pascal nepaan ns payx npo6neH peuae1cn uepes ncnonLsoaanne
nornuecko qynkunn Member, ko1opan roaopn1 naH, copepxn1 nn nepeHennan snauenne,
o1nocnueecn k sapannoHy knaccy. Ecnn qynkunn Member nokasmaae1, u1o npeo6pasoaanne
nanne1cn sakonnmH, 1o Hoxe1 6m1L sapec1aoaano npnaepenne 1nna pnn npeo6pasoaannn
snauennn k coo1ae1c1aykueHy 1nny pannmx:
var
aBall : WhiteBall;
aList : List;
aValue : ListElement;
...
(* wssneus onemeni ws cnwcxa *)
aValue := aList.firstElement;
(* iwn zann-x cooiseicisyei? *)
if Member(aValue, WhiteBall) then
(* npwcsawsanwe saxonno *)
aBall := WhiteBall(aValue);
To ec1L nsaneuenne aneHen1a ns c1pyk1ypm pannmx Hoxe1 amnonnn1Lcn a neckonLko
a1anoa, no a1o nHenno 1a 1exnnka, ko1opan ncnonLsye1cn ao Hnornx koHHepueckn
poc1ynnmx c1pyk1ypnmx knaccax. Cnoxnoc1L a ncnonLsoaannn a1nx c1pyk1yp npnaena
nporpaHHnc1oa k neo6xopnHoc1n paccHo1pe1L anL1epna1nanme aapnan1m.
Unknm uac1o cospak1cn uepes c1pyk1ypm, nopo6nme n1epa1opaH (cH. npnaepennoe amue
peuenne a1o npo6neHm a nsmke Objective-C). Opnako, kak n a cnyuae qynkunn
firstElement, pesynL1a1oH pa6o1m n1epa1opa Hoxe1 6m1L 1onLko snauenne 1nna ListElement.
O6nsannoc1Lk nporpaHHnc1a nanne1cn npnaec1n ero k ppyroHy 1nny pannmx:
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; (* cnezyxmee snauenwe wiepaiopa *)
end;
[o 1oro kak 6mna aaepena cnc1eHa RTTI (Run-Time Typing Information npen1nqnkaunn
1nna ao apeHn amnonnennn), a nsmke C++ snauennn o6muno ne snann caoero co6c1aennoro
pnnaHnueckoro 1nna. D1o ycnoxnnno noc1poenne kon1enepoa, nockonLky nporpaHHnc1
ponxen 6mn ne 1onLko npnaopn1L 1nn, no n o6ecneunaa1L co6c1aennm HexannsH
onpepenennn pnnaHnueckoro 1nna snauenn. Hepaanee nonanenne qynkunn dynamic_cast
npnsaano peun1L nHenno a1y npo6neHy.
converted to PDF by BoJIoc
15.4. Ckpm1oe npuaepeuue 1una pauumx
npu uacnepoaauuu
HpnnunnnanLnme cnoxnoc1n a ncnonLsoaannn 1exnnkn, onncanno a npepmpyueH paspene,
coc1on1 a cnepykueH:
C1pyk1ypa pannmx Hoxe1 xpann1L 1onLko 1e snauennn, ko1opme o1nocn1cn k
nopknaccy knacca ListElement.
Hsmk nporpaHHnpoaannn ponxen noppepxnaa1L npnnunn nopc1anoakn.
O6Lek1m o6nsanm sna1L cao co6c1aennm pnnaHnueckn 1nn.
Hpn nsaneuennn pannmx 1pe6ye1cn kak nanan npoaepka 1nna, 1ak n onepaunn
npnaepennn 1nna.
Hpnaepenne 1nna a1o poaonLno onacnan konc1pykunn, ko1opyk npn nporpaHHnpoaannn
cnepye1 ns6era1L rpe 1onLko aosHoxno. Bpo6aaok npn cospannn a6c1pakun pannmx a C++
cyuec1aenno c1anoan1cn a1opan 1pypnoc1L. BcnoHnnH, u1o C++ ne noppepxnaae1
nopc1anoaky pnn o6Lek1oa, o6Lnanennmx o6munmH o6pasoH (noppepxka ocyuec1anne1cn
1onLko pnn ykasa1ene n ccmnok). Ho a1o npnunne Hnorne c1pyk1ypm pannmx a nsmke C++
paspa6o1anm 1ak, u1o6m xpann1L ykasa1enn na snauennn, a ne caHn snauennn.
C1anpap1no npepnaraeHan 1exnnka nporpaHHnpoaannn ncnonLsye1 pouepnne knaccm n
nacnepoaanne, u1o6m saHacknpoaa1L neo6xopnHoc1L npnaepennn 1nna a 1ako cn1yaunn.
HpepnonoxnH, nanpnHep, u1o Hm yxe nHeeH a6c1pakunk pannmx co cnepykunH
nn1epqecoH:
class GenericList // cnwcox yxasaienen omero swza void *
{
public:
void addToList (void * newElement);
void * firstElement();
private:
GenericLink * firstLink;
};
class GenericLink
{
public:
void * value;
GenericLink * nextLink;
};
Coo1ae1c1aenno nau cnncok o6uero anpa copepxn1 ykasa1enn void. Onn Hory1 ykasmaa1L
na u1o yropno. B 1eopnn 1akan coaokynnoc1L Hoxe1 6m1L ckonL yropno pasnopopno npn
ncnonLsoaannn ykasa1ene na o6Lek1m pasnnunoro 1nna. TenepL npepnonoxnH, u1o Hm
xo1nH cospa1L cnncok ykasa1ene na okna (c1pyk1ypa Window). Epnnc1aennoe, u1o napo
cpena1L, a1o onpepenn1L nopknacc knacca o6uero anpa n nsHenn1L 1nnm apryHen1oa n
pesynL1a1a a He1opax, aosapauakunx aneHen1 cnncka. B nk6oH cnyuae qak1nueckyk
pa6o1y amnonnne1 popn1enLckn knacc.
class WindowList : public GenericList
{
public:
void addToList (Window * newElement)
{
GenericList::addToList (newElement);
}
Window * firstElement ()
converted to PDF by BoJIoc
{
return (Window *) GenericList::firstElement;
}
};
TaknH cnoco6oH ypae1cn cospa1L c1pyk1ypm pannmx, ko1opme xpann1 snauennn, o1nnunme
o1 ykasa1ene (uenme n aeuec1aennme uncna). Ho peannsaunn 1pe6ye1 onpepenennn
nopknaccoa kak pnn knacca List, 1ak n pnn knacca Link, a 1akxe, aepon1no, cospannn noamx
knaccoa-n1epa1opoa.
Mm poc1nrnn po neko1opo c1enenn Hnorokpa1noro ncnonLsoaannn kopa, no 1onLko sa cue1
1oro, u1o sac1aannn nporpaHHnc1a aaopn1L noame nopknaccm acnkn pas, korpa on xoue1
npnHenn1L a6c1pakunk pannmx. Mnorne nporpaHHnc1m o1aeprak1 1akoe peuenne npoc1o
no1oHy, u1o ono poc1aanne1 ne HenLue xnono1, ueH nanncanne c1pyk1yp pannmx c nynn.
15.5. HapaHe1pusoaauume knaccm
Hocnepnn nau 1esnc coc1onn a 1oH, u1o (no kpane Hepe pnn nsmkoa co c1pornH
kon1poneH 1nna pannmx) caHo no ce6e nacnepoaanne nepoc1a1ouno pnn cospannn npoc1mx a
ncnonLsoaannn kon1enepnmx knaccoa. [onxen npnHenn1Lcn ppyro HexannsH. Tako
HexannsH cyuec1aye1. On saknkuae1cn a onpepenennn knacca c 1nnoH a kauec1ae
napaHe1pa. Takne knaccm nasmaak1cn ua6nonaHn a C++ n o6o6uennmHn knaccaHn a
neko1opmx ppyrnx nsmkax.
La6nonm pak1 nporpaHHnc1y aosHoxnoc1L onpepenn1L 1nnm pannmx, a ko1opmx
nnqopHaunn o 1nne npepnaHepenno oc1ae1cn nesapanno. D1o1 npo6en sanonnne1cn
nospnee. 1o6m nyuue nonn1L napaHe1pnsaunk, npepc1aaL1e ce6e, u1o onncannk knacca
1nn noc1aanne1cn kak apryHen1 npouepypm nnn qynkunn. Touno 1ak xe, kak npn amsoae
qynkunn e Hory1 nepepaaa1Lcn pasnnunme snauennn uepes cnncok apryHen1oa, 1ak n
pasnme aonnouennn napaHe1pnsoaannoro knacca nonyuak1 nnqopHaunk o 1nne-
napaHe1pe.
HapaHe1pnsoaannoe onncanne knacca pnn a6c1pakunn cansnoro cnncka sanncmaae1cn a C++
cnepykunH o6pasoH:
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 *);
};
Bny1pn ua6nona knacca apryHen1 ua6nona (a pannoH cnyuae npen1nqnka1op T) Hoxe1
ncnonLsoaa1Lcn kak nHn 1nna pannmx. Coo1ae1c1aenno, paspeuae1cn onpepenn1L
nepeHennme 1nna T, aaopn1L qynkunn, aosapauakune pesynL1a1 1nna T, n 1. p.
Oynkunn-unenm, ko1opme onpepennk1 He1opm a ua6none knacca, ponxnm 1akxe
onncmaa1Lcn kak ua6nonm:
converted to PDF by BoJIoc
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)
{ }
HonLsoaa1enL cospae1 pasnnunme anpm cnnckoa, ykasmaan konkpe1nme 1nnm. HanpnHep,
cnepykune onepa1opm onpepennk1 cnnckn uenmx n aeuec1aennmx uncen:
List listOne;
List listTwo;
TaknH cnoco6oH Hory1 6m1L cospanm 1onLko opnopopnme cnnckn.
La6nonm aneran1noe peuenne npo6neHm kon1enepnmx knaccoa. Onn nosaonnk1
poc1nuL nc1nnnoro Hnorokpa1noro ncnonLsoaannn, cospaaa1L n o6pa6a1maa1L koHnonen1m
o6uero nasnauennn c HnnnHyHoH cnoxnoc1e, a 1akxe rapan1npoaa1L 6esonacnoc1L npn
o6pauennn c 1nnaHn, u1o nanne1cn uenLk nsmkoa nporpaHHnpoaannn co c1pornH kon1poneH
1nnoa pannmx.
Hepoc1a1kn nHek1cn n y ua6nonoa. Onn ne nosaonnk1 onpepenn1L cnnckn pasnopopnmx
pannmx, nockonLky ace aneHen1m ponxnm coo1ae1c1aoaa1L o6LnanennoHy 1nny. (D1y
npo6neHy Hoxno o6o1n sa cue1 xpanennn ykasa1ene na snauennn aHec1o caHnx
snauenn.) Bonee aaxnm nepoc1a1ok: peannsaunn ua6nonoa cnnLno aapLnpye1cn a
o1nouennn kak nerkoc1n ncnonLsoaannn, 1ak n kauec1aa nonyuaeHoro kopa a pasnnunmx
koHnnnn1opax. BonLunnc1ao ns nnx ne penak1 nnuero, kpoHe nn1epnpe1aunn ua6nonoa a
anpe cnoxnmx Hakpocoa, 1ak u1o pnn kaxporo noaoro napaHe1pa-1nna cospae1cn
coaepuenno noaoe onpepenenne knacca n nonnoc1Lk nesaancnHme 1ena He1opoa. He nyxno
roaopn1L, u1o a1o npnaopn1 k snaun1enLnoHy yaennuennk pasHepa kopa.
TeH ne Henee, nockonLky ua6nonm ocao6oxpak1 nporpaHHnc1a o1 6onLuoro konnuec1aa
nypno pa6o1m (a nHenno o1 nepenncmaannn knaccoa pnn noamx c1pyk1yp pannmx a
ouepepnoH npnnoxennn), onn nonLsyk1cn 6onLuo nonynnpnoc1Lk. B cnepykue rnaae Hm
paccHo1pnH 6n6nno1eky ua6nonoa.
15.5.1. uknm u u1epauu a C++
Cyuec1aoaanne HexannsHa ua6nonoa kak pnn knaccoa, 1ak n pnn nnpnanpyanLnmx qynkun
nosaonne1 onpepenn1L ne opnn, a paa pasnnunmx cnoco6a n1epaun a C++. O6a onn
aknkuenm a nepaano paspa6o1annyk c1anpap1nyk 6n6nno1eky ua6nonoa pnn C++, ko1opan
paccHa1pnaae1cn a rnaae 16.
Hepam cnoco6 ncnonLsye1 n1epa1op. Kon1enepnm knacc onpepenne1 1nn pannmx pnn
n1epa1opa, a 1akxe qynkunn, ko1opme aosapauak1 snauenne n1epa1opa. HanpnHep,
n1epa1op pnn nauero knacca cansnmx cnnckoa sanncmaae1cn cnepykunH o6pasoH:
template
class List
converted to PDF by BoJIoc
{
public:
typedef ListIterator iterator;
...
iterator begin()
{
// sepnw mne wiepaiop s wcxoznom cocionnww
return ListIterator (firstLink);
}
iterator end()
{
// xoneu
return ListIterator (0);
}
}
template
class ListIterator
{
public:
ListIterator (Link * sl) : currentLink(sl)
{ }
void operator ++ ()
{
// nepexoz x cnezyxmemy onemeniy
currentLink = currentLink->nextLink;
}
T operator * ()
{
// sepnyis iexymwn onemeni
return currentLink->value;
}
bool operator == (ListIterator & right)
{
return currentLink == right.currentLink;
}
private:
Link * currentLink;
};
TenepL n1epa1op Hoxe1 6m1L o6Lnanen n nnnunannsnpoaan sapannmH cnnckoH.
HoaneHen1nm unkn amnonnne1cn 6es snannn any1penne c1pyk1ypm cnncka:
List::iterator start = aList.begin();
List::iterator end = aList.end();
for (; itr != end; itr++)
{
cout << (*itr) << endl;
};
B1opo anp n1epaun, nasmaaeHm nnorpa n1epaunnHn c npnHenenneH, a neko1opoH
o1nouennn noxox na unknm a nsmke Smalltalk. Hpn 1ako n1epaunn kon1enep nonyuae1
qynkunk a kauec1ae apryHen1a, n on caH npnHenne1 ee k kaxpoHy aneHen1y coaokynnoc1n.
D1n paa pec1ann o6Lepnnnk1cn a qynkunn for_each, ko1opan npnHenne1 nepepaaaeHyk
kak apryHen1 qynkunk k kaxpoHy aneHen1y cnncka:
void printOut(int n)
{
cout << "the collection contains a " << n << "\n";
}
converted to PDF by BoJIoc
...
for_each (aList.begin(), aList.end(), printOut);
Hpo6neHa c n1epaunnHn 1akoro 1nna coc1on1 a 1oH, u1o onn 1pe6yk1 cospannn nnn
ncnonLsoaannn qynkunn, nepepaaaeHo a kauec1ae apryHen1a. Ecnn kon1enep nanne1cn
unc1o nokanLnmH o6Lek1oH, 1akyk qynkunk sauac1yk poaonLno cnoxno onpepenn1L. B
1aknx cn1yaunnx unkn c ncnonLsoaanneH n1epa1opa Hoxe1 6m1L npoue.
Ynpaxueuun
1. Kon1enepnme knaccm a o6Lek1no-opnen1npoaannoH nporpaHHnpoaannn: ycnex nnn
neypaua?
2. C1pyk1ypm pannmx paspennk1cn na 1e, ko1opme xapak1epnsyk1cn caoe
peannsaune (cansnme cnnckn, pepeaLn), n na 1e, ko1opme onpepennk1cn nx
npepnasnauenneH (c1ekn, Hnoxec1aa). Onnun1e, kak Hoxno ncnonLsoaa1L OOH pnn
ynpouennn a1oporo 1nna c1pyk1yp n Hacknpoakn pe1ane nx peannsaunn.
Hpnaepn1e a kauec1ae nnnkc1paunn c1pyk1ypy pannmx c epnnmH nn1epqecoH n
payHn papnkanLno o1nnuakunHncn peannsaunnHn.
3. [a1e npnHep pasnopopnoro kon1enepa, 1o ec1L kon1enepa snauenn pasnnunoro
1nna.
4. Hopxop Smalltalk k noc1poennk n1epaun coc1on1 a 1oH, u1o6m crpynnnpoaa1L
onepaunn, ko1opme napo amnonnn1L, a 6nok n nepepa1L ero c1pyk1ype pannmx.
Hanpo1na, n1epa1op a1o c1pyk1ypa, ko1opan nepepae1 pannme opno sa ppyrnH
aneune npouepype, rpe c nnHn amnonnnk1cn nyxnme pec1ann. Moxno nn
peannsoaa1L nopxop Smalltalk a ppyrnx nsmkax nporpaHHnpoaannn (Object Pascal,
C++)? Kak npn a1oH ckasmaae1cn c1porn kon1ponL 1nnoa?
5. Hpnaepn1e npnHep ua6nonoa, ne cansannmx c kon1enepnmHn knaccaHn.
Fnaaa 16

HpuHep: STL
Bora1an konnekunn c1pyk1yp pannmx, ocnoaannan na ncnonLsoaannn ua6nonoa, nepaano
6mna po6aanena k c1anpap1no 6n6nno1eke nsmka C++. D1n c1pyk1ypm copepxa1 knaccm
pnn aek1opoa, cnnckoa, Hnoxec1a, kap1 (cnoaape), c1ekoa, ouepepe, ouepepe c
npnopn1e1aHn. Korpa a1o1 c1anpap1 c1ane1 pacnpoc1panen noaceHec1no, nporpaHHnc1m na
nsmke C++ 6ypy1 ocao6oxpenm o1 neo6xopnHoc1n noc1onnno nepeonpepenn1L n noa1opno
peannsoamaa1L opnn n 1o1 xe na6op knaccoa pnn c1pyk1yp pannmx. Bonee noppo6nan
nnqopHaunn o 6n6nno1eke STL (STL Standard Template Library, c1anpap1nan 6n6nno1eka
ua6nonoa) Hoxe1 6m1L napena a pa6o1ax [Musser 1996, Glass 1996].
Cospanne STL nanne1cn pesynL1a1oH Hnorone1nnx nccnepoaann nop pykoaopc1aoH
Anekcanppa C1enanoaa n Menra n ns koHnannn Hewlett-Packard n [aanpa Mkccepa ns
Rensselaer Polytechnic Institute. Cospanne STL apoxnoannnocL ne 1onLko npepuec1aykunHn
o6Lek1no-opnen1npoaannmHn 6n6nno1ekaHn, no n Hnorone1nnH onm1oH pa6o1m ee
cospa1ene a o6nac1n qynkunonanLnmx n pnpek1nanmx nsmkoa nporpaHHnpoaannn (Scheme
n Ada).
Opna ns nan6onee neo6munmx npe a STL a1o o6o6uennme anropn1Hm. Ona sacnyxnaae1
o1penLnoro paccHo1pennn, nockonLky amrnnpn1 amsoaoH o6Lek1no-opnen1npoaannmH
npnnunnaH, ko1opme Hm o6cyxpann po cnx nop, n a 1o xe apeHn nanne1cn nc1ounnkoH
converted to PDF by BoJIoc
orpoHno Houn 6n6nno1ekn STL. O6o6uennme anropn1Hm a STL nanoHnnak1 knaccm-
ua6nonm. Hpen ua6nonoa npnHenne1cn k o1penLnmH qynkunnH. 1o6m nonn1L konuenunk
o6o6uennmx anropn1Hoa, Hm cnepaa ponxnm onnca1L ncnonLsoaanne nnkancynnunn a
6onLunnc1ae o6Lek1nmx 6n6nno1ek.
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne paccHa1pnaae1 nnkancynnunk kak rnaanyk
uenL. Xopouo paspa6o1annm o6Lek1 c1apae1cn nnkancynnpoaa1L ace coc1onnne n
noaepenne, neo6xopnHme pnn amnonnennn sapaun, n a 1o xe apeHn ckpmaae1 kak Hoxno
6onLue pe1ane any1pennero yc1poc1aa. Bo Hnornx npepuec1aykunx o6Lek1no-
opnen1npoaannmx 6n6nno1ekax a1o1 qnnocoqckn nopxop aonnouancn a kon1enepnmx
knaccax, o6napakunx unpoko qynkunonanLnoc1Lk n 6ora1mH nn1epqecoH.
Paspa6o1unkn STL nounn a coaepuenno ppyroH nanpaanennn. Hoaepenne, o6ecneunaaeHoe
nx c1anpap1nmHn koHnonen1aHn, nanne1cn HnnnHanLnmH, nou1n u1o cnap1ancknH. BHec1o
a1oro kaxpm koHnonen1 npepnasnauen pnn qynkunonnpoaannn coaHec1no c 6onLunH
na6opoH o6o6uennmx anropn1Hoa, nHekunxcn a 6n6nno1eke. D1n anropn1Hm ne saancn1 o1
kon1enepoa n noa1oHy Hory1 pa6o1a1L co HnornHn pasnnunmHn 1nnaHn.
O1pennn qynkunonnpoaanne anropn1Hoa o1 kon1enepnmx knaccoa, 6n6nno1eka STL Hnoro
amnrpmaae1 a pasHepe kak a o6LeHe caHo 6n6nno1ekn, 1ak n a renepnpyeHoH kope.
BHec1o 1oro u1o6m py6nnpoaa1L anropn1Hm pnn pkxnnm nnn okono 1oro kon1enepnmx
knaccoa, opno-epnnc1aennoe onncanne 6n6nno1euno qynkunn Hoxe1 ncnonLsoaa1Lcn c
nk6mH kon1enepoH. Bonee 1oro, onncanne a1nx qynkun nanne1cn nac1onLko o6unH, u1o
onn Hory1 npnHenn1Lcn c o6munmHn HaccnaaHn n ykasa1ennHn (a cHmcne nsmka C), n c
ppyrnHn 1nnaHn pannmx.
Hau npnHep nponnnkc1pnpye1 neko1opme ocnoanme caoc1aa c1anpap1no 6n6nno1ekn
ua6nonoa. O6o6uennm anropn1H find naxopn1 nepaoe axoxpenne sapannoro snauennn a
konnekunk. H1epa1opm c1anpap1no 6n6nno1ekn coc1on1 ns nap snauenn, o1Heuakunx
nauano n koneu c1pyk1ypm. Anropn1H find ncnonLsye1 napy n1epa1opoa n nue1 nepaoe
axoxpenne sapannoro snauennn. On onpepenen cnepykunH o6pasoH:
template
InputIterator find (InputIterator first,
InputIterator last,
const T& value)
{
while (first != last && *first != value)
{
++first;
};
return first;
}
Anropn1H 6ype1 pa6o1a1L co c1pyk1ypo nk6oro 1nna, a 1oH uncne n c o6munmHn HaccnaaHn
nsmka C. 1o6m na1n nepaoe axoxpenne uncna 7 a Haccna uenmx, nonLsoaa1enL
amnonnne1 cnepykun kop:
int data[100];
...
int * where;
where = find(data, data+100, 7);
Honck nepaoro snauennn a uenouncnennoH cnncke nnuy1L ne cnoxnee:
list aList;
...
list::iterator where;
where = find(aList.begin(), aList.end(), 7);
B a1o rnaae Hm HoxeH onnca1L 1onLko ocnoanme caoc1aa STL. Cnepykune paa paspena
nocanuenm 6asoamH konuenunnH, ncnonLsyeHmH a 6n6nno1eke, a nHenno n1epa1opaH n
converted to PDF by BoJIoc
o6Lek1aH-qynkunnH. 3a1eH 1pn npnHepa nponnnkc1pnpyk1 a pec1ann kon1enepm n
o6o6uennme anropn1Hm 6n6nno1ekn STL.
16.1. H1epa1opm
H1epa1opm a1o qynpaHen1, na ko1opoH ocnoaano ncnonLsoaanne kon1enepnmx knaccoa
n coo1ae1c1aykunx anropn1Hoa ns c1anpap1no 6n6nno1ekn. A6c1pak1no n1epa1op a1o
npoc1o o6Lek1 1nna ykasa1enn, ko1opm npnHenne1cn pnn npoxopa no aceH aneHen1aH a
kon1enepe.
Hopo6no 1oHy kak ykasa1enn no-pasnoHy ncnonLsyk1cn a 1papnunonnoH
nporpaHHnpoaannn, n1epa1opm npnHennk1cn pnn pasnnunmx uene. H1epa1op Hoxe1
o6osnaua1L konkpe1noe snauenne (kak ykasa1enL ykasmaae1 na konkpe1nyk o6nac1L
naHn1n). C ppyro c1oponm, napa n1epa1opoa Hoxe1 sapaaa1L pnanason snauenn (paa
ykasa1enn o1Heuak1 rpannum nenpepmanoro yuac1ka naHn1n). Opnako a cnyuae n1epa1opoa
onncmaaeHme snauennn pacnonoxenm ppyr sa ppyroH ne qnsnueckn, a nornueckn. D1o
nponcxopn1, nockonLky onn asn1m ns opnoro kon1enepa n, cnepoaa1enLno, cnepyk1 ppyr sa
ppyroH a 1oH nopnpke, kak onn xpannnncL a kon1enepe.
Tpapnunonnme ykasa1enn nnorpa npnnnHak1 snauenne null 1o ec1L ne ykasmaak1 nn na
u1o. H1epa1opm ananornunmH o6pasoH Hory1 ne onpepenn1L kakoe-nn6o konkpe1noe
snauenne. Hopo6no 1oHy, kak nornuecko oun6ko nanne1cn pasmHenoaanne n
ncnonLsoaanne ykasa1enn co snauenneH null, nenLsn pasmHenoamaa1L n npnHenn1L
n1epa1op, ko1opm ne onpepenne1 nnkakoro snauennn.
Korpa a nsmke C++ ncnonLsyk1cn paa ykasa1enn, ko1opme orpannunaak1 o6nac1L naHn1n,
no cornauennk a1opo ykasa1enL ne paccHa1pnaae1cn kak uac1L o6nac1n. HanpnHep,
Haccna c nHeneH x n pnnno 10 nnorpa onncmaae1cn kak sannHakun o6nac1L o1 x po
x+10, xo1n aneHen1 x+10 ne nanne1cn uac1Lk Haccnaa. Ha caHoH pene ykasa1enL x+10
ccmnae1cn na sanpepenLnm aneHen1, 1o ec1L aneHen1, cnepykun sa nocnepnnH
aneHen1oH onncmaaeHoro pnanasona. H1epa1opm onpepennk1 pnanason ananornunmH
o6pasoH. B1opoe snauenne paccHa1pnaae1cn ne kak uac1L onpepenneHoro pnanasona, no kak
sanpepenLnm aneHen1, onncmaakun snauenne, cnepykuee a nocnepoaa1enLnoc1n sa
nocnepnnH snauenneH ns sapannoro pnanasona.
Hopo6no 1papnunonnmH ykasa1ennH, ocnoanoe pec1ane, ko1opoe Hopnqnunpye1 n1epa1op,
a1o nnkpeHen1aunn (onepa1op ++). Korpa nnkpeHen1aunn npnHenne1cn k n1epa1opy,
ko1opm ykasmaae1 na nocnepnee snauenne a nocnepoaa1enLnoc1n, n1epa1op npnnnHae1
onncannoe amue sanpepenLnoe snauenne. Onepa1op pasmHenoaannn (*) ocyuec1anne1
poc1yn k pannmH, onpepenneHmH n1epa1opoH.
[nanason Hoxe1 onncmaa1L aecL kon1enep npn sapannn n1epa1opa, ykasmaakuero na
nepam aneHen1, n n1epa1opa, nHekuero cneunanLnoe nocnepnee snauenne. [nanasonm
Hory1 onncmaa1L nopnocnepoaa1enLnoc1n, axopnune a kon1enep (payH n1epa1opaH
npncaanaak1cn konkpe1nme snauennn). B c1anpap1nmx kon1enepax n1epa1op nauana
kon1enepa aosapauae1cn qynkune begin(), a n1epa1op, o6osnauakun koneu
kon1enepa, qynkune end().
16.2. O61ek1m-qyukuu
Pnp o6o6uennmx anropn1Hoa ns 6n6nno1ekn STL 1pe6ye1 qynkun a kauec1ae apryHen1oa.
Hpoc1mH npnHepoH cnyxn1 o6o6uennm anropn1H for_each(), ko1opm amsmaae1 qynkunk,
nepepannyk a kauec1ae apryHen1a, pnn kaxporo snauennn a kon1enepe. Cnepykun kop
ncnonLsye1cn pnn 1oro, u1o6m amaec1n nonnm na6op snauenn a uenouncnennoH cnncke:
void printElement(int value)
{
cout << "The list contains " << value << endl;
}
converted to PDF by BoJIoc
main ()
{
list aList;
...
for_each(aList.begin(), aList.end(), printElement);
}
Honn1ne qynkunn 6mno o6o6ueno po nonn1nn o6Lek1a-qynkunn. O6Lek1-qynkunn a1o
akseHnnnp knacca, a ko1opoH onpepenen He1op kpyrnme cko6kn (). B pnpe cnyuaea
ypo6no saHenn1L qynkunn na o6Lek1m-qynkunn. Korpa o6Lek1-qynkunn ncnonLsye1cn a
kauec1ae qynkunn, 1o npn ee amsoae ncnonLsye1cn onepa1op kpyrnme cko6kn.
1o6m nponnnkc1pnpoaa1L a1o, paccHo1pnH cnepykuee onpepenenne knacca:
class biggerThanThree
{
public:
bool operator () (int v)
{
return V > 3 ;
}
};
Ecnn Hm cospapnH akseHnnnp knacca biggerThanThree, 1o kaxpm pas, korpa Hm 6ypeH
ccmna1Lcn na nero c ncnonLsoaanneH cnn1akcnca amsoaa qynkunn, 6ype1 amsmaa1Lcn
He1op, coo1ae1c1aykun onepa1opy kpyrnme cko6kn. Cnepykun uar o6o6un1L a1o1
knacc, po6aana k neHy konc1pyk1op n nensHenneHoe none pannmx, ko1opoe
yc1anaannaae1cn konc1pyk1opoH:
class biggerThan
{ public:
biggerThan (int x) : testValue(x) { }
const int testValue;
bool operator () (int val)
{ return val > testValue; }
};
PesynL1a1oH nanne1cn qynkunn o6uero anpa, amnonnnkuan uenouncnennoe cpaanenne
6onLue ueH X, rpe snauenne X onpepenne1cn npn cospannn akseHnnnpa knacca. D1o
Hoxe1 6m1L cpenano, nanpnHep, npn nepepaun opno ns o6o6uennmx qynkun
apryHen1a nornuecko qynkunn. Mm nueH a cnncke nepaoe snauenne, 6onLuee 12, c
noHouLk kopa
list::iterator firstBig = find_if(aList.begin(),
aList.end(),
biggerThan(12));
16.3. HpuHep nporpaHHm: uuaeu1apusaun
Hau nepam npnHep nnnkc1pnpye1 cospanne n o6pa6o1ky o6Lek1oa a 6n6nno1eke STL.
[onyc1nH, qnpHe WorldWideWidgetWorks no1pe6oaanacL nporpaHHa yue1a anpxe1oa.
Bnpxe1m a1o npoc1me yc1poc1aa, pasnnuaeHme no npen1nqnkaunonnmH noHepaH:
class Widget
{
public:
Widget(int a) : id(a) { }
Widget() : id(0) { }
int id;
};
ostream & operator << (osteam & out, Widget & w)
{
converted to PDF by BoJIoc
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;
}
Hnaen1apL onncmaae1cn payHn cnnckaHn. Opnn npepc1aanne1 co6o anpxe1m, nHekunecn a
pannm HoHen1 na cknape. B1opo cnncok copepxn1 1nnm anpxe1oa, ko1opme 6mnn
sakasanm nokyna1ennHn. Hepam cnncok copepxn1 co6c1aenno anpxe1m, a a1opo
npen1nqnkaunonnme 1nnm anpxe1oa. 1o6m ynpaann1L nnaen1apeH, 1pe6yk1cn pae
koHanpm:
order() o6cnyxnaae1 sakasm;
receive() cnepn1 sa noc1aakaHn noamx anpxe1oa.
class inventory
{
public:
void order (int wid);
// opaoixa saxasa swz+eia iwna wid
void receive (int wid);
// nonyuenwe swz+eia iwna wid
private:
list on_hand;
list on_order;
};
Korpa noc1ynae1 noam anpxe1, Hm cpaannaaeH ero npen1nqnkaunonnm noHep co cnnckoH
sakasannmx anpxe1oa. Mm ncnonLsyeH qynkunk find() pnn noncka a cnncke sakasoa n
neHepnenno nepecmnaeH anpxe1 nokyna1enk, ecnn on (anpxe1) 6mn sakasan. B npo1nanoH
cnyuae on po6aanne1cn k cnncky anpxe1oa na cknape.
void inventory::receive(int wid)
{
cout << "Hpwmna napiwn swz+eios iwna" << wid << endl;
list::iterator weNeed = find(on_order.begin(),
on_order_end(),
wid);
if ( weNeed != on_order.end() )
{
cout << "Hociaswis " << Widget(wid)
<< " uio- nononnwis sanac" << endl;
on_order.erase(weNeed);
}
else
{
on_hand.push_front(Widget(wid));
};
}
Korpa nokyna1enL sakasmaae1 noam anpxe1, Hm npocHa1pnaaeH c noHouLk qynkunn
find_if() cnncok nHekunxcn na cknape anpxe1oa, u1o6m onpepenn1L, nenLsn nn o6cnyxn1L
sakas neHepnenno. [nn a1oro onpepenena ynapnan qynkunn, ko1opan 6epe1 a kauec1ae
converted to PDF by BoJIoc
apryHen1a anpxe1 n onpepenne1, coo1ae1c1aye1 nn on 1pe6yeHoHy 1nny. D1a qynkunn
sanncmaae1cn cnepykunH o6pasoH:
class WidgetTester
{ public:
WidgetTester(int t) : testid(t) { }
const int testid;
bool operator () (const Widget & wid)
{ return wid.id == testid;
}
};
Oynkunn, o6cnyxnaakuan sakasm anpxe1oa, amrnnpn1 1ak:
void inventory::order(int wid)
{
cout << "Honyuen saxas na swz+ei- iwna " << wid << endl;
list::iterator weHave = find_if(on_hand.begin(),
on_hand.end(),
WidgetTester(wid));
if ( weHave != on_hand.end() )
{
cout << "Hociaswis " << *weHave << endl;
on_hand.erase(weHave);
}
else
{
cout << "Baxasais swz+ei iwna " << wid << endl;
on_order.push_front(wid);
};
}
16.4. HpuHep nporpaHHm: rpaqm
B1opo n 1pe1n npnHepm ncnonLsyk1 1nn pannmx kap1a. Kap1a map a1o
nnpekcnpoaannm cnoaapL, 1o ec1L na6op nap knku-snauenne.
Kap1a, a ko1opo aneHen1aHn 1oxe nannk1cn kap1m, cnyxn1 ec1ec1aennmH npepc1aanenneH
nanpaanennoro rpaqa. HpepnonoxnH, k npnHepy, u1o Hm ncnonLsyeH 1ekc1oame c1pokn pnn
kopnpoaannn nasaann ropopoa n xo1nH ckonc1pynpoaa1L kap1y, rpe snauennn pe6ep
rpaqa a1o pacc1onnne Hexpy payHn coo1ae1c1aykunHn ropopaHn. Tako rpaq nokasan
na pnc. 16.1.

Puc. 16.1. Bsaeuennm rpaq


converted to PDF by BoJIoc
Ruc1uur 16.1. Onepa1opm nnnunannsaunn rpaqa

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;

Tnn pannmx stringVector a1o aek1op uenmx uncen, nnpekcnpoaannm c1pokaHn. Tnn
pannmx graph a1o payHepnm paspexennm Haccna, nnpekcnpoaannm c1pokaHn n
copepxaun uenme uncna. Hocnepoaa1enLnoc1L onepa1opoa npncaanaannn nnnunannsnpye1
rpaq. Ipaq, nokasannm na pnc. 16.1, peannsoaan a nnc1nnre 16.1.
Moxno ncnonLsoaa1L na6op knaccnuecknx anropn1Hoa pnn o6pa6o1kn 1aknx rpaqoa. OpnnH
ns npnHepoa nanne1cn anropn1H [ekc1pm noncka kpa1uauero ny1n. Tpe6ye1cn ykasa1L
ropop, c ko1oporo naunnae1cn ny1L. Hpn a1oH cospae1cn npnopn1e1nan ouepepL pnn nap
pacc1onnne/ropop. Ona nnnunannsnpye1cn nyneH pnn nauanLnoro ropopa. Hpnopn1e1nan
ouepepL copepxn1 ropopa a nopnpke o1 6nnxauero po caHoro panLnero. Onpepenenne
1nna pannmx DistancePair amrnnpn1 cnepykunH o6pasoH:
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; }
Hpn kaxpo n1epaunn unkna Hm nsanekaeH ns ouepepn ropop. Ecnn pnn nero eue ne
napeno kpa1uauero ny1n, sanncmaae1cn 1ekyuee pacc1onnne n ny1eH npoaepkn rpaqa
amuncnne1cn pacc1onnne po cocepnnx ropopoa. D1o1 npouecc npoponxae1cn po 1ex nop,
noka ne 6ype1 ncuepnana npnopn1e1nan ouepepL.
void shortestDistance(const graph & cityMap,
const string & start,
stringVector & distances)
{
// opaoiais npwopwieinyx ouepezs paccionnwn zo ropozos
converted to PDF by BoJIoc
priority_queue,
greater > que;
que.push(DistancePair(0, start));
while (! que.empty() ) {
// s-pais nw+anmwn ropoz ws ouepezw
int distance = que.top().first;
string city = que.top().second;
que.pop();
// ecnw m- eme ne scipeuanw iaxon ropoz, opaoiais ero
if (0 == distances.count(city))
{
// saiem zoaswis ero x xapie
// xpaiuanmwx paccionnwn
distances[city] = distance;
// w saneciw s ouepezs nos-e snauenwn
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. HpuHep nporpaHHm: anqaau1um ykasa1enu
HocnepnnH npnHepoH nanne1cn anqaan1nm ykasa1enL. HporpaHHa ncnonLsye1 cansnme
cnnckn n 1nn pannmx multimap. multimap a1o pasnoanpnoc1L kap1, ko1opan nosaonne1
nnpekcnpoaa1L opnnH knkuoH neckonLko pannmx.
Anqaan1nm ykasa1enL npepc1aanne1 co6o cnncok ynopnpouennmx no anqaan1y cnoa,
ko1opm nokasmaae1 noHepa c1pok, rpe ac1peuae1cn 1o nnn nnoe cnoao. D1n snauennn
xpann1cn a anqaan1noH ykasa1ene a anpe HynL1nkap1m multimap, nnpekcnpoaanno
cnoaaHn (1nn pannmx string). Ee copepxnHoe uenme uncna (noHepa c1pok a 1ekc1e).
MynL1nkap1a ncnonLsye1cn no1oHy, u1o opno n 1o xe cnoao ac1peuae1cn a neckonLknx
Hec1ax. Ha caHoH pene o6napyxenne 1aknx noa1openn rnaanoe npepnasnauenne
anqaan1noro ykasa1enn.
Tnn pannmx pnn anqaan1noro ykasa1enn sapae1cn cnepykunH o6pasoH:
class concordance // anaswin-n yxasaiens
{
typedef multimap wordDictType;
public:
concordance() : wordMap() { }
void addWord (string, int);
void readText(istream &);
void printConcordance(ostream &);
private:
wordDictType wordmap;
};
Cospanne anqaan1noro ykasa1enn pas6naae1cn na paa uara. Cnepaa on renepnpye1cn
nporpaHHo (c1pokn 1ekc1a cun1maak1cn ns axopnoro no1oka), a sa1eH pesynL1a1
neua1ae1cn a amxopno qan. D1n paa uara peannsyk1cn a qynkunnx-unenax readText() n
printConcordance(). Hepaan ns nnx sanncmaae1cn cnepykunH o6pasoH:
void concordance::readText(istream & in)
// cuwiais iexci ws sxoznoro noioxa,
converted to PDF by BoJIoc
// sanocn cnosa s anaswin-n yxasaiens
{
string line;
// uwiais cipoxw sxoznoro noioxa
for (int i=1; getline(in, line); i++)
{
allLower(line); // npeopasosais s nw+nwn perwcip
list words;
split(line, " ,.;:", words); // paswis cipoxy na cnosa
list::iterator wptr; // sneciw cnosa s xonnexuwx
for (wptr = words.begin(); wptr != words.end(); ++wptr)
addWord(*wprt, i);
}
}
C1pokn 1ekc1a cun1maak1cn ns axopnoro no1oka opna sa ppyro. Hx 1ekc1 npexpe acero
npeo6pasye1cn k nnxneHy pernc1py. 3a1eH c1poka pas6naae1cn na cnoaa c noHouLk
qynkunn split(). Ha6op nn1ep-paspenn1ene n ncxopnm 1ekc1 nepepak1cn kak apryHen1m.
D1a qynkunn nnnkc1pnpye1 neko1opme ns aosHoxnoc1e o6pa6o1kn 1ekc1oamx c1pok,
sanoxennmx a 6n6nno1eky STL:
void split(string & text, string & separators,
list & words)
{
int n = text.length();
int start, stop;
// naniw nepsyx yxsy, ne paszenwiens
start = text.find_first_not_of(separator);
while ( (start >= 0) && (start < n) )
{
// naniw xoneu iexymero cnosa
stop = text_find_first_of(separators, start);
if ( (stop < 0) || (stop > n) ) stop = n;
// zoaswis cnoso x cnwcxy cnos
words.push_back(text.substr(start, stop-start));
// naniw nauano cnezyxmero cnosa
start = text.find_first_not_of(separators,stop+1);
}
}
Bcnep sa amsoaoH qynkunn split() kaxpoe napennoe cnoao po6aanne1cn a anqaan1nm
ykasa1enL c noHouLk cnepykuero He1opa:
void concordance::addWord(string word, int line)
{
// nposepwis, ne scipeuaeicn nw cnoso s cnwcxe
// cnauana naniw zwanason sxo+zenwn c ny+n-m xnxuom
wordDictType::iterator low =
wordMap.lower_bound(word);
wordDictType::iterator high =
wordMap.upper_bound(word);
// uwxn no sxo+zenwnm, cooiseicisyxi nw onw cipoxe
for (; low != high; ++low)
if ( (*low).second == line ) return;
// ne scipeiwnacs, zoaswm
wordMap.insert(make_pair(word, line));
}
Me1op addWord() rapan1npye1, u1o snauennn a kap1e cnoa ne py6nnpyk1cn, ecnn kakoe-
nn6o cnoao ac1peuae1cn a opno c1poke paaxpm. D1o poc1nrae1cn 6naropapn npoaepke
converted to PDF by BoJIoc
sannce, ko1opme coo1ae1c1ayk1 knkuy noncka. Kaxpan sanncL npoaepne1cn na coanapenne
noHepoa c1pok, n a cnyuae coanapennn sanncL a ykasa1enL ne po6aanne1cn. TonLko ecnn
unkn npoaepkn saaepuae1cn 6es o6napyxennn sanncn c 1eH xe noHepoH c1pokn, noaan
napa cnoao/noHep c1pokn po6aanne1cn a kap1y.
Hocnepnn uar pacneua1a1L anqaan1nm ykasa1enL. D1o penae1cn cnepykunH o6pasoH:
void concordance::printConcordance(ostream & out)
{
string lastword("");
wordDictType::iterator pairPtr;
wordDictType::iterator stop = wordMap.end();
for(pairPtr = wordMap.begin(); pairPtr != stop;
++pairPtr)
// ecnw cnoso iaxoe +e, uio w npez-zymee,
// npocio naneuaiais nomep cipoxw
if ( lastword == (*pairPtr).first)
out << " " << (*pairPtr).second;
else
// nepsoe sxo+zenwe cnosa
{
lastword = (*pairPtr).first;
out << endl << lastword << ": " <<
(*pairPtr).second;
}
cout << endl; // sasepmwis nocneznxx cipoxy s-soza
}
H1epa1op unknnueckn npoxopn1 no aceH aneHen1aH, copepxaunHcn a cnncke cnoa. Kaxpoe
noaoe cnoao nopoxpae1 noayk c1poky amaopa; cnepykune sa cnoaoH noHepa c1pok
paspennk1cn npo6enaHn. Ecnn, k npnHepy, na axope sapan 1ekc1
It was the best of times,
it was the worst of times.
1o na amxope 6ypy1 naneua1anm cnoaa o1 best po worst:
best: 1
it: 1 2
of: 1 2
the: 1 2
times: 1 2
was: 1 2
worst: 1
16.6. Bypyee OOH
Mm o1Heuann, u1o ao Hnornx o1nouennnx 6n6nno1eka STL ne nanne1cn o6Lek1no-
opnen1npoaanno. Ckopee, ona nanncana nop aospec1aneH qynkunonanLnoro
nporpaHHnpoaannn. Osnauae1 nn aknkuenne STL a cnncok c1anpap1nmx 6n6nno1ek C++,
u1o o6Lek1no-opnen1npoaannoe nporpaHHnpoaanne amxopn1 ns Hopm?
Besycnoano, ne1. O6Lek1no-opnen1npoaannme 1exnnkn npoek1npoaannn n
nporpaHHnpoaannn ne nHek1 ce6e paanmx npn paspa6o1ke 6onLunx cnoxnmx cnc1eH. B
6onLunnc1ae sapau nporpaHHnpoaannn OOH 6ype1 oc1aaa1Lcn rnaaenc1aykunH. Opnako
paspa6o1ka STL (n ne 1onLko) nokasmaae1 xenannme npnsnakn 1oro, u1o coo6uec1ao
o6Lek1no-opnen1npoaannmx nporpaHHnc1oa npnsnae1, u1o ne ace npen ponxnm nHe1L
ampaxenne a o6Lek1no-opnen1npoaannoH c1nne, paano kak n ne ace npo6neHm o6nsanm
peua1Lcn c noHouLk o6Lek1no-opnen1npoaanno 1exnnkn.

converted to PDF by BoJIoc
Ynpaxueuun
1. Hpepnonoxn1e, u1o nHee1cn nenocpepc1aennan peannsaunn knacca nnneno
c1pyk1ypm pannmx (nanpnHep, knacca cansnmx cnnckoa ns rnaam 15). Onnun1e
ocnoanme caoc1aa knacca-n1epa1opa pnn a1o c1pyk1ypm. 3a kako nnqopHaune
ponxen cnepn1L aau n1epa1op?
2. TenepL paccHo1pn1e nennnenyk c1pyk1ypy pannmx (nanpnHep, paonunoe pepeao).
Kakyk nnqopHaunk ponxen noppepxnaa1L n1epa1op pnn 1oro, u1o6m ampaaa1L
aneHen1m, copepxaunecn a kon1enepe?
Fnaaa 17

BupuHoc1u u saaucuHoc1u
B rnaae 1 Hm amncnnnn, u1o asanHosaancnHoc1L nporpaHHnmx koHnonen1 nanne1cn
ocnoanmH npenn1c1aneH na ny1n paspa6o1kn Hnorokpa1no ncnonLsyeHoro kopa. D1o1 qak1
paano npnsnan coo6uec1aoH paspa6o1unkoa. HHee1cn nn1epa1ypa, nocanuennan
xapak1epnc1nke asanHosaancnHoc1n koHnonen1, rpe npnaepenm npaanna, kak ns6era1L
apepnmx canse (cH., k npnHepy, [Gillet 1982] n [Fairley 1885]). B a1o rnaae Hm
nccnepyeH neko1opme ns a1nx coo6paxenn a kon1ekc1e o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn.
BypeH onncmaa1L asanHocansn a 1epHnnax anpnHoc1n n saancnHoc1n. BnpnHoc1L onncmaae1
neko1opyk xapak1epnc1nky nHen o6Lek1oa. O6Lek1 nanne1cn anpnHmH a neko1opoH
kon1ekc1e, ecnn ero nHn nanne1cn npaannLnmH n o6osnauae1 o6Lek1. Bnnskn 1epHnn,
uac1o ncnonLsyeHm pnn onncannn anpnHoc1n, a1o o6nac1L anpnHoc1n npen1nqnka1opa.
BnpnHoc1L 1ecno cansana c asanHosaancnHoc1Lk: ynpaannn anpnHoc1Lk nHenn
npen1nqnka1opa, Hm HoxeH 6onee ue1ko oxapak1epnsoaa1L ncnonLsoaanne
npen1nqnka1opa. B nsmke Smalltalk, k npnHepy, anpnHoc1L nepeHennmx akseHnnnpa
orpannuena He1opaHn k 1aknH nepeHennmH neaosHoxen nenocpepc1aennm poc1yn ane
He1opa. D1o ne osnauae1, u1o onn ne Hory1 nsHenn1Lcn nnn cun1maa1Lcn nsane knacca. Bce
1akne pec1ann, opnako, ponxnm npoaopn1Lcn npn nocpepnnuec1ae He1opa. C ppyro
c1oponm, a nsmke Object Pascal aepcnn Apple nepeHennme akseHnnnpa anpnm ackpy, rpe
nsaec1no nHn coo1ae1c1aykuero knacca. To ec1L a1o1 nsmk ne o6ecneunaae1 HexannsHoa,
rapan1npykunx, u1o nepeHennme akseHnnnpa Hopnqnunpyk1cn 1onLko He1opaHn. BHec1o
a1oro naH npnxopn1cn nonara1Lcn na napnexauee noaepenne nonLsoaa1ene.
Honn1ne saancnHoc1n coo1nocn1 pasnnunme uac1n npnnoxennn. Ecnn nporpaHHnan cnc1eHa
(knacc, HopynL n 1. p.) ne Hoxe1 ocHmcnenno cyuec1aoaa1L a o1pmae o1 ppyro cnc1eHm,
roaopn1, u1o nepaan cnc1eHa saancn1 o1 a1opo. HanpnHep, pouepnn knacc nou1n acerpa
saancn1 o1 caoero popn1enn. 3aancnHoc1n Hory1 6m1L n ropaspo 6onee 1onknHn, kak Hm
yanpnH a cnepykueH paspene.
17.1. 3aenneuue u cansuoc1u
Honn1nn sauennennn n cansnoc1n 6mnn aaepenm C1naencoH, Konc1an1anoH n MaepcoH
[Stevens 1981] pnn ouenkn aqqek1nanoro ncnonLsoaannn Hopyne. Mm 6ypeH o6cyxpa1L
nx, nHen a anpy nsmkn, noppepxnaakune Hopynn, n 1onLko sa1eH nepepeH k o6Lek1no-
opnen1npoaannmH nsmkaH.
3auennenne onncmaae1 o1nouennn Hexpy HopynnHn, a cansnoc1L any1pn nnx.
YHenLuenne asanHosaancnHoc1n Hexpy HopynnHn (nnn knaccaHn) poc1nrae1cn,
converted to PDF by BoJIoc
cnepoaa1enLno, sa cue1 yHenLuennn sauennennn. C ppyro c1oponm, xopouo
paspa6o1annme Hopynn ponxnm cnyxn1L neko1opo uenn, 1o ec1L ace aneHen1m Hopynn
ponxnm 6m1L cansanm o6ue sapaue. D1o osnauae1, u1o xopouo paspa6o1annm HopynL
ponxen 6m1L any1penne cansnmH.
17.1.1. Pasuoaupuoc1u saenneuun
3auennenne Hexpy HopynnHn aosnnkae1 no HnornH npnunnaH. Heko1opme ns nnx nannk1cn
6onee npneHneHmHn nnn xena1enLnmHn, ueH ppyrne. Ynopnpouennm cnncok npnunn
amrnnpn1 npnHepno cnepykunH o6pasoH:
sauennenne any1pennnx pannmx;
sauennenne no rno6anLnmH pannmH;
sauennenne npn ynpaanennn;
sauennenne ns-sa napaHe1poa;
sauennenne nopknaccoa1.
3auennenne no any1pennnH pannmH nponcxopn1, korpa opnn ns Hopyne nsHenne1
nokanLnme pannme a ppyroH Hopyne. D1a pen1enLnoc1L sa1pypnne1 nonnHanne n ocosnanne
cHmcna nporpaHHm, n ee cnepye1 ns6era1L, rpe 1onLko aosHoxno. B opnoH ns cnepykunx
paspenoa Hm paccHo1pnH aapnc1nueckn nopxop, ko1opm ncnonLsye1cn pnn yHenLuennn
sauennennn any1pennnx pannmx a o6Lek1no-opnen1npoaannmx cnc1eHax.
3auennenne no rno6anLnmH pannmH nponcxopn1, korpa paa Hopynn cansanm uepes o6une
rno6anLnme c1pyk1ypm pannmx. Onn1L-1akn, a1o ycnoxnne1 nonnHanne Hopyne npn
nsonnpoaannoH nx paccHo1pennn, no nnorpa 1akoe sauennenne nens6exno.
Ha npak1nke aaxno pasnnua1L paa anpa rno6anLnmx nepeHennmx. Bo Hnornx nporpaHHax
neko1opme rno6anLnme nepeHennme nHek1 o6nac1L anpnHoc1n, orpannuennyk 1ekyunH
qanoH (file scope), cnepoaa1enLno, onn ncnonLsyk1cn 1onLko a npepenax opnoro qana.
[pyrne rno6anLnme nepeHennme anpnm ao ace nporpaHHe (program scope), n, snaun1,
no1enunanLno onn Hory1 Hopnqnunpoaa1Lcn rpe yropno. HonnHanne cHmcna rno6anLnmx
nepeHennmx, anpnHmx ao ace nporpaHHe, 1pypnee, ueH amncnenne npepnasnauennn
nepeHennmx, poc1ynnmx 1onLko a npepenax opnoro qana.
B paHkax o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn anL1epna1nao sauennennk no
rno6anLnmH pannmH nanne1cn cospanne noaoro knacca, uLe o6nsannoc1Lk nanne1cn
apHnnnc1pnpoaanne nyxnmx pannmx. 3a1eH Hoxno saHenn1L ace o6pauennn k rno6anLnmH
pannmH na amsoam He1opoa knacca-apHnnnc1pa1opa. (D1o1 nopxop nanoHnnae1
ncnonLsoaanne qynkun poc1yna pnn saun1m nokanLnmx pannmx any1pn o6Lek1a.) Tako
nopxop caopn1 sauennenne no rno6anLnmH pannmH k sauennennk no napaHe1paH, u1o nerue
pnn nonnHannn n kon1ponn. B nsmke Java ne1 rno6anLnmx nepeHennmx, n ace snauennn
ponxnm ynpaann1Lcn neko1opmH knaccoH.
3auennenne no ynpaanennk nponcxopn1, korpa opnn HopynL ponxen amnonnn1L nekne
onepaunn a nopnpke, ko1opm onpepenne1cn ppyrnH HopyneH. HanpnHep, cnc1eHa
ynpaanennn 6aso pannmx ponxna nnnunannsnpoaa1Lcn, cun1a1L 1ekyune sanncn, o6noan1L
nx, ypann1L uac1L sannce, cospa1L o1ue1. Opnako kaxpoe pec1ane ocyuec1anne1cn
o1penLno npouepypo, n nocnepoaa1enLnoc1L amsoaoa Hoxe1 saance1L o1 kopa a ppyroH
Hopyne. Hannune sauennennn no ynpaanennk nokasmaae1, u1o paspa6o1unk Hopynn
pykoaopc1aoaancn 6onee nnsknH ypoaneH a6c1parnpoaannn, ueH 1pe6ye1cn (1o ec1L
epnnc1aennan pnpek1naa o6pa6o1a1L 6asy pannmx pas6naae1cn na uarn). [axe ecnn
sauennenne no ynpaanennk nens6exno, sppaam cHmcn nopckasmaae1, u1o6m paspenennm
na a1anm HopynL caH rapan1npoaan, u1o onn amnonnnk1cn a ponxnoH nopnpke (a ne
nonarancn 6m a a1oH aonpoce na 6naropasyHne co c1oponm amsmaakunx 6nokoa).
converted to PDF by BoJIoc
3auennenne no napaHe1paH nponcxopn1, korpa opnn HopynL ponxen nonLsoaa1Lcn ycnyraHn
n npouepypaHn ppyroro Hopynn, n npn a1oH epnnc1aennan cansL Hexpy
nnHn ocyuec1anne1cn uepes axopnme n amxopnme napaHe1pm. Takan qopHa sauennennn
nanne1cn 1nnnuno, npoc1o pnn o1cnexnaannn n nerko pnn c1a1nuecko npoaepkn
(nanpnHep, napaHe1pm npn amsoae cpaannaak1cn c onpepenenneH qynkunn). To ec1L a1o
nan6onee xena1enLnm aapnan1 sauennennn.
3auennenne uepes nopknaccm cneunqnuno pnn o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn. Ono onncmaae1 o1nouennn knacca co caonH popn1eneH (nnn
popn1ennHn a cnyuae Hnoxec1aennoro nacnepoaannn). 3a cue1 nacnepoaannn akseHnnnp
pouepnero knacca Hoxe1 paccHa1pnaa1Lcn kak npnnapnexaun popn1enLckoHy knaccy. Kak
Hm anpenn a neckonLknx yue6nmx npnHepax a knnre, pannoe caoc1ao nosaonne1
paspa6a1maa1L o6LeHnme nporpaHHnme koHnonen1m (cnc1eHm c okonnmH nn1epqecoH),
ko1opme cna6o coo1nocn1cn (sa cue1 sauennennn uepes nopknaccm) c ppyrnHn paspenaHn
nporpaHHm.
17.1.2. Pasuoaupuoc1u cansuoc1u
Bny1pennnn cansnoc1L Hopynn a1o Hepa cuennennn ppyr c ppyroH pasnnunmx aneHen1oa
any1pn Hopynn. Kak n a cnyuae sauennennn, cansnoc1L Hoxe1 6m1L panxnpoaana no ukane
o1 cna6o (nanHenee xena1enLno) po cnnLno (caHo xena1enLno) cnepykunH o6pasoH:
cansnoc1L no coaHeuennk;
nornueckan cansnoc1L;
apeHennan cansnoc1L;
koHHynnkaunonnan cansnoc1L;
nocnepoaa1enLnan cansnoc1L;
qynkunonanLnan cansnoc1L;
cansnoc1L pannmx.
Cansnoc1L no coaHeuennk osnauae1, u1o aneHen1m Hopynn rpynnnpyk1cn 6es anpnHo
npnunnm uac1o kak pesynL1a1 nponsaonLnoro pas6nennn na Hopynn 6onLuo
nporpaHHm. O6muno a1o npnsnak nnoxo paspa6o1kn. B o6Lek1no-opnen1npoaannoH
nopxope Hm roaopnH, u1o nHee1 Hec1o cansnoc1L no coaHeuennk, korpa knacc coc1on1 ns
He1opoa, ne nHekunx Hexpy co6o nnuero o6uero.
ornueckan cansnoc1L aosnnkae1, korpa nHee1cn nornueckan cansL Hexpy aneHen1aHn
Hopynn (nnn He1opaHn knacca), no ne1 qak1nueckoro coepnnennn nn no pannmH, nn no
ynpaanennk. Bn6nno1eka Ha1eHa1nuecknx qynkun (cnnyc, kocnnyc n 1. p.) cnyxn1
npnHepoH nornuecko cansnoc1n, ecnn kaxpan ns qynkun sakopnpoaana o1penLno, 1o ec1L
6es cansn c ppyrnHn.
BpeHennan cansnoc1L aosnnkae1, korpa aneHen1m o6Lepnnnk1cn aHec1e, 1ak kak ace onn
ponxnm ncnonLsoaa1Lcn npnHepno opnoapeHenno. MopynL nnnunannsaunn nporpaHHm
nanne1cn 1nnnunmH npnHepoH. B a1oH cnyuae 6onee ypaunan paspa6o1ka Horna 6m
pacnpepenn1L pasnnunme nnnunannsnpykune onepaunn no neckonLknH HopynnH, ko1opme
a 6onLue c1enenn o1aeuak1 sa coo1ae1c1aykune pec1ann.
KoHHynnkaunonnan cansnoc1L aosnnkae1, korpa aneHen1m (nnn He1opm knacca)
o6Lepnnenm a HopynL, nockonLky onn nHek1 poc1yn k opnnH n 1eH xe yc1poc1aaH
aaopa/amaopa. MopynL pa6o1ae1 kak apHnnnc1pa1op yc1poc1aa.
converted to PDF by BoJIoc
Hocnepoaa1enLnan cansnoc1L aosnnkae1, ecnn aneHen1m Hopynn ponxnm ak1nansnpoaa1Lcn
a onpepenennoH nopnpke. D1a cansnoc1L uac1o nanne1cn cnepc1aneH nonm1kn ns6exa1L
sauennennn no ynpaanennk. Onn1L-1akn, o6muno naxopn1cn nyuuan cxeHa, ecnn nopnn1L
ypoaenL a6c1pakunn. (Koneuno xe, neo6xopnHoc1L amnonnn1L pec1ann a onpepenennoH
nopnpke ponxna 6m1L ampaxena na neko1opoH ypoane a6c1pakunn. Baxno ckpm1L a1y
neo6xopnHoc1L o1 ppyrnx ypoane a6c1pakunn.)
OynkunonanLnan cansnoc1L xena1enLna. Hpn ee nannunn ace aneHen1m Hopynn cansanm
amnonnenneH epnno sapaun.
Hakoneu, cansnoc1L na ypoane pannmx aosnnkae1 a Hopyne, korpa on any1pennnH o6pasoH
onpepenne1 na6op pannmx n akcnop1npye1 nopnporpaHHm (npouepypm, qynkunn, He1opm),
ko1opme Hannnynnpyk1 a1o c1pyk1ypo pannmx. Cansnoc1L no pannmH aosnnkae1, ecnn
HopynL ncnonLsye1cn pnn peannsaunn a6c1pak1noro 1nna pannmx.
ac1o Hoxno ouenn1L c1enenL cansnoc1n Hopynn, ecnn kpa1ko cqopHynnpoaa1L
npepnoxenne, onncmaakuee ero npepnasnauenne (acnoHnn1e CRC-kap1oukn ns rnaam 2).
Cnepykun na6op 1ec1oa 6mn npepnoxen Konc1an1anoH:
1. Ecnn npepnoxenne, ko1opoe onncmaae1 npepnasnauenne Hopynn, coc1aanoe, 1o ec1L
copepxn1 sann1yk n 6onee opnoro rnarona, 1o HopynL (ckopee acero) amnonnne1
6onee opno qynkunn. Bepon1no, on o6napae1 nocnepoaa1enLno nnn
koHHynnkaunonno cansnoc1Lk.
2. Ecnn npepnoxenne copepxn1 cnoaa, nHekune o1nouenne ko apeHenn (1akne, kak
nepam, cnepykun, sa1eH, nocne, korpa, naua1L), 1o HopynL,
aepon1no, o6napae1 nocnepoaa1enLno nnn apeHenno cansnoc1Lk. HanpnHep:
Hopoxpa1L, noka akseHnnnp ne nonyun1 cnrnan, u1o nonLsoaa1enL ac1aann
anek1ponnyk kap1y, sa1eH sanpocn1L nnpnanpyanLnm npen1nqnkaunonnm
noHep.
3. Ecnn npepnka1 npepnoxennn ne copepxn1 epnnoro, konkpe1noro o6Lek1a,
cnepykuero sa rnaronoH, 1o HopynL, aepon1no, o6napae1 nornuecko cansnoc1Lk.
HanpnHep, y1aepxpenne pepak1npoaa1L ace pannme o6napae1 nornuecko
cansnoc1Lk. Bmckasmaanne pepak1npoaa1L ncxopnme pannme Hoxe1 o6napa1L
qynkunonanLno cansnoc1Lk.
4. Ecnn npepnoxenne copepxn1 cnoaa apope nnnunannsnpoaa1L nnn o6noan1L, 1o
HopynL ckopee acero o6napae1 apeHenno cansnoc1Lk.
17.1.3. 3aenneuue u cansuoc1u a OOH
B rnaae 1 Hm o1He1nnn neckonLko acnek1oa, na ocnoaannn ko1opmx knaccm Hory1
paccHa1pnaa1Lcn kak nornueckoe npoponxenne Hopyne. TeH caHmH npaanna paspa6o1kn
pnn Hopyne nerko nepenocn1cn na o6Lek1m. Pasnnunme knaccm ponxnm 6m1L sauennenm
kak Hoxno HenLue ne 1onLko pnn 6onLue nonn1noc1n, no 1akxe n pnn 1oro, u1o6m nx
nerue 6mno nsaneuL ns opnoro npnnoxennn n noa1opno ncnonLsoaa1L a noamx npoek1ax. C
ppyro c1oponm, kaxpm o6Lek1 knacca ponxen nHe1L konkpe1nyk uenL, a He1opm o6nsanm
cnoco6c1aoaa1L a1o uenn 1eH nnn nnmH cnoco6oH. To ec1L o6Lek1 ponxen 6m1L cansnmH.
17.1.4. 3akou QeHe1epa
Coae1m no nanncannk nporpaHH 6maak1 a6c1pak1nmHn (Hopynn ponxnm o6napa1L
any1penne cansnoc1Lk n HnnnHnsnpoaa1L sauennenne) n konkpe1nmHn (npouepypm ne
ponxnm copepxa1L 6onee 60 c1pok kopa). Konkpe1nme npen nerue nonnHa1L n npnHenn1L,
no onn uac1o y6akknaak1 nporpaHHnc1oa (n apHnnnc1pa1opoa npoek1a) noxnmH uyac1aoH
6esonacnoc1n n o1anekak1 annHanne o1 nac1onue npo6neHm. B kauec1ae cpepc1aa
cnnxennn cnoxnoc1n nporpaHHm npaanno, orpannunaakuee npouepypm pasHepoH 60 c1pok,
nanne1cn a nyuueH cnyuae ycnoanmH. Kopo1kan npouepypa co cnoxno nornuecko
converted to PDF by BoJIoc
c1pyk1ypo 6maae1 ropaspo 6onee 1pypno pnn nonnHannn n npaannLnoro kopnpoaannn,
ueH pnnnnan nocnepoaa1enLnoc1L npnHonnnenmx koHanp npncaanaannn.
Ananornuno, qana1nueckan nonm1ka neko1opmx nkpe neckonLko ne1 nasap am6pocn1L
onepa1op goto uac1o yaopnna a nenpaannLnoH nanpaanennn. Onepa1op goto caH no ce6e
npoc1o cnHn1oH 6onesnn, a ne 6onesnL. Y1aepxpenne coc1onno ne a 1oH, u1o koHanpa goto
nanne1cn nnoxo o1 npnpopm n u1o nporpaHHm, ko1opme ee ns6erak1, nannk1cn opnosnauno
6onee xopounHn, no a 1oH, u1o npn ncnonLsoaannn goto 1pypnee nonn1L cHmcn nporpaHHm.
Baxna nonn1noc1L nporpaHH, a ne onepa1op goto. TeH ne Henee Hm ne HoxeH nrnopnpoaa1L
1o1 qak1, u1o a1o npoc1oe npaanno nanne1cn nonesnmH, ero nerko npnHenn1L n ono
aqqek1nano a 6onLunnc1ae cnyuaea a cHmcne poc1nxennn xenaeHoro pesynL1a1a. CnpocnH
ce6n: Hory1 nn 6m1L cospanm 1akne pykoaopnune npaanna pnn o6Lek1no-opnen1npoaannmx
nporpaHH?
Opno ns 1aknx npaann 6mno npepnoxeno KapnoH n6epxepoH a pesynL1a1e ero pa6o1m nap
cpepc1aoH o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn nop nasaanneH Demeter.
Hpaanno nonyunno nasaannn sakon [eHe1epa [Lieberherr 1989a, Lieberherr 1989b].
HHek1cn pae qopHm a1oro sakona: cna6an n cnnLnan. O6e c1peHn1cn yHenLun1L
sauennenne o6Lek1oa sa cue1 orpannuennn canse Hexpy nnHn.
3akon [eHe1epa. B He1ope M knacca C ponxnm ncnonLsoaa1Lcn ncknkun1enLno He1opm:
knacca C;
knaccoa, k ko1opmH npnnapnexa1 apryHen1m He1opa M (aosHoxnm apryHen1m
knacca C). Ino6anLnme o6Lek1m n o6Lek1m, cospaaaeHme any1pn He1opa M,
paccHa1pnaak1cn kak apryHen1m a1oro He1opa.
Ecnn nepeqpasnpoaa1L a1o1 sakon a 1epHnnax o6Lek1oa, a ne He1opoa, 1o Hm npnxopnH k
cnepykueHy y1aepxpennk.
3akon [eHe1epa (cna6an qopHa). TonLko cnepykune o6Lek1m ponxnm amc1yna1L a ponn
nc1ounnka pannmx n npneHnnka coo6uenn He1opa:
1. ApryHen1m amnonnneHoro He1opa (aknkuan o6Lek1 self).
2. DkseHnnnpm nonyua1enn He1opa.
3. Ino6anLnme nepeHennme (n poc1ynnme ao ace nporpaHHe n 1e, u1o anpnm a opnoH
qane).
4. BpeHennme nepeHennme, cospaaaeHme any1pn He1opa.
D1o1 sakon a caoe cnnLno qopHe paspeuae1 poc1yn ns He1opa 1onLko k akseHnnnpaH
knacca, a ko1opoH onpepenen He1op. [oc1yn k akseHnnnpaH cynepknacca ponxen
ocyuec1ann1Lcn ncknkun1enLno nocpepc1aoH qynkun poc1yna.
3akon [eHe1epa (cnnLnan qopHa). TonLko cnepykune o6Lek1m ponxnm amc1yna1L a ponn
nc1ounnka pannmx n npneHnnka coo6uenn He1opa:
1. ApryHen1m amnonnneHoro He1opa (aknkuan o6Lek1 self).
2. DkseHnnnpm knacca, copepxauero amnonnneHm He1op.
3. Ino6anLnme nepeHennme.
4. BpeHennme nepeHennme, cospaaaeHme any1pn He1opa.
Honesno paccHo1pe1L, kakne npaanna poc1yna am1ekak1 ns sakona [eHe1epa, n coo1nec1n
ero c konuenunnHn sauennennn n cansnoc1n, onncannmHn amue. Ocnoano anp poc1yna,
converted to PDF by BoJIoc
ko1opm sanpeuae1cn npaannaHn [eHe1epa, a1o npnHan o6pa6o1ka (Hannnynnpoaanne)
none akseHnnnpa ppyroro knacca. B npo1nanoH cnyuae opnn o6Lek1 saancn1 o1
any1pennero npepc1aanennn ppyroro o6Lek1a (u1o nanne1cn qopHo sauennennn
any1pennnx pannmx). Co6nkpenne a1oro npaanna npnaopn1 k 1oHy, u1o knaccm Hory1
nsyua1Lcn n 6m1L nonn1nmHn nesaancnHo ppyr o1 ppyra (nockonLky onn asanHopec1ayk1
Hexpy co6o npoc1mH, ue1ko onpepenennmH o6pasoH). Bnpqc-Bpok n Bnnkepcon eue 6onee
yxec1ouak1 sakon [eHe1epa, cun1an, u1o ccmnkn ns He1opa paxe na nepeHennme
akseHnnnpa 1oro xe knacca ponxnm amnonnn1Lcn uepes qynkunn poc1yna [Wirfs-
Brock 1989a]. Hx apryHen1aunn coc1on1 a 1oH, u1o nenocpepc1aennm poc1yn k nepeHennmH
cepLesno orpannunaae1 aosHoxnoc1L nporpaHHnc1a popa6a1maa1L cyuec1aykune knaccm.
17.1.5. BupuHoc1u: ua ypoaue knaccoa u ua ypoaue
o61ek1oa
To1 qak1, u1o knacc Hoxe1 nHe1L neckonLko akseHnnnpoa, npnaopn1 k pnpy noamx
coo6paxenn, kacakunxcn kon1ponn nap sauennenneH. B o6Lek1no-opnen1npoaannmx
nsmkax nporpaHHnpoaannn pnn onncannn anpnHoc1n nHen ncnonLsyk1cn pae Hopenn. Onn
Hory1 6m1L onncanm kak anpnHoc1L na ypoane knacca n anpnHoc1L na ypoane o6Lek1a.
Pasnnune Hexpy nnHn caopn1cn k o1ae1y na aonpoc: paspeueno nn o6Lek1y sarnnpmaa1L
any1pL popc1aennoro o6Lek1a?
Hsmkn, ko1opme ynpaannk1 anpnHoc1Lk na ypoane knaccoa (nanpnHep, C++),
paccHa1pnaak1 ace akseHnnnpm knacca opnnH cnoco6oH. Kak Hm ckopo ysnaeH, C++
ponyckae1 kon1ponL anpnHoc1n npen1nqnka1opoa, no paxe a nan6onee orpannun1enLnoH
cnyuae (nonn pannmx onncanm c knkueamH cnoaoH private) akseHnnnp knacca acerpa nHee1
aosHoxnoc1L poc1yna k nonnH pannmx ppyrnx akseHnnnpoa 1oro xe knacca. To ec1L
o6Lek1aH acerpa paspeuen poc1yn ko any1penneHy coc1onnnk popc1aennmx o6Lek1oa.
C ppyro c1oponm, ynpaanenne anpnHoc1Lk na ypoane o6Lek1oa paccHa1pnaae1
nnpnanpyanLnm o6Lek1 kak ocnoanyk epnnnuy kon1ponn poc1yna. Hsmkn
nporpaHHnpoaannn c anpnHoc1Lk na ypoane o6Lek1oa (nanpnHep, Smalltalk) sanpeuak1
o6Lek1aH poc1yn ko any1penneHy coc1onnnk ppyroro o6Lek1a, paxe ecnn onn o6a nannk1cn
akseHnnnpaHn opnoro n 1oro xe knacca.
17.1.6. Ak1uaume suaveuun
Ak1nanoe snauenne [Stefik 1986] a1o nepeHennan, c ko1opo Hm xo1nH amnonnn1L
neko1opme pec1ann acnkn pas, korpa nsHenne1cn ee snauenne. Cnc1eHa c ak1nanmHn
snauennnHn nnnkc1pnpye1, noueHy sauennenne uepes napaHe1pm npepnou1n1enLnee, ueH
ppyrne anpm sauennennn, oco6enno pnn o6Lek1no-opnen1npoaannmx nsmkoa. HpepnonoxnH,
u1o HopenL npepnoro peak1opa aknkuae1 a ce6n knacc Reactor, ko1opm copepxn1
pasnnunyk nnqopHaunk o coc1onnnn peak1opa. Cpepn na6nkpaeHmx napaHe1poa snaun1cn
1eHnepa1ypa 1ennoo1aopnue cpepm (aopm, unpkynnpykue aokpyr 6noka). [anee
npepnonoxnH, u1o a1a aennunna Hopnqnunpye1cn c npnHenenneH knaccnueckoro o6Lek1no-
opnen1npoaannoro nopxopa: snauenne yc1anaannaae1cn uepes He1op setHeat, a cun1maae1cn
uepes qynkunk getHeat. Knacc amrnnpn1 cnepykunH o6pasoH:
@interface Reactor : Object
{ ...
double heat; ...
}
- (void) setHeat: (double) newValue;
- (double) getHeat;
Hpepc1aanH, u1o nporpaHHa 6mna paspa6o1ana n naxopnnacL a pa6oueH coc1onnnn, korpa
nporpaHHnc1 peunn, u1o 6mno 6m nennoxo noc1onnno ansyanLno o1o6paxa1L 1ekyuyk
1eHnepa1ypy aopm a npouecce Hopennpoaannn. Xena1enLno cpena1L a1o c HnnnHanLnmH
a1opxenneH a 1eno nporpaHHm. B uac1noc1n, paspa6o1unk ne xoue1 Henn1L knacc Reactor.
converted to PDF by BoJIoc
(HanpnHep, no1oHy, u1o a1o1 knacc 6mn nanncan ppyrnH nporpaHHnc1oH, nnn xe knacc
ncnonLsye1cn a ppyroH npnnoxennn, rpe ykasannoe caoc1ao ne 1pe6ye1cn.)
Hpoc1oe peuenne coc1on1 a 1oH, u1o6m nopopn1L nopknacc knacca Reactor (c nHeneH
GraphicalReactor), ko1opm nepeonpepenne1 ncknkun1enLno He1op setHeat. D1o1 He1op
1enepL o6noanne1 rpaqnueckne nso6paxennn nepep amsoaoH coo1ae1c1aykuero He1opa
napknacca (cH. nnxe). TaknH o6pasoH, nporpaHHnc1 6ype1 cospaaa1L o6Lek1m ne 1nna
Reactor, a 1nna GraphicalReactor. D1o nponcxopn1, aepon1no, nnuL opnaxpm npn
nnnunannsaunn. [o 1ex nop noka ace nsHenennn snauennn 1eHnepa1ypm pnn o6Lek1a
Reactor nponcxopn1 ncknkun1enLno uepes He1op setHeat, pa1unk 6ype1 o1paxa1L snauenne
koppek1no.
@implementation GraphicalReactor : Reactor
- (void) setHeat: (double) newValue
{
/* xoz, neoxozwm-n znn onosnenwn zaiuwxa */
[ super setHeat: newValue ];
}
@end
Hsmkn Smalltalk n Objective-C noppepxnaak1 6onee o6uyk konuenunk, nasmaaeHyk
saancnHoc1L. Mm o6cyxpaeH ee a paspene 17.4.
17.2. Knueu1m-nopknaccm u knueu1m-nonusoaa1enu
Mm neckonLko pas o1Heuann, u1o o6Lek1, nopo6no Hopynk, nHee1 pae coc1aannkune:
o1kpm1yk (public) n sakpm1yk (private). O1kpm1an uac1L oxaa1maae1 ace caoc1aa (He1opm,
nepeHennme), k ko1opmH nHee1cn poc1yn ane Hopynn. 3akpm1an uac1L aknkuae1
o6uepoc1ynnyk, a 1akxe He1opm n nepeHennme, poc1yn k ko1opmH aosHoxen 1onLko
nsny1pn o6Lek1a. HonLsoaa1enk cepanca, o6ecneunaaeHoro o6Lek1oH (1o ec1L knnen1y),
1pe6ye1cn sna1L noppo6noc1n 1onLko npo o1kpm1yk c1opony Hopynn. [e1ann peannsaunn n
ppyrne any1pennne caoc1aa, ne nannkunecn aaxnmHn pnn knnen1a, ponxnm 6m1L o1 nero
cnpn1anm.
Anan Cnapep [Snyder 1986] n ppyrne nccnepoaa1enn o1Heuann, u1o nacnepoaanne a
o6Lek1no-opnen1npoaannmx nsmkax nporpaHHnpoaannn osnauae1, u1o knaccm nHek1 eue n
1pe1Lk coc1aannkuyk a nHenno caoc1aa, poc1ynnme pnn nopknaccoa, no ne nyxnme
ppyrnH nonLsoaa1ennH. Paspa6o1unky nopknacca pannoro knacca no1pe6ye1cn, aepon1no,
sna1L 6onLue o any1penneH yc1poc1ae ncxopnoro knacca, ueH nonLsoaa1enk akseHnnnpoa
knacca. Opnako n paspa6o1unk nopknacca ne nyxpae1cn ao ace nnqopHaunn o6 ncxopnoH
knacce.
Mm HoxeH pyHa1L kak o paspa6o1unke nopknacca, 1ak n o nonLsoaa1ene knacca kak o
knnen1ax knacca, nockonLky onn ncnonLsyk1 npepoc1aanneHme nH cpepc1aa. Opnako 1ak
kak a1n paa knnen1a nHek1 pasnnunme 1pe6oaannn, nonesno o1penn1L knnen1oa-nopknaccoa
o1 knnen1oa-nonLsoaa1ene. Hocnepnne cospak1 akseHnnnpm knacca n nocmnak1 nH
coo6uennn, a nepame konc1pynpyk1 noame knaccm, ocnoaannme na pannoH knacce.
B na6ope knaccoa, cospannmx naHn a rnaae 8 kak uac1L kap1ounoro nacLnnca, knacc Card
onncmaae1 nepeHennme r n s (copepxaune panr n Hac1L nrpanLno kap1m) kak sakpm1me.
TonLko He1opm knacca Card Hory1 nHe1L poc1yn nnn Hopnqnunpoaa1L a1n nepeHennme. C
ppyro c1oponm, pannme knacca CardPile pas6n1m na 1pn ka1eropnn: sakpm1me private,
saunuennme protected n o1kpm1me public. 3akpm1an nepeHennan firstCard poc1ynna 1onLko
nsny1pn knacca CardPile, a 1o apeHn kak saunuennme nonn pannmx x n y poc1ynnm nn6o
uepes knacc, nn6o uepes ero nopknaccm. Epnnc1aennm o6uepoc1ynnm ynnaepcanLnm
nn1epqec uepes He1opm. Hpn yc1panennn o1kpm1mx nepeHennmx akseHnnnpa nsmk
nporpaHHnpoaannn rapan1npye1, u1o Hexpy knaccoH n ppyrnHn koHnonen1aHn nporpaHHm
ne aosnnkne1 sauennennn no pannmH. (Opnako nsmk nnuL npepoc1aanne1 coo1ae1c1aykun
HexannsH. HpaannLnoe ero ncnonLsoaanne oc1ae1cn o6nsannoc1Lk nporpaHHnc1a
nanpnHep, ny1eH onncannn none pannmx kak private nnn protected.)
converted to PDF by BoJIoc
Moxno pyHa1L o pasan1nn n Hopnqnkaunn nporpaHHnoro o6ecneuennn a 1epHnnax
knnen1oa-nopknaccoa n knnen1oa-nonLsoaa1ene. Korpa paspa6o1unk knacca peknapnpye1
o6uepoc1ynnme caoc1aa knacca, on 1eH caHmH onpepenne1 nekn kon1pak1: knacc o6nsan
amnonnn1L sananennme o6nsannoc1n. HporpaHHnc1 cao6open ao any1penne peannsaunn
knacca po 1ex nop, noka aneunn nn1epqec oc1ae1cn 6es nsHenenn (nnn, aosHoxno,
napaunaae1cn). Ananornuno, xo1n a1o Henee npnnn1o n ne c1onL oueanpno, paspa6o1unk
knacca ponxen o6ecneun1L nn1epqec pnn pa6o1m nopknaccoa. 3pecL aosnnkae1
c1anpap1nm n 1pypnoynoanHm nc1ounnk oun6ok a nporpaHHnoH o6ecneuennn: npn
nsHenennn any1pennnx pe1ane knacca nopknaccm nepec1ak1 pa6o1a1L. O1pennn sakpm1me
any1pennne uac1n knacca o1 nonLsoaa1enLckoro nn1epqeca pasnnunoro ypoann (xo1n 6m
1onLko a cnny cornauennn), nporpaHHnc1 yc1anaannaae1 rpannum pnn ponyc1nHmx
nsHenenn n Hopnqnkaun. Besonacnoc1L nsHenenn, anocnHmx a cyuec1aykun kop,
kpn1nuna npn conpoaoxpennn 6onLunx nporpaHHnmx cnc1eH pnn1enLnoro ncnonLsoaannn.
Honn1ne knnen1a-nopknacca Hoxe1 amsaa1L nepoyHenne y neko1opmx un1a1ene, nockonLky
korpa akseHnnnp nopknacca yxe cospan, 1o knacc n nopknacc cnnaannk1cn a epnnm
o6Lek1. TeH ne Henee a1o nonn1ne nonesno, korpa Hm paccHa1pnaaeH paspa6o1unkoa
knacca. 3auac1yk paspa6o1unk knacca n paspa6o1unk nopknacca a1o pasnme nkpn. TeH
caHmH xopouan npak1nka nporpaHHnpoaannn 1pe6ye1, u1o6m paspa6o1unk nk6oro knacca
yun1maan aosHoxnoc1L nopoxpennn nopknacca a 6ypyueH n o6ecneunaan neo6xopnHyk
pokyHen1aunk n nporpaHHnme cpepc1aa, ko1opme o6nerua1 a1o1 npouecc.
17.3. Ynpaaneuue poc1ynoH u aupuHoc1um
B a1oH paspene Hm akpa1ue ouep1nH pasnnunme caoc1aa Hacknpoakn nnqopHaunn a
paccHa1pnaaeHmx naHn o6Lek1no-opnen1npoaannmx nsmkax nporpaHHnpoaannn. Mm 6ypeH
o1Heua1L 1akxe noppepxky a pasnnunmx nsmkax konuenun, anannsnpyeHmx a nac1onue
rnaae.
17.3.1. BupuHoc1u a Smalltalk
Cnc1eHa Smalltalk o6ecneunaae1 ckpoHnm na6op cpepc1a saun1m n Hacknpoakn pannmx n
He1opoa. HepeHennme akseHnnnpa acerpa paccHa1pnaak1cn kak sakpm1me n poc1ynnm
1onLko nsny1pn He1opoa knacca-npo1o1nna akseHnnnpa nnn ero nopknacca. [oc1yn k
nepeHennmH akseHnnnpa nsane o6Lek1a ponxen amnonnn1Lcn kocaennmH ny1eH uepes
qynkunn poc1yna.
C ppyro c1oponm, He1opm acerpa paccHa1pnaak1cn kak o6uepoc1ynnme, n poc1yn k nnH
o1kpm1 nk6oHy o6Lek1y. Hopo6no 1oHy, kak ne1 cpepc1a, u1o6m cpena1L nonn pannmx
akseHnnnpa o6uepoc1ynnmHn, 1ak ne1 n cpepc1a pnn Hacknpoakn He1opoa. Opnako
neko1opme He1opm noHeuak1cn private. D1o osnauae1, u1o onn ponxnm ncnonLsoaa1Lcn
1onLko knaccoH n ne ponxnm amsmaa1Lcn knnen1aHn-nonLsoaa1ennHn. XopounH 1onoH
nanne1cn yaaxenne a1oro cornauennn n Hopa1opn na ncnonLsoaanne sakpm1mx He1opoa.
17.3.2. BupuHoc1u a Object Pascal
Hsmk Object Pascal aepcnn Apple o6ecneunaae1 ne6ora1me cpepc1aa ynpaanennn
anpnHoc1Lk none o6Lek1a. Bce nonn kak pannme, 1ak n He1opm poc1ynnm n pnn
knnen1oa-nonLsoaa1ene, n pnn knnen1oa-nopknaccoa. TonLko a cnny 1papnunn nnn
cornauennn nonn pannmx cun1ak1cn o1kpm1mHn pnn paspa6o1unkoa nopknaccoa, a
He1opm pnn knnen1oa-nonLsoaa1ene. [axe ecnn pykoaopnune ykasannn no c1nnk
nporpaHHnpoaannn (nopo6nme sakonaH [eHe1epa) n ne Hory1 c1poro kon1ponnpoaa1Lcn
cnc1eHo, onn ace-1akn oc1ak1cn a cnne n ponxnm yaaxa1Lcn nporpaHHnc1oH. Honesno
1akxe, ecnn nporpaHHnc1 ykasmaae1 a koHHen1apnnx na 1e He1opm knacca, ko1opme
cnepye1 nepeonpepenn1L a nopknaccax.
Bepcnn nsmka qnpHm Borland nanne1cn neHnoro 6onee Houno a a1oH o1nouennn. Delphi
noppepxnaae1 knkueame cnoaa public, protected n private a cHmcne, ouenL 6nnskoH k nx
converted to PDF by BoJIoc
snauennk a nsmke C++. Opnako any1pn paspena implementation 6n6nno1ek unit ace nonn
paccHa1pnaak1cn kak o1kpm1me. D1o nosaonne1 akseHnnnpaH nHe1L poc1yn k sakpm1mH
nonnH pannmx caonx popc1aennnkoa.
17.3.3. BupuHoc1u a C++
Hs acex paccHa1pnaaeHmx naHn nsmkoa C++ o6ecneunaae1 nan6onee 6ora1m na6op
cpepc1a kon1ponn poc1yna k nnqopHaunn. Kak Hm o1Heuann a npepmpyunx rnaaax, a1o
o6ecneunaae1cn 1peHn knkueamHn cnoaaHn: public, protected n private.
Korpa ykasannme knkueame cnoaa ncnonLsyk1cn npn onncannn none pannmx knacca, nx
aqqek1 onncmaae1cn nou1n nenocpepc1aenno a 1epHnnax paspena 17.2. [annme, ko1opme
cnepyk1 sa cneunqnka1opoH poc1yna public:, poc1ynnm a paano Hepe n knnen1aH-
nopknaccaH, n knnen1aH-nonLsoaa1ennH. Honn, onpepenennme co cneunqnka1opoH
protected:, poc1ynnm 1onLko any1pn knacca n ero nopknaccoa n noa1oHy npepnasnauenm pnn
knnen1oa-nopknaccoa, no ne pnn knnen1oa-nonLsoaa1ene. Hakoneu, cneunqnka1op poc1yna
private: npepuec1aye1 nonnH pannmx, ko1opme poc1ynnm ncknkun1enLno akseHnnnpaH
caHoro knacca: onn sakpm1m n pnn nopknaccoa, n pnn nonLsoaa1ene. Hpn o1cy1c1ann
kakoro-nn6o nanoro cneunqnka1opa nonn pannmx paccHa1pnaak1cn kak private.
C 1oukn spennn o6ue qnnocoqnn HexannsHm kon1ponn nsmka C++ npepnasnauenm pnn
saun1m o1 nenpepnaHepennoro poc1yna, no ne o1 snoro yHmcna. Ec1L neckonLko cnoco6oa
o6o1n saun1y. Hpoc1eun ns nnx coc1on1 a ncnonLsoaannn qynkun, aosapauakunx
ykasa1enL nnn ccmnky. PaccHo1pnH cnepykun knacc:
class Sneaky
{
private:
int safe;
public:
// wnwuwanwswposais none safe snauenwem 10
Sneaky() { safe = 10; }
int &sorry() { return safe; }
}
Xo1n none pannmx safe n onncano kak private, ccmnka na nero aosapauae1cn He1opoH sorry.
Cnepoaa1enLno, ampaxenne anpa
Sneaky x;
x.sorry() = 17;
nsHenn1 snauenne nonn safe c 10 na 17 paxe a 1oH cnyuae, korpa amsoa He1opa sorry
ocyuec1anne1cn nonLsoaa1enLcknH kopoH.
Bonee 1onknH HoHen1oH nanne1cn 1o, u1o cneunqnka1opm poc1yna a nsmke C++ ynpaannk1
ne anpnHoc1Lk, a poc1ynoH k aneHen1aH pannmx. Knaccm, nokasannme nnxe, nnnkc1pnpyk1
a1o:
int i; // rnoansnan nepemennan
class A
{
private:
int i;
};
class B : public A
{
void f();
};
B::f()
{
i++; // omwxa: A::i onwcano xax private
converted to PDF by BoJIoc
}
Oun6ka aosnnkae1, nockonLky qynkunn f nm1ae1cn Hopnqnunpoaa1L nepeHennyk i,
nacnepyeHyk ns knacca A, no nepoc1ynnyk (1ak kak ona onncana kak private:). Ecnn 6m
cneunqnka1opm poc1yna ynpaannnn anpnHoc1Lk, a ne poc1ynoH, 1o nepeHennan i knacca A
6mna 6m ne anpna n o6noanennk nopaeprnacL 6m rno6anLnan nepeHennan i.
Popc1aeuume skseHnnnpm
Mopnqnka1opm poc1yna o1nocn1cn k knaccy, a ne k ero akseHnnnpaH. To ec1L nonn pannmx,
onncannme kak private, a nsmke C++ ne coo1ae1c1ayk1 a 1ounoc1n konuenunn,
paspa6o1anno naHn panee npn o6ueH o6cyxpennn nonn1nn anpnHoc1n. Cornacno a1o
konuenunn sakpm1me pannme poc1ynnm 1onLko caHoHy o6Lek1y, a 1o apeH kak a C++ onn
o1kpm1m nk6oHy o6Lek1y 1oro xe knacca. TeH caHmH a nsmke C++ o6Lek1y paspeuae1cn
Hannnynnpoaa1L sakpm1mHn nonnHn ppyroro akseHnnnpa 1oro xe knacca.
B kauec1ae npnHepa paccHo1pnH onncanne knacca, npnaepennoe nnxe. Honn pannmx rp n ip,
ko1opme osnauak1 aeuec1aennyk n HnnHyk uac1n koHnnekcnoro uncna, noHeuenm kak
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);
}
};
Bnnapnan onepaunn + neperpyxae1cn c uenLk npaannLnoro cnoxennn payx koHnnekcnmx
uncen. HecHo1pn na sakpm1yk npnpopy none rp n ip, onepa1opy-qynkunn paspeuen poc1yn
k nnH a apryHen1e x, nockonLky apryHen1 n nonyua1enL o1nocn1cn k opnoHy knaccy.
Konc1pyk1opm n pec1pyk1opm, nopo6nme qynkunn Complex a npnaepennoH npnHepe,
o6muno onncmaak1cn kak public. O6Lnanenne konc1pyk1opa kak protected noppasyHeaae1,
u1o 1onLko nopknaccm nnn ppyxec1aennme knaccm (cH. panee) Hory1 cospaaa1L akseHnnnpm
a1oro knacca, a 1o apeHn kak onncanne konc1pyk1opa c knkueamH cnoaoH private
orpannunaae1 cospanne noamx akseHnnnpoa 1onLko ppysLnHn n akseHnnnpaHn caHoro
knacca.
Cna6an qopHa sakonoa [eHe1epa uac1nuno amnonnne1cn npn onncannn acex none kak
saunuennmx (protected). CnnLnan qopHa peannsye1cn npn o6Lnanennn sakpm1mx none
(private). Bonee noppo6nm ananns npnnoxennn sakonoa [eHe1epa k nsmky C++ Hoxno
na1n a pa6o1e [Sakkinen 1988b].
Xo1n Hopnqnka1opm poc1yna a C++ naHnoro cnnLnee n rn6ue, ueH a ppyrnx
paccHa1pnaaeHmx naHn nsmkax, aqqek1nanoe ncnonLsoaanne a1nx caoc1a 1pe6ye1
npepycHo1pn1enLnoc1n n onm1a. Kak n a cnyuae am6opa Hexpy anp1yanLnmH n
neanp1yanLnmH He1opaHn, ypoaenL kon1ponn, o6ecneunaaeHm nsmkaHn C++ nnn Delphi
Pascal, npnaopn1 k 1oHy, u1o nerkoc1L nopoxpennn nopknacca saancn1 o1 1oro, u1o sanncan
paspa6o1unk a ncxopnoH knacce. Ecnn knacc nanne1cn upesHepno sakpm1mH (saunuennme
nonn pannmx o6Lnanenm sakpm1mHn), 1o cospanne nopknacca sa1pypneno. Bosnnkak1
cepLesnme npo6neHm, korpa paspa6o1unk nopknacca ne Hoxe1 Hopnqnunpoaa1L ncxopnyk
qopHy knacca nanpnHep, ecnn ncxopnm knacc pacnpoc1panne1cn kak uac1L 6n6nno1ekn.
converted to PDF by BoJIoc
3akpm1oe nacnepoaanne
Knkueame cnoaa public n private 1akxe npepuec1ayk1 nHenaH napknaccoa npn onncannn
knacca. B pannoH cnyuae ykasannme knkueame cnoaa onpepennk1 anpnHoc1L nnqopHaunn,
nacnepyeHo ns napknacca. Hopknacc, ko1opm nacnepye1 o1 ppyroro knacca o1kpm1o
(public), coo1ae1c1aye1 nonn1nk nacnepoaannn, ko1opmH Hm pykoaopc1aoaanncL po cnx
nop: nopknacc nanne1cn nop1nnoH. Ecnn nopknacc nopoxpae1cn sakpm1o (private), 1o
o6uepoc1ynnme caoc1aa napknacca ypesak1cn po ypoann Hopnqnka1opa. B pesynL1a1e
pannm Hopnqnka1op ykasmaae1, u1o napknacc ncnonLsye1cn 1onLko pnn konc1pynpoaannn,
n pesynL1npykun knacc ne ponxen n ne Hoxe1 paccHa1pnaa1Lcn kak nop1nn ncxopnoro
knacca.
Korpa knacc nopoxpae1cn sakpm1mH o6pasoH, akseHnnnpm nopknacca ne ponxnm
npncaanaa1Lcn npen1nqnka1opaH napknacca (1akoe aosHoxno npn o1kpm1oH
nacnepoaannn). Hpoc1o cnoco6 sanoHnn1L ykasannoe orpannuenne aocnonLsoaa1Lcn
ycnoaneH 6m1L akseHnnnpoH. Hacnepoaanne uepes Hopnqnka1op public osnauae1, u1o
amnonneno ycnoane 6m1L akseHnnnpoH, n 1eH caHmH akseHnnnpm nopknacca Hory1
ncnonLsoaa1Lcn aespe, rpe ac1peuak1cn akseHnnnpm napknacca. Co6aka Dog nanne1cn
akseHnnnpoH knacca Hnekonn1akunx Mammal, n, cnepoaa1enLno, Dog Hoxe1
ncnonLsoaa1Lcn ao acex cn1yaunnx, rpe ac1peuae1cn Mammal. 3akpm1oe nacnepoaanne ne
noppasyHeaae1 amnonnennn ycnoann 6m1L akseHnnnpoH, nockonLky akseHnnnpm
nopoxpennoro knacca ne Hory1 ao acex cnyuanx ncnonLsoaa1Lcn aHec1o akseHnnnpoa
popn1enn. HanpnHep, 6eccHmcnenno npnHenn1L knacc 1a6nnu cnHaonoa SymbolTable
(nacnepykun o1 6onee o6uero knacca cnoaape Dictionary uepes Hopnqnka1op private)
1aH, rpe 1pe6ye1cn ncnonLsoaanne knacca Dictionary. Ecnn nepeHennan onncana c 1nnoH
pannmx Dictionary, e nenLsn npncaanaa1L snauenne 1nna SymbolTable (a1o 6mno 6m
paspeueno, ecnn 6m nacnepoaanne 6mno o1kpm1mH).
Qpyxec1aeuume qyukuu
[pyro acnek1 anpnHoc1n a nsmke C++ a1o ppyxec1aennme qynkunn. Onn npepc1aannk1
co6o o6munme qynkunn (ne He1opm), ko1opme onncanm c Hopnqnka1opoH friend a
onpepenennn knacca. [pyxec1aennmH qynkunnH paspeuae1cn un1a1L n sanncmaa1L a nonn
pannmx o6Lek1a, onncannme n kak private, n kak protected.
PaccHo1pnH onncanne knacca, pacunpnkuee npnaepennoe amue onpepenenne koHnnekcnmx
uncen:
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);
}
Honn pannmx rp n ip a c1pyk1ype pannmx, npepc1aannkue koHnnekcnme uncna, onncanm c
Hopnqnka1opoH private, n 1eH caHmH nepoc1ynnm ane He1opoa knacca. Oynkunn abs,
ko1opan neperpyxae1 qynkunk c 1eH xe nHeneH, onpepenennyk pnn aeuec1aennmx
converted to PDF by BoJIoc
snauenn c paono 1ounoc1Lk, ne nanne1cn He1opoH (a1o o6munan qynkunn). Opnako
nockonLky ona onncana kak ppyxec1aennan c Hopnqnka1opoH friend a knacce koHnnekcnmx
uncen, e paspeuen poc1yn ko aceH nonnH pannmx knacca, a 1oH uncne n k sakpm1mH.
Takxe paspeuae1cn onncmaa1L knaccm n paxe o1penLnme He1opm knaccoa kak
ppyxec1aennme. Han6onee 1nnnunan npnunna ncnonLsoaannn ppyxec1aennmx qynkun
coc1on1 a 1oH, u1o
qynkunn 1pe6ye1cn poc1yn ko any1penne c1pyk1ype payx knaccoa;
neo6xopnHo amsaa1L ppyxec1aennyk qynkunk nHenno kak qynkunk, a ne kak
coo6uenne, nepepaaaeHoe o6Lek1y, 1o ec1L kak abs(x), a ne kak x.abs().
[pyxec1aennme qynkunn nannk1cn HounmH cpepc1aoH, no onn 1akxe nerko Hory1 c1a1L
nc1ounnkoH npo6neH. B uac1noc1n, onn aaopn1 a 1ounoc1n 1y pasnoanpnoc1L sauennennn
pannmx, ko1opan npen1nqnunpoaanacL a nauane a1o rnaam kak apepnan pnn paspa6o1kn
Hnorokpa1no ncnonLsyeHoro nporpaHHnoro o6ecneuennn. Bespe, rpe 1onLko aosHoxno,
6onee o6Lek1no-opnen1npoaannme He1opm nnkancynnunn (nanpnHep, He1opm) ponxnm
nHe1L npepnou1enne nepep ppyxec1aennmHn qynkunnHn. TeH ne Henee ec1L cnyuan, korpa
ne1 ppyrnx cpepc1a nanpnHep, qynkunn 1pe6ye1cn poc1yn ko any1penne c1pyk1ype payx
(nnn 6onee) knaccoa. B 1aknx cnyuanx ppyxec1aennme qynkunn nannk1cn nonesno
a6c1pakune [Koenig 1989c].
Hpoc1pauc1aa uHeu
[pyroe nepaanee nsHenenne a nsmke C++ aaepenne npoc1panc1aa nHen (namespace).
namespace noHorae1 npepo1apa1n1L pasHnoxenne rno6anLnmx nHen. Knkueaoe cnoao static
orpannunaae1 o6nac1L anpnHoc1n opnnH qanoH. Hoa1oHy korpa npexpe 1pe6oaanocL
cpena1L neko1opoe nHn coaHec1no ncnonLsyeHmH a payx qanax, 1o epnnc1aennm amxop
coc1onn a 1oH, u1o6m cpena1L ero rno6anLnmH. Hopo6nme nHena Hory1 1enepL
aknapmaa1Lcn any1pL onncann namespace:
namespace myLibrary
{
int x;
class A
{
...
};
class B : public A
{
...
};
...
}
HepeHennme, onncannme any1pn namespace, ne nannk1cn rno6anLnmHn. Ecnn nporpaHHnc1
xoue1 nopknkun1L kakoe-nn6ypL konkpe1noe npoc1panc1ao nHen, on ero nano ykasmaae1. B
pesynL1a1e, ace nHena aepxnero ypoann, onpepenennme any1pn ykasannoro npoc1panc1aa
nHen, c1anoan1cn anpnHmHn:
using namespace myLibrary;
HnpnanpyanLnme aneHen1m 1akxe Hory1 akcnop1npoaa1Lcn ns konkpe1noro npoc1panc1aa
nHen c noHouLk nanoro ykasannn nn6o npoc1panc1aa uennkoH, nn6o o1penLnoro nHenn:
myLibrary::A anA; // nsno nozxnxuaem sce npocipanciso wmen
using myLibrary::B; // wmnopiwpyem ionsxo xnacc B
B aNewB; // ieneps B wmn iwna zann-x
converted to PDF by BoJIoc
Hoc1onuume vneum
B nsmke C++ knkueaoe cnoao const ncnonLsye1cn pnn ykasannn na snauenne, ko1opoe
oc1ae1cn nensHennmH ao apeHn cyuec1aoaannn o6Lek1a. Ino6anLnme nepeHennme, ko1opme
onncanm 1aknH o6pasoH, nannk1cn rno6anLnmHn konc1an1aHn. HepeHennme, o6Lnanennme
kak const nokanLno a npouepype, poc1ynnm 1onLko any1pn nee n ne Hory1
Hopnqnunpoaa1Lcn nocne nx nnnunannsaunn.
Honn pannmx akseHnnnpa uac1o aepy1 ce6n kak konc1an1m, no nx nauanLnoe snauenne ne
Hoxe1 6m1L onpepeneno po 1oro, kak cospan coo1ae1c1aykun o6Lek1. HanpnHep, nonn
pannmx, npepc1aannkune co6o aeuec1aennyk n HnnHyk uac1n koHnnekcnoro uncna a
knacce Complex (cH. amue), nnkorpa ne ponxnm nsHenn1Lcn pas, yx koHnnekcnoe uncno
cospano. B rnaae 3 Hm nasmaann 1akne nonn pannmx nensHenneHmHn. Onn cospak1cn c
noHouLk knkueaoro cnoaa const.
Tak kak npncaanaanne noc1onnnmH nonnH pannmx akseHnnnpa ne paspeuae1cn, a C++ onn
nnnunannsnpyk1cn c noHouLk 1o xe cnn1akcnuecko konc1pykunn, ko1opan ncnonLsye1cn
pnn amsoaa konc1pyk1opa popn1enn (cH. rnaay 7, rpe o6cyxpae1cn amsoa konc1pyk1opoa
popn1enLcknx knaccoa). PaccHo1pnH cnepykuee onncanne knacca:
class Complex
{
public:
const double rp;
const double ip;
Complex(double, double);
};
Complex::Complex(double a, double b) : rp(a), ip(b)
{
/* nycian xomanza */
}
B a1oH cnyuae nonn pannmx rp n ip onncanm uepes Hopnqnka1op const, 1ak u1o ne
npepc1aanne1 onacnoc1n cpena1L nx nonnHn public, nockonLky onn ace paano ne Hory1 6m1L
Hopnqnunpoaanm. 1o6m npncaon1L nH nauanLnoe snauenne, konc1pyk1op, no-anpnHoHy,
amsmaae1 rp n ip, kak ecnn 6m onn 6mnn napknaccaHn. D1o epnnc1aennm cnoco6
npncaanaannn snauenn noc1onnnmH nonnH. Korpa naunnae1 amnonnn1Lcn 1eno
konc1pyk1opa, snauenne noc1onnnmx none pannmx yxe ne Hoxe1 6m1L nsHeneno.
Honn pannmx, onncannme kak ccmnkn, nnnunannsnpyk1cn ananornunmH o6pasoH. Knkueaoe
cnoao const Hoxe1 npncoepnnn1Lcn 1akxe k onncannk qynkunn-unena. Opnako o6cyxpenne
a1o 1eHm amxopn1 sa paHkn panno knnrn.
BsanHopec1ane Hexpy neperpysko n nepeonpepenenneH
[pyro npnaopnun a cHyuenne acnek1 npaann anpnHoc1n a C++ a1o cansL nonn1n
neperpyskn n nepeonpepenennn. HHena qynkun, aknkuan He1opm knaccoa, Hory1
neperpyxa1Lcn payHn nnn 6onee onpepenennnHn po 1ex nop, noka cnnckn nx apryHen1oa
poc1a1ouno pasnnunm c 1oukn spennn koHnnnn1opa. D1o nokasmaae1 cnepykuee onncanne
knacca, ko1opoe neperpyxae1 qynkunk test sa cue1 ncnonLsoaannn uenouncnennoro
apryHen1a a opnoH cnyuae n aeuec1aennoro a ppyroH:
class A
{
public:
void test(int a)
{
cout << "This is the integer version\n";
}
void test(double b)
converted to PDF by BoJIoc
{
cout << "This is the floating point version\n";
}
};
C1apancL noporna1L coo6uenne nop nopxopnun He1op, C++ cnepaa npocHa1pnaae1
o6nac1L nHen, a ko1opo onpepenen cenek1op coo6uennn, a sa1eH nue1 nan6onee
nopxopnuyk qynkunk, onpepenennyk a npepenax a1o o6nac1n nHen. [axe ecnn ec1L
6onee nopxopnuan qynkunn, nacnepyeHan ns ppyroro npoc1panc1aa nHen, ona ne 6ype1
paccHa1pnaa1Lcn. D1o nnnkc1pnpye1cn cnepykunH onncanneH knaccoa:
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";
}
};
Honm1ka nocna1L coo6uenne test c aeuec1aennmH apryHen1oH akseHnnnpy knacca B
npnaepe1 k npepynpexpennk koHnnnn1opa, nockonLky a o6nac1n nHen, a ko1opo
koHnnnn1op nauen He1op c nHeneH test (a nHenno, any1pn knacca B), ne1 onpepenennn
qynkunn c aeuec1aennmH apryHen1oH. D1o nponcxopn1, necHo1pn na 1o, u1o nyxnan
qynkunn Hoxe1 6m1L ynacnepoaana o1 knacca A. PesynL1a1 6ype1 1eH xe caHmH nesaancnHo
o1 1oro, onncana qynkunn test kak virtual nnn ne1. 1o6m cnpaan1Lcn co aceH a1nH,
nporpaHHnc1y nyxno onpepenn1L o6e aepcnn a knacce B, opna ns ko1opmx 6ype1 npoc1o
amsmaa1L coo1ae1c1aykuyk qynkunk popn1enn:
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. BupuHoc1u a Java
Kak Hm anpenn na npnHepax nporpaHH a nsmke Java, Hopnqnka1opm private n public
pasHeuak1cn o1penLno pnn kaxporo nonn pannmx n qynkunn-unena.
Java aaopn1 noam nn1epecnm Hopnqnka1op c nHeneH final. OnnanLnm knacc ne Hoxe1
nopoxpa1L nopknaccm. Okonua1enLnm He1op ne Hoxe1 nepeonpepenn1Lcn ppyrnH
He1opoH. HepeHenno akseHnnnpa, onncanno kak final, nenLsn npncaanaa1L snauennn.
HcnonLsoaanne knkueaoro cnoaa final nosaonne1 koHnnnn1opy on1nHnsnpoaa1L kop.
converted to PDF by BoJIoc
Kak n a C++, Hopnqnka1op private a nsmke Java o1nocn1cn k knaccaH, a ne k akseHnnnpaH.
[nn akseHnnnpoa opnoro knacca paspeuen poc1yn k sakpm1mH nonnH pannmx ppyr ppyra.
[pyroe cpepc1ao ynpaanennn o6nac1Lk anpnHoc1n, npepoc1aanneHoe nsmkoH Java, a1o
nake1m. Hake1 copepxn1 knaccm n nn1epqecm. Hake1m cnyxa1 pnn 6eskonqnnk1noro
ynpaanennn 6onLunHn o6nac1nHn nHen. Hake1 onncmaae1cn c noHouLk knkueaoro cnoaa
package, ko1opoe ponxno 6m1L nepamH onepa1opoH a qane:
package packageName;
Kop opnoro nake1a Hoxe1 ccmna1Lcn na knaccm n nn1epqecm ns ppyroro nake1a, nn6o nano
ykasmaan nake1, a ko1opoH naxopn1cn o6Lek1, nn6o nHnop1npyn aecL nake1. Cnepykune
koHanpm nnnkc1pnpyk1 nepam HexannsH:
// nonyuwis iwn zann-x foo ws naxeia bar
bar.foo newObj = new bar.foo();
HHnop1npoaanne nake1a penae1 nHena acex ero o1kpm1mx knaccoa n nn1epqecoa
poc1ynnmHn 1ak xe, kak ecnn 6m onn 6mnn onpepenenm a 1ekyueH qane:
// wmnopiwposais sce o+exi- w wniepenc-
// ws naxeia c wmenem bar
import bar.*;
Hpn xenannn Hoxno sapa1L nHena o1penLnmx o6Lek1oa n nn1epqecoa aHec1o
ynnaepcanLnoro cnHaona *.
// wmnopiwposais wzeniwwxaiop foo
// ws naxeia bar
import bar.foo;
17.3.5. BupuHoc1u a Objective-C
B nsmke Objective-C o6Lnanenne skseHnnnpnmx nepeHennmx ponxno noHeua1Lcn a
nn1epqecnoe onncanne knacca. HenLsn o6Lnann1L noame nonn a paspene peannsaunn
(paxe necHo1pn na 1o, u1o a1n nonn ne c1any1 uac1Lk nn1epqeca), nockonLky onn
poc1ynnm 1onLko nsny1pn He1opoa (a 1epHnnax nsmka C++ onn nannk1cn saunuennmHn).
BnpnHoc1L akseHnnnpnmx nepeHennmx Hopnqnunpye1cn c noHouLk knkueaoro cnoaa
@public, ko1opoe penae1 ace nonn, cnepykune sa knkueamH cnoaoH, poc1ynnmHn pnn
nonLsoaa1enn. HanpnHep, cnepykun npnHep nokasmaae1 onncanne nn1epqeca knacca
Ball, ko1opm npepc1aanne1 co6o rpaqnueckn o6Lek1-uap. Honoxenne uapa sapae1cn
kooppnna1aHn, xpannunHncn a nonnx pannmx x n y. Ono o6uepoc1ynno, a 1o apeHn kak
nanpaanenne panxennn n aneprnn uapa nannk1cn saunuennmHn.
@interface Ball : Object
{
double direction;
double energy;
@public
double x;
double y;
}
B o1nnune o1 akseHnnnpnmx nepeHennmx a paspene implementation paspeuae1cn onncmaa1L
He1opm, ne ynoHnny1me a nn1epqecno uac1n. Takne He1opm okasmaak1cn anpnHmHn n
Hory1 amsmaa1Lcn 1onLko a 1o uac1n nporpaHHnoro kopa, ko1opan cnepye1 sa
onpepenenneH noaoro He1opa.
HenLsn cospa1L He1op, ko1opm 6m amsmaancn knnen1aHn-nopknaccaHn, no npn a1oH 6mn
6m ne poc1ynen knnen1aH-nonLsoaa1ennH. Takxe neaosHoxno aaec1n nc1nnno sakpm1me
snauennn akseHnnnpnmx nepeHennmx.

converted to PDF by BoJIoc
17.4. HpepuaHepeuuoe saenneuue
Xo1n, kak npaanno, nporpaHHnc1m c1apak1cn ns6era1L sauennennn Hexpy qparHen1aHn
kopa, nnorpa ono nonesno. Hpepc1aaL1e ce6e, nanpnHep, u1o am Hopennpye1e nekn
pnnaHnueckn o6Lek1. Make1 rpaqnueckn o1o6paxae1cn a rpaqnueckoH okne (nnn oknax),
ko1opoe ponxno nenpepmano o6noann1Lcn.
Cnepyn nsnoxennmH amue coo6paxennnH, Hm ponxnm 6mnn 6m ns6era1L sauennennn a
nporpaHHe, 1o ec1L cnnukoH 1ecnoro coepnnennn Hopenn n ee rpaqnueckoro o6pasa. B
uac1noc1n, Hopenn ne cnepye1 sna1L, kak ona o1o6paxae1cn na akpane (nanpnHep, kak
npepc1aannk1cn snauennn: uncnenno nnn rpaqnueckn). Kak Hoxe1 HopenL 6es 1ecno cansn
c He1opaHn o1o6paxennn no1pe6oaa1L o1 nnx o6noanennn akpana?
Opnn ns cnoco6oa ns6exa1L cnnukoH cnnLno asanHosaancnHoc1n Hexpy cansannmHn
koHnonen1aHn ncnonLsoaa1L apHnnnc1pa1op saancnHoc1e. On nanne1cn c1anpap1no
uac1Lk run-time 6n6nno1ek a Smalltalk n Objective-C, no Hoxe1 6m1L nerko ckonc1pynpoaan
n pnn ppyrnx nsmkoa (nanpnHep, C++). Ocnoanan npen coc1on1 a 1oH, u1o apHnnnc1pa1op
saancnHoc1e pec1aye1 kak nocpepnnk, o6cnyxnaan cnncok o6Lek1oa n ppyrnx koHnonen1,
o1 nnx saancnunx. Mopenn 1pe6ye1cn sna1L 1onLko o6 apHnnnc1pa1ope saancnHoc1e.
O6Lek1m-o1o6paxennn pernc1pnpyk1 ce6n c noHouLk apHnnnc1pa1opa saancnHoc1e,
ykasmaan npn a1oH, u1o onn saancn1 o1 o6Lek1a-Hopenn. Bnocnepc1ann o6Lek1-HopenL npn
caoeH nsHenennn nocmnae1 epnnc1aennoe coo6uenne apHnnnc1pa1opy saancnHoc1e. TeH
caHmH nocnepnn ysnae1, u1o HopenL nsHennnacL n ace saancnHme o1 nee koHnonen1m
ponxnm 6m1L onoaeuenm o6 a1oH. 3aancnHme koHnonen1m nonyuak1 coo6uennn o1
apHnnnc1pa1opa saancnHoc1e, ko1opme nsaeuak1, u1o HopenL nsHennnacL n ponxnm 6m1L
npepnpnnn1m napnexaune pec1ann.
Takan cnc1eHa o6cnyxnaannn saancnHoc1e noHorae1 nyuue nsonnpoaa1L koHnonen1m ppyr
o1 ppyra, u1o yHenLuae1 konnuec1ao canse any1pn nporpaHHm. Opnako a o1nnune o1
cxeHm, onncanno a noppaspene 17.1.6, ona pa6o1ae1, 1onLko ecnn saancnHme koHnonen1m
snak1, u1o k1o-1o oxnpae1 nx nsHenennn. Ona ne 6ype1 pa6o1a1L, ecnn, kak a cnyuae
npnHepa o6Lek1a Reactor, xena1enLno HnnnHnsnpoaa1L aHeua1enLc1ao a ncxopnm kop
Hopenn.
Ynpaxueuun
1. Paspa6o1a1e cpepc1ao, o6napyxnaakuee napyuennn sakona [eHe1epa, a
nporpaHHax, nanncannmx na aaueH nk6nHoH o6Lek1no-opnen1npoaannoH nsmke.
2. CnnLnan qopHa sakona [eHe1epa sanpeuae1 poc1yn k akseHnnnpaH knacca no1oHka.
Onnun1e npenHyuec1aa n nepoc1a1kn a1oro orpannuennn. PaccHo1pn1e 1akne
HoHen1m, kak sauennenne Hexpy knaccaHn n nonn1noc1L kopa nporpaHHm.
3. He kaxe1cn nn aaH, u1o cnnLnan qopHa sakona [eHe1epa ponxna orpannunaa1L
poc1yn 1akxe n k rno6anLnmH nepeHennmH? O6ocny1e caoe Hnenne. O6pyHmaan
o1ae1, am Hoxe1e o6pa1n1Lcn k c1a1Le BynLqa n Loy [Wulf 1973].
4. Kakne eue Hoxno npepnoxn1L konkpe1nme npaanna, ananornunme sakony
[eHe1epa, 1akne, u1o:
o nx amnonnenne o6muno npnaopn1 k cnc1eHaH c HenLue
asanHosaancnHoc1Lk;
o cn1yaunn, korpa npaanna ponxnm napyua1Lcn, ac1peuak1cn pepko.
3akon [eHe1epa nocanuen sauennennk Hexpy pasnnunmHn o6Lek1aHn. Moxno nn
npepnoxn1L npaanna, ko1opme c1nHynnpoaann 6m 6onLuyk cansnoc1L any1pn
o6Lek1a?
converted to PDF by BoJIoc
5. 3aunuennme pannme (pnn knnen1oa-nopknaccoa) sannHak1 npoHexy1ounoe
nonoxenne no c1enenn sakpm1oc1n Hexpy o1kpm1mHn n sakpm1mHn nonnHn.
Hpepc1aaL1e ce6e ananornunyk npoHexy1ounyk c1ynenL Hexpy cnepykunHn
nonoxennnHn: poc1yn k o6Lek1y paspeuen koHy yropno n poc1yn k o6Lek1y
ocyuec1anne1 1onLko caH o6Lek1. HanpnHep: akseHnnnpm neko1oporo knacca (n
1onLko onn) Hory1 nHe1L poc1yn ko any1penneHy coc1onnnk o6Lek1a. B nsmke
C++, nanpnHep, akseHnnnp knacca nHee1 poc1yn k nk6oHy nonk pannmx ppyroro
o6Lek1a 1oro xe knacca, paxe ecnn a1n nonn onncanm kak private nnn protected. B
ppyrnx nsmkax (nanpnHep, a Smalltalk) 1akoe ne paspeuae1cn. O6cypn1e
npenHyuec1aa n nepoc1a1kn kaxporo ns nopxopoa.
6. [pyran aosHoxnan aapnaunn na 1eHy npaann anpnHoc1n pnn knnen1oa-nopknaccoa
coc1on1 a 1oH, u1o6m paspeun1L poc1yn k nenocpepc1aennoHy popn1enk knacca, no
ne k 6onee ypanennmH npepkaH. O6cypn1e poc1onnc1aa n nepoc1a1kn a1o npen (cH.
[Snyder 1986]).
7. Hs paccHo1pennmx naHn nsmkoa 1onLko C++ n Delphi Pascal nHek1 nanme cpepc1aa
pnn o1penennn caoc1a, poc1ynnmx knnen1aH-nopknaccaH, o1 caoc1a, poc1ynnmx
knnen1aH-nonLsoaa1ennH. TeH ne Henee ace nsmkn nHek1 cao HexannsH onncannn
naHepenn nporpaHHnc1a. 3auac1yk ncnonLsyk1cn c1pyk1ypnpoaannme
koHHen1apnn (pnpek1nam koHnnnn1opa) pnn nepepaun pononnn1enLno nnqopHaunn
cnc1eHe, o6cnyxnaakue nsmk nporpaHHnpoaannn. Onnun1e cornauennn no
koHHen1npoaannk, ko1opme 6m noHornn onpepenn1L ypoaenL anpnHoc1n. 3a1eH
oxapak1epnsy1e anropn1H, ko1opm ponxen ncnonLsoaa1Lcn nporpaHHnmH
cpepc1aoH pnn o6ecneuennn amnonnennn npaann anpnHoc1n.
Fnaaa 18

Cpepm u cxeHm paspa6o1ku
Knacc a1o HexannsH nnkancynnpoaannn peuennn konkpe1no sapaun. On npepoc1aanne1
onpepenennm cepanc, noc1aanne1 pannme n o6ecneunaae1 neo6xopnHoe noaepenne.
Knaccm coaHec1nmHn ycnnnnHn amnonnnk1 sapauy, aosnoxennyk na npnknapnyk
nporpaHHy. MexannsH knaccoa nonesen 1eH, u1o on nosaonne1 pas6n1L nporpaHHy na
koHnonen1m, ko1opme anannsnpyk1cn nesaancnHo ppyr o1 ppyra. Opnako o1penLnme knaccm
caHn no ce6e pepko npepoc1aannk1 peuenne ace npo6neHm.
Yun1maan a1o1 qak1, sa nocnepnne neckonLko ne1 aospoc nn1epec k cnoco6aH, c noHouLk
ko1opmx knaccm Hory1 pa6o1a1L coaHec1no nap peuenneH npo6neHm. Han6onee
nonynnpnmHn c1ann pae npen: cpepa paspa6o1kn n cxeHm paspa6o1kn. B nocnepykunx
paspenax Hm onnueH kaxpyk ns nnx 6onee noppo6no.
18.1. Cpepa paspa6o1ku
Cpepa paspa6o1kn a1o na6op knaccoa, 1ecno co1pypnnuakunx Hexpy co6o. BHec1e onn
npepc1aannk1 paspa6o1ky, npepnasnauennyk pnn Hnorokpa1noro ncnonLsoaannn n
npnropnyk pnn peuennn o6unx npo6neH. CaHoe pacnpoc1panennoe npnHenenne cpep
paspa6o1kn saknkuae1cn a cospannn rpaqnuecknx nn1epqecoa nonLsoaa1enn nnn GUI-
npnnoxenn. Mm noppo6no paccHo1pnH opno ns 1aknx npnnoxenn a rnaae 19. Opnako
pannan konuenunn npnHenne1cn ne 1onLko npn paspa6o1ke nonLsoaa1enLckoro nn1epqeca.
HanpnHep, cyuec1ayk1 cpepm, ko1opme paccun1anm na noc1poenne pasnoo6pasnmx
pepak1opoa, koHnnnn1opoa, qnnancoamx Hopene [Gamma 1995, Deutsch 1989,
Weinand 1988].
converted to PDF by BoJIoc
Mm acerpa HoxeH a6c1parnpoaa1Lcn n o6cyxpa1L aneHen1m paspa6o1kn, na ko1opmx
ocnoamaae1cn cpepa paspa6o1kn, no o1penLnoc1n. Opnako, kak npaanno, 1akan cpepa
oc1ae1cn na6opoH cneunqnuecknx knaccoa, ko1opme o6muno peannsoaanm 1onLko pnn opno
konkpe1no nna1qopHm. B kauec1ae npnHepa Hoxno npnaec1n cpepy Model-View-Controller a
nsmke Smalltalk nnn 6n6nno1eky OWL na PC.
Cpepa pnk1ye1 o6uyk c1pyk1ypy npnnoxennn. Ona onncmaae1, kak pacnpepenenm
o6nsannoc1n Hexpy pasnnunmHn koHnonen1aHn n kak nocnepnne ponxnm
asanHopec1aoaa1L Hexpy co6o. HpenHyuec1ao cpepm coc1on1 a 1oH, u1o paspa6o1unky
noaoro npnnoxennn nyxno acero nnuL ckonuen1pnpoaa1Lcn na cneunqnke peuaeHo a
pannm HoHen1 npo6neHm. Bonee pannne paspa6o1kn a 1o xe cpepe ne 1pe6yk1
pnn1enLno nopro1oakn k noa1opnoHy ncnonLsoaannk, a nx kop ne nyxpae1cn a
nepenncmaannn.
HanpnHep, Manan Cpepa Paspa6o1kn LAF, o ko1opo Hm pacckasmaaeH a rnaae 19, copepxn1
npnHepno pkxnny knaccoa. Uen1panLnm knacc application peannsye1 noaepenne,
oxnpaeHoe o1 okon: cnoco6noc1L k panxennk n nsHenennk pasHepa. Opnako on ne
peannsye1 qynkunonanLnoc1L, 1nnnunyk pnn konkpe1noro npnnoxennn. Oxnpae1cn, u1o
nonLsoaa1enL cpepm cospac1 noam knacc, no1oHka application. B neH 6ypy1
nepeonpepenenm neko1opme He1opm (nanpnHep, peakunn na nepeHeuenne Hmun nnn
cnoco6 nepepncoamaannn okna). Hnxe npnaopn1cn onncanne knacca pnn kap1ounoro
nacLnnca, nopo6noro 1oHy, u1o 6mn nanncan naHn a rnaae 8. 3pecL knacc paspa6a1maae1cn
na ocnoae LAF.
class cardApp : public application
{ public:
// xoncipyxiop
cardApp();
// nosezenwe, cneuwwunoe znn npwno+enwn
virtual void mouseButtonDown(int x, int y);
virtual void paint();
private:
CardPile * piles[13];
};
Cospaaan akseHnnnp a1oro knacca, nonLsoaa1enL cpasy xe nonyuae1 rpaqnueckoe okno pnn
caoero npnnoxennn. Paspa6o1unky ne1 nyxpm 6ecnokon1Lcn o 1oH, kak nannca1L aecL kop
okna, a snaun1, Hoxno cocpepo1oun1Lcn na noaepennn, cneunqnunoH pnn npnnoxennn.
Hepoc1a1ok epnno o6ue cpepm paspa6o1kn a 1oH, u1o ona xec1ko orpannunaae1 qopHy
npnnoxennn. HanpnHep, cpepa LAF ynpouae1 cospanne opnookonnmx npnnoxenn,
ncnonLsykunx knonkn, Henk n 1ekc1oame o6nac1n, no ona apnp nn noHoxe1 aaH npn
noc1poennn Hnorookonnoro npnnoxennn. Cnn1ne a1oro orpannuennn 1pe6ye1 rno6anLnoro
nepecHo1pa ace cpepm, 1eH caHmH y1paunaae1cn nepaonpnunna ee ncnonLsoaannn.
B npnnoxennnx 1papnunonnoro 1nna kop, cneunqnunm pnn konkpe1no sapaun,
onpepenne1 o6un no1ok amuncnenn, apeHn o1 apeHenn amsmaan 6n6nno1eunme
npouepypm (1akne, kak Ha1eHa1nueckne nopnporpaHHm nnn onepaunn aaopa/amaopa) pnn
amnonnennn neko1opmx cneunqnuecknx qynkun.

converted to PDF by BoJIoc
Hpn ncnonLsoaannn cpepm paspa6o1kn no1ok ynpaanennn onpepenne1cn cpepo n ne
Henne1cn o1 npnnoxennn k npnnoxennk. Hepeonpepennk1cn nopnporpaHHm, amsmaaeHme
cpepo, no o6uan c1pyk1ypa oc1ae1cn ynnaepcanLno. TaknH o6pasoH, cpepa paspa6o1kn
poHnnnpye1 nap cneunqnuecknH kopoH.

TaknH o6pasoH, Hm na6nkpaeH cHeny pone o6uero n cneunqnueckoro kopoa. Hoa1oHy
nnorpa cpepa paspa6o1kn nasmaae1cn 6n6nno1eko nansnanky [Wilson 1990].
18.1.1. Java API
[pyrnH npnHepoH cpepm paspa6o1kn nanne1cn Java API (Application Programming Interface,
nporpaHHnm nn1epqec npnnoxenn). On coc1on1 ns knaccoa, ncnonLsyeHmx
nporpaHHnc1oH pnn konc1pynpoaannn noamx npnnoxenn Java, nasmaaeHmx annne1aHn.
Kak n a cnyuae co cpepo LAF, cospanne noaoro knacca, nacnepnnka 6asoaoro knacca API,
a1o ocnoano HexannsH aknkuennn noaoro noaepennn a npnnoxenne.
OynpaHen1anLnm knacc pnn acex Java-npnnoxenn nasmaae1cn Applet. On onpepenne1
o6uyk c1pyk1ypy npnnoxennn uepes He1op main, ko1opm o6muno ne nepeonpepenne1cn
nporpaHHnc1aHn. D1o1 He1op amsmaae1 pnp ppyrnx He1opoa, ko1opme nepeonpepennk1cn,
u1o6m o6ecneun1L noaepenne, cneunqnunoe pnn npnnoxennn. Heko1opme ns amsmaaeHmx
He1opoa akpa1ue nepeuncnenm nnxe:
init () amsmaae1cn npn nnnunannsaunn annne1a
start () amsmaae1cn a nauane pa6o1m npnnoxennn
paint (Graphics) amsmaae1cn npn nepepncoake okna
mouseDown (Event, int, int) amsmaae1cn npn naxa1nn knonkn Hmun
keyDown (Event, int, int) amsmaae1cn npn naxa1nn knaanun
stop () amsmaae1cn npn ypanennn okna
KpoHe 1oro, cpepa API npepoc1aanne1 6ora1yk konnekunk knaccoa pnn
konc1pynpoaannn knonok n Henk, amaopa 1ekc1a c ncnonLsoaanneH upnq1oa,
pa6o1m c uae1oH, amnonnennn Ha1eHa1nuecknx pec1an n Hnororo ppyroro.
18.1.2. Cpepa Hopenupoaauun
[nn 1oro u1o6m nponnnkc1pnpoaa1L, u1o ne ace cpepm ponxnm 6m1L cansanm c
nn1epqecaHn nonLsoaa1enn, Hm akpa1ue paccHo1pnH cpepy Hopennpoaannn, ko1opyk
Hoxno ncnonLsoaa1L, nanpnHep, npn Hopennpoaannn 6nnLnppnoro uapa (cH. rnaay 6). Kak
onncano a nocnepne uac1n rnaam, Hm Hornn 6m naua1L c onpepenennn acex o6Lek1oa a
Hopenn kak nopknaccoa o6uero knacca rpaqnuecknx o6Lek1oa:
GraphicalObject = object
(* nonn zann-x *)
link : GraphicalObject;
region : Rect;
(* wnwuwanwswpyxmwe ynxuww *)
procedure setRegion(left, top, right, bottom :
integer);
(* onepauww, s-nonnnem-e rpawuecxwmw o+exiamw *)
procedure draw;
converted to PDF by BoJIoc
procedure erase;
procedure update;
function intersect(anObj : GraphicalObject) :
boolean;
procedure hitBy(anObj : GraphicalObject);
end;
Ipaqnueckne o6Lek1m sannHak1 o6nac1L na akpane, snak1, kak napncoaa1L caHnx ce6n, n
coo6uak1 o nepeceuennn c ppyrnHn o6Lek1aHn. TaknH o6pasoH, cpepa Hopennpoaannn
cospana, konL ckopo onpepenen knacc o6uero nasnauennn pnn pa6o1m c rpaqnuecknHn
o6Lek1aHn:
GraphicalUniverse = object
(* nonn zann-x *)
moveableObjects : GraphicalObject;
fixedObjects : GraphicalObject;
continueUpdate : boolean;
(* meioz- *)
procedure initialize;
procedure installFixedObject (newObj :
GraphicalObject);
procedure installMovableObject (newObj :
GraphicalObject);
procedure drawObjects;
procedure updateMovableObjects;
procedure continueSimulation;
end;
Ocnoaa cpepm Hopennpoaannn a1o nopnporpaHHa pnn o6noanennn acex nepeHeuaeHmx
o6Lek1oa. Ona npoc1o npoxopn1 no cnncky nepeHeuaeHmx o6Lek1oa, coo6uan kaxpoHy ns
nnx o neo6xopnHoc1n o6noan1L ce6n. Ecnn kako-nn6ypL ns o6Lek1oa npocn1 npoponxn1L
unkn o6noanennn (ny1eH amsoaa npouepypm continueSimulation), ero npocL6a
ypoane1aopne1cn. B npo1nanoH cnyuae Hopennpoaanne npepmaae1cn:
procedure GraphicalUniverse.updateMovableObjects;
var
currentObject : GraphicalObject;
begin
repeat
continueUpdate := false;
currentObject := movableObjects;
while currentObject <> nil do
begin
currentObject.update;
currentObject := currentObject.link;
end
until not continueUpdate
end;
B pesynL1a1e cpepa Hopennpoaannn nnuero ne snae1 o konkpe1noH npnnoxennn, pnn
ko1oporo ona 6ype1 ncnonLsoaa1Lcn, n, 1aknH o6pasoH, Hoxe1 npnHenn1Lcn pnn
Hopennpoaannn 6nnLnppnmx uapoa, pm6ok a akaapnyHe, a akonornueckoH nccnepoaannn o
kponnkax n aonkax, a 1akxe ao Hnoxec1ae ppyrnx npnnoxenn.
[pyro 1nn Hopennpoaannn ynpaanne1cn co6m1nnHn. Co6m1nn xpann1cn a npnopn1e1no
ouepepn, ynopnpouenno no apeHenn nx aosnnknoaennn. Coo1ae1c1aykune snauennn
nsanekak1cn ns ouepepn n nocnepoaa1enLno amnonnnk1cn. Kaxpoe co6m1ne Hoxe1 amsaa1L
noame co6m1nn, ko1opme 1akxe po6aannk1cn a ouepepL co6m1n. Cpepa pnn paspa6o1kn
npnnoxenn 1akoro 1nna onncana a [Budd 1994].
converted to PDF by BoJIoc
18.2. CxeHm paspa6o1ku
Cpepa paspa6o1kn a1o na6op konkpe1nmx knaccoa, paspa6o1annmx pnn cospannn
npnnoxenn onpepenennoro popa na 6ase konkpe1no nna1qopHm. Honn1ne cxeHm
paspa6o1kn ropaspo 6onee aHopqno. CxeHa paspa6o1kn aonnouae1 a ce6e xapak1epnme
cnoco6m peuennn sapau, 1o ec1L npen, ko1opme noc1onnno ncnonLsyk1cn npn peannsaunn
npnnoxenn a pasnmx npepHe1nmx o6nac1nx. O6muno 1akne cxeHm onpepennk1
asanHopec1ane Hexpy neckonLknHn o6Lek1aHn nnn knaccaHn. Hy1eH cnc1eHa1nueckoro
nsyuennn cxeH Hoxno nonyun1L npepc1aanenne o unpokoH kpyre ro1oamx aosHoxnmx
peuenn nk6o konkpe1no npo6neHm. B 6ypyueH a1o noHoxe1 pacnosna1L
o6c1on1enLc1aa, rpe npnHennHa 1a nnn nnan cxeHa.
18.2.1. CxeHm c nocpepuukoH
Hponnnkc1pnpyeH pannyk konuenunk na npnHepe. O6uan npen cxeHm Hoxe1 6m1L
ampaxena opnnH cnoaoH: nopc1anoaka. B 1ako cxeHe nHee1cn o6Lek1-knnen1, ko1opm
pyHae1, u1o on asanHopec1aye1 c ppyrnH o6Lek1oH (nocpepnnkoH). Opnako na caHoH pene
nocpepnnk ne amnonnne1 1pe6yeHmx pec1an nx coaepuae1 1pe1n o6Lek1, nasmaaeHm
ncnonnn1eneH. D1a npoc1an konuenunn Hoxe1 nHe1L Hnoxec1ao aonnouenn. Bo1
neko1opme ns nnx.
Hepepava. Hocpepnnk a1o aren1, ko1opm nocmnae1 sanpocm no onpepenennmH
kananaH, nanpnHep uepes koHnLk1epnyk ce1L. Oak1nueckoe ncnonnenne sanpoca
nponsaopn1 cepaep na ppyroH konue ce1n. O1ae1 na sanpoc nepecmnae1cn o6pa1no.
HcnonLsoaanne nocpepnnka ckpmaae1 pe1ann nepepaun nnqopHaunn, npnpaaan npoueccy
anpnHoc1L aneHen1apnoro coo6uennn.

4acap. Oak1nueckan pa6o1a no o6cnyxnaannk sanpoca knnen1a amnonnne1cn ne opnnH
o6Lek1oH, a ckopee coaokynnoc1Lk asanHopec1aykunx o6Lek1oa. Hocpepnnk pec1aye1
kak 1ouka qokyca, ns ko1opo sanpocm nepecmnak1cn k coo1ae1c1aykueHy ncnonnn1enk. B
1ako cxeHe sa cue1 o6ecneuennn epnnoro npoc1oro nn1epqeca knnen1aH ne nyxno
noHnn1L cnncok acex o6Lek1oa, amnonnnkunx qak1nueckoe o6cnyxnaanne sanpocoa.

Tpaucnn1op (unu apan1ep). Oak1nueckan pa6o1a nponsaopn1cn opnnH o6Lek1oH, opnako
npo1okon, ncnonLsyeHm a1nH o6Lek1oH, o1nnuae1cn o1 npo1okona knnen1a. Hocpepnnk
pec1aye1 kak nepeaopunk, nepeaopn coo6uennn knnen1a na nsmk, nonn1nm ncnonnn1enk.

Qekopa1op (ou xe ynakoauk, ou xe qunu1p). B 1o apeHn kak ncnonnn1enL amnonnne1
ocnoano o6LeH pa6o1, nocpepnnk anocn1 caok ne6onLuyk nen1y. Hpn a1oH nn1epqec
oc1ae1cn 6es nsHenennn. K npnHepy, pekopa1op Hoxe1 o6ecneunaa1L o6paHnenne okna
(nanpnHep, nonocm npokpy1kn), a 1o apeHn kak ncnonnn1enL o6noanne1 copepxnHoe okna.
Knnen1 asanHopec1aye1 c qnnL1poH, kak 6yp1o a1o ncnonnn1enL, xo1n ocnoanan uac1L
converted to PDF by BoJIoc
pa6o1m qak1nueckn amnonnne1cn ppyrnH o6Lek1oH. Hpn 1akoH nopxope qnnL1p npospauen
pnn knnen1a. ac1o qnnL1p a1o Henee poporoc1onuan n 6onee rn6kan anL1epna1naa, ueH
cospanne nopknaccoa, nockonLky ero Hoxno po6aan1L nnn y6pa1L ao apeHn ncnonnennn
nporpaHHm.

Moc1 (unu coc1onuue). B pannoH cnyuae nocpepnnk onn1L-1akn o1aeuae1 sa nn1epqec,
no ne sa amnonnenne noc1aanenno sapaun, ko1opan peuae1cn neckonLknHn aosHoxnmHn
cnoco6aHn, npnueH am6op 1oro nnn nnoro cnoco6a saancn1 o1 apeHenn. Hocpepnnk
amsmaae1 na ncnonnenne nopxopnun aapnan1. HcnonLsoaanne nocpepnnka o6ecneunaae1
epnnm nn1epqec, a 1o apeHn kak qak1nueckoe amnonnenne aapLnpye1cn a saancnHoc1n
o1 apeHenn nnn ycnoan (coc1onnnn).

CxeHm c nocpepnnkoH uaue acero aosnnkak1 a cn1yaunn, korpa npoHexy1ounm o6Lek1
o1nocn1enLno nerkoaecen, 1o ec1L on caH ne amnonnne1 nou1n nnkako pa6o1m, a aHec1o
a1oro npoc1o nepepae1 koHanpm ppyrnH aren1aH. HanpnHep, cxeHa nn1epnpe1a1opa,
onncannan a cnepykueH paspene, Horna 6m a neko1opoH cHmcne paccHa1pnaa1Lcn kak
pasnoanpnoc1L cxeHm c nocpepnnkoH, nockonLky knnen1 asanHopec1aye1 c opnnH
o6Lek1oH (nn1epnpe1a1opoH), ko1opm npepc1aanne1 co6o qacap ropaspo 6onee cnoxno
c1pyk1ypm. Opnako nn1epnpe1a1op no caoe npnpope 1akxe poaonLno 1nxenoaecen. B
a1oH cHmcne on o1nnuae1cn o1 oc1anLnmx cxeH npoek1npoaannn, onncannmx a a1oH paspene.
18.2.2. CxeHm o6xopa
Eue opna pacnpoc1panennan pasnoanpnoc1L cxeH paspa6o1kn aosnnkae1, korpa snauennn
pannmx oprannsyk1cn a nepapxnueckyk c1pyk1ypy (nanpnHep, paonunoe pepeao). Tnnnunan
sapaua coc1on1 a 1oH, u1o6m npo1n no kaxpoHy ysny a1oro pepeaa. Mennk1cn knnen1m,
npoxopnune no ysnaH, nnn caHn ysnm, nnn xe n 1o n ppyroe.

Hoce1n1enL. B nepaoH cnyuae snauennn ysnoa a nepapxnn opno1nnnm, opnako nHee1cn
Hnoro pasnnunmx noce1n1ene, ko1opme npoxopn1 no ysnaH pepeaa. HpnHepoH cnyxn1
paonunoe pepeao, a ko1opoH kaxpm ysen o1nocn1cn k cnepykueHy knaccy:
template class TreeNode
{
public:
TreeNode (T & initial);
void visit (Visitor & v);
private:
T value;
TreeNode * left;
TreeNode * right;
converted to PDF by BoJIoc
};
3pecL kaxpm ysen copepxn1 snauenne n ykasa1enn na npaam n neam ysnm nnxnero
ypoann; kaxpm ns ykasa1ene Hoxe1 6m1L nyneamH.
Bce noce1n1enn ysnoa pepeaa ponxnm nponcxopn1L ns o6uero knacca Visitor. Onn
nepeonpepennk1 anp1yanLnm He1op c nHeneH action. Me1op visit knacca TreeNode
ocyuec1anne1 a onpepenennoH nopnpke npocHo1p ysnoa, amnonnnn pnn kaxporo ysna
pec1ane action. AnL1epna1nao Hoxe1 6m1L kopnpoaanne npoxopa no ysnaH co c1oponm
knnen1a, opnako o6muno npoue (k 1oHy xe, a1o 6onee coo1ae1c1aye1 npenH OOH) nopyun1L
npoxop co6c1aenno pepeay:
TreeNode::visit (Visitor & v)
{
v.action(value);
if (left != NULL) left->visit(v);
if (right != NULL) right->visit(v);
}
HsHenunaoc1L aosnnkae1 ns-sa o1noxennoro He1opa action. Hpouepypa visit ne snae1 a
1ounoc1n, kakoe nHenno pec1ane 6ype1 amnonnn1Lcn a1nH He1opoH. C ppyro c1oponm,
cxeHa 1pe6ye1, u1o6m ace noce1n1enn 6mnn no1oHkaHn o6uero knacca Visitor.
Hu1epnpe1a1op. CxeHa, ko1opan aneune nanoHnnae1 npnaepennyk amue, aosnnkae1,
korpa coc1aanan c1pyk1ypa ace eue nanne1cn nepapxnuecko, no yxe ne opnopopno.
HpnHepoH nanne1cn a6c1pak1noe cnn1akcnueckoe pepeao. Ono npepc1aanne1 c1pyk1ypy
perynnpnoro ampaxennn, ko1opoe coc1on1 ns ysnoa am6opa, noa1opennn n cnHaonoa. Ysnm
noa1opennn n nepeknkuennn copepxa1 a ce6e noppepeaLn coc1onnnn, ko1opme
npepc1aannk1 co6o pasnnunme anL1epna1nam nnn noa1opnkuykcn c1pyk1ypy.
[annan cxeHa o1nnuae1cn o1 nepao 1eH, u1o pec1ane, ko1opoe neo6xopnHo amnonnn1L,
o6muno sakopnpoaano a caHoH pepeae, a ne onpepenne1cn aneune c1pyk1ypo pannmx.
HanpnHep, naueHy a6c1pak1noHy cnn1akcnueckoHy pepeay nepepae1cn 1ekc1oaan c1poka.
3apae1cn aonpoc: coo1ae1c1aye1 nn c1poka perynnpnoHy ampaxennk? 1o6m o1ae1n1L na
aonpoc, kaxpm ysen a ampaxennn nponsaopn1 pasnnunme nocnepoaa1enLnoc1n pec1an.
Ysnm-cnHaonm coo1ae1c1ayk1 1onLko 1ouno sapannmH aennunnaH. Ysen am6opa npoaepn1 no
ouepepn kaxpyk nHekuykcn anL1epna1nay, u1o6m amncnn1L, ne nopxopn1 nn opna ns nnx,
n 1. p. Knnen1 nepepae1 c1poky, ko1opan ponxna 6m1L npoaepena na coanapenne, kopnk
pepeaa, a sa1eH nonyuae1 o1ae1.
D1a cxeHa 1akxe o1nnuae1cn o1 nepao: c1pyk1ypa pannmx ne nHee1 nnoro asanHopec1ann
c knnen1oH, kpoHe kak nonyuennn axopno aennunnm n aosapauennn amuncnennoro
pesynL1a1a. Kak 1onLko He1op, onpepenennm kopneH pepeaa, amsmaae1cn knnen1oH,
pepeao nponsaopn1 cepnk pec1an, pakuyk pesynL1a1.
H1epa1op. Opna ns npo6neH co cxeHo 1nna noce1n1enL coc1on1 a 1oH, u1o knnen1 n
c1pyk1ypa pepeaa cnnukoH 1ecno cansanm Hexpy co6o. Ecnn knnen1 amnonnne1 o6xop
pepeaa, 1o a1o noppasyHeaae1, u1o on nHee1 poc1a1ouno nonnoe npepc1aanenne o c1pyk1ype
pepeaa. Ecnn pepeao ocyuec1anne1 o6xop ysnoa, 1o amnonnneHoe pec1ane ponxno 6m1L
eHy nsaec1no (no kpane Hepe, a qopHe o1noxennoro He1opa). Heo6xopnHoc1L
cornacoaannn Hexpy pepeaoH n knnen1oH Hoxe1 6m1L yHenLuena ny1eH noHeuennn
nocpepnnka Hexpy knnen1oH n snauennnHn pannmx.

Tako nocpepnnk nasmaae1cn n1epa1opoH. On nponsaopn1 npocHo1p c1pyk1ypm pepeaa,
nepepaaan knnen1y snauennn opno sa ppyrnH. TeH caHmH knnen1 nHee1 peno c nnocko,
converted to PDF by BoJIoc
nnneno c1pyk1ypo, a ne co cnoxno nepapxne, ncnonLsyeHo na caHoH pene pnn
xpanennn snauenn. Hn1epqec n1epa1opa o6muno amrnnpn1 cnepykunH o6pasoH:
initialize wnwuwanwswposais wiepaiop xopnem zepesa
current sepnyis iexymee snauenwe
atEnd sepnyis true, ecnw snauenwn onsme nei
advance sepeniw x cnezyxmemy snauenwx
HcnonLsyn n1epa1op, knnen1 amnonnne1 unkn, ananornunm cnepykueHy:
itr.initialize();
while not itr.atEnd() do
begin
... (* nexoiopoe zenciswe co snauenwem itr.current() *)
itr.advance();
end;
H1epa1op aqqek1nano paspenne1 knnen1a n qak1nueckyk c1pyk1ypy pannmx. E ne1
neo6xopnHoc1n sna1L, kak ncnonLsye1cn ee nnqopHaunn, a knnen1 ne snae1 o any1penneH
npepc1aanennn pannmx.
18.2.3. CxeHa paouo pucne1vepusauu
CxeHa paono pncne1uepnsaunn npnHenne1cn, korpa cyuec1ayk1 paa (kak HnnnHyH)
nc1ounnka nsHenunaoc1n npn o6Hene pannmHn. HanpnHep, ponyc1nH, u1o a cnyuae
npocHo1pa pepeaa, onncannoro a npepmpyue cxeHe, n knnen1, n ysnm nepapxnuecko
c1pyk1ypm aapLnpyk1 cao 1nn pannmx. Moxno npnaec1n n Hnoxec1ao ppyrnx npnHepoa.
Hpepc1aanH ce6e, u1o y nac ec1L pasnme HnoroyronLnnkn, npepc1aanennme a anpe 6asoaoro
knacca Shape, n coo1ae1c1aykune nopknaccm (Triangle, Square n 1. p.). HpepnonoxnH, y
nac ec1L paa yc1poc1aa amaopa: npnn1ep n 1epHnnan. Onn npepc1aannk1cn nopknaccaHn
knacca Device. KoHanpm, neo6xopnHme pnn amnonnennn onepaunn amaopa na a1n
yc1poc1aa, nac1onLko pasnnunm, u1o o6un nn1epqec neaosHoxen. BHec1o a1oro kaxpan
qnrypa caHa nnkancynnpye1 nnqopHaunk o 1oH, kak neua1a1L ee na npnn1epe n kak
nso6paxa1L ee na 1epHnnane:
class Triangle : public Shape
{
public:
Triangle (Point, Point, Point);
// ...
virtual void displayOnPrinter (Printer);
virtual void displayOnTerminal (Terminal);
// ...
private:
Point p1, p2, p3;
};
Bonpoc: kak nonnHopqnyk qnrypy Shape (nnn nk6o ee nopknacc) o1o6pasn1L na
nonnHopqnoH yc1poc1ae 1nna Device? O1ae1 coc1on1 a 1oH, u1o nyxno ncnonLsoaa1L
nepecmnky coo6uenn pnn amnaannaannn opnoro ns payx snauenn. [nn onpepenennn
o6enx aennunn Hm npoc1o no ouepepn nocmnaeH nH coo6uenne. (O6o6uenne a1o npen na
cnyua 1pex n 6onee nepeHennmx oueanpno nepeunn1e coo6uenne kaxpo nensaec1no
nepeHenno.)
HanpnHep, cnauana Hm nepepaeH koHanpy c apryHen1oH 1nna Shape na yc1poc1ao amaopa.
D1a koHanpa onncana kak o1noxennan a knacce Device n nepeonpepenne1cn a kaxpoH ero
nopknacce. Hoa1oHy nepecmnka coo6uennn am6npae1 pnn amnonnennn npaannLnyk
qynkunk:
function Printer.display (Shape aShape)
begin
converted to PDF by BoJIoc
aShape.displayOnPrinter(self);
end;
function Terminal.display (Shape aShape)
begin
aShape.displayOnTerminal(self);
end;
O6pa1n1e annHanne: He1op display ne snae1 o 1oH, kakan qnrypa pncye1cn a nac1onun
HoHen1. Ho kaxpm ns He1opoa displayOnPrinter n displayOnTerminal a caok ouepepL
nanne1cn o1noxennmH (on onncan a knacce Shape n nepeonpepenne1cn a kaxpoH
nopknacce). HpepnonoxnH, u1o o6pa6a1maaeHan qnrypa nanne1cn 1peyronLnnkoH Triangle.
K 1oHy apeHenn, korpa 6ype1 amsaan He1op knacca Triangle, o6a nc1ounnka nsHenunaoc1n
(anp qnrypm n 1nn neua1akuero yc1poc1aa) yxe npnansanm k onpepenennmH ka1eropnnH:
procedure Triangle.displayOnPrinter (Printer p)
begin
// xoz, cneuwwun-n znn npwniepa
// s-seciw ipeyronsnwx
// ...
end;
procedure Triangle.displayOnTerminal(Terminal t)
{
// xoz, cneuwwun-n znn iepmwnana
// s-seciw ipeyronsnwx
// ...
}
Ocnoanan cnoxnoc1L, cansannan c panno 1exnnko, coc1on1 a 6onLuoH konnuec1ae
1pe6yeHmx He1opoa. HanpnHep, kaxpan qnrypa ponxna nHe1L no He1opy na kaxpoe
neua1akuee yc1poc1ao. HecHo1pn na a1o1 nepoc1a1ok, pannan 1exnnka ouenL aqqek1nana
pnn 6opL6m c opnoapeHenno neonpepenennoc1Lk payx (n 6onee) aennunn [Ingalls 1986,
Budd 1991, LaLonde 1990a, Hebel 1990]
1
.

1
[aonan pncne1uepnsaunn anepame 6mna onncana [anoH HnrancoH [Ingalls
1986], ko1opm nasmaan ee Hnoxec1aennmH nonnHopqnsHoH. AnL1epna1nanm
1epHnn paonan pncne1uepnsaunn nonyunn 6onLuee pacnpoc1panenne, nockonLky
nosaonne1 ns6exa1L ny1annum c nonn1neH Hnoxec1aennoro nacnepoaannn.
18.2.4. Knaccuqukaun cxeH paspa6o1ok
Kak nnnkc1pnpyk1 npnaepennme amue npnHepm, cxeHm paspa6o1kn nonesnm 1eH, u1o onn
npepoc1aannk1 cnoaapnm sanac, neo6xopnHm npn o6cyxpennn aosHoxnmx peuenn
npo6neHm. Epnnan 1epHnnonornn nosaonne1 rpynnaH nporpaHHnc1oa o6Hennaa1Lcn npenHn
6es npnanskn k konkpe1noHy npnnoxennk n nepepaaa1L snannn n 1papnunn o1 opno
paspa6o1kn k ppyro.
B 1o apeHn kak konkpe1nme cxeHm onncmaak1cn poaonLno nerko, 6onee cnoxno
npo6neHo nanne1cn nx ka1eropnsaunn, ko1opan Horna 6m ncnonLsoaa1Lcn pnn sanncn n
aocnponsaepennn cxeH a nocnepykunx npoek1ax. BonLuan uac1L 1ekyue pa6o1m a a1o
o6nac1n cansana c cospanneH nopo6nmx 6as snann [Gamma 1995, Coplien 1995,
Pree 1995].
O6pa1n1e annHanne na 1o, u1o cxeHm pec1ayk1 na nnoH ypoane pe1annsaunn no
cpaanennk co cpepo paspa6o1kn. Cpepa copepxn1 a ce6e sakonuennyk c1pyk1ypy uenoro
npnnoxennn. Hanpo1na, cxeHa paspa6o1kn a1o npoc1o kon1yp peuennn ne6onLunx
cneunqnuecknx npo6neH. PeanLnme npnnoxennn uac1o coc1aanenm ns kycoukoa,
sanHc1aoaannmx ns neckonLknx pasnmx cxeH.
converted to PDF by BoJIoc
H nakoneu, cnepye1 nopuepkny1L, u1o xo1n cxeHm paspa6o1ok nonesnm npn am6ope
aapnan1a paspa6o1kn, onpepenenne nopxopnue cxeHm ne saHenne1 ampa6o1ky peuennn.
CxeHa npoc1o o6osnauae1 o6une kon1ypm. Hpopa6o1ka pe1ane Hoxe1 no1pe6oaa1L
snaun1enLnmx ycnnn.
Ynpaxueuun
1. Onnun1e ponL nacnepoaannn n o1noxennmx He1opoa a cpepe paspa6o1kn. BosHoxno
nn cospanne cpepm pnn nsmka nporpaHHnpoaannn, ne nannkuerocn o6Lek1no-
opnen1npoaannmH?
2. Onnun1e ppyroe npnnoxenne, ko1opoe Horno 6m 6m1L cospano c noHouLk
rpaqnuecko cpepm Hopennpoaannn ns noppaspena 18.1.2.
3. Moxe1e nn am npnaec1n ppyrne npnHepm cxeHm 1nna nocpepnnk?
4. O6Lncnn1e, noueHy nnkancynnunn c1anoan1cn 6onee nonesno a cxeHe
noce1n1enL, ecnn npoxop pepeaa ocyuec1anne1cn c1pyk1ypo pannmx (pepeaoH), a ne
knnen1oH?
5. O6Lncnn1e, kaknH o6pasoH a nsmke nporpaHHnpoaannn 6es c1pororo kon1ponn 1nna
pannmx (nanpnHep, Smalltalk) pnn amnonnennn apnqHe1nuecknx pec1an c onepanpaHn
cHeuannoro 1nna Hoxe1 6m1L ncnonLsoaana 1exnnka paono pncne1uepnsaunn. Kak, a
uac1noc1n, cnoxn1L paa uncna, kaxpoe ns ko1opmx Hoxe1 6m1L kak uenmH, 1ak n
aeuec1aennmH?
Fnaaa 19

Yve6um npuHep: cpepa paspa6o1ku
Kak Hm yxe o1Heuann a rnaae 18, cpepa paspa6o1kn a1o coaokynnoc1L knaccoa, ko1opme
onpepennk1 c1pyk1ypy, neo6xopnHyk pnn peuennn konkpe1no npo6neHm. BosHoxno,
nan6onee uac1o cpepm paspa6o1kn ncnonLsyk1cn npn cospannn rpaqnuecknx nn1epqecoa
nonLsoaa1enn. [ec1an1enLno, a 6onLue c1enenn nonynnpnoc1L o6Lek1no-
opnen1npoaannmx 1exnnk Hoxno o1nec1n sa cue1 ycnexa cpep 1nna GUI (Graphics User
Interface rpaqnueckn nn1epqec nonLsoaa1enn).
B a1o rnaae Hm paccHo1pnH ouenL npoc1yk cpepy paspa6o1kn Manyk Cpepy Paspa6o1kn
LAF (LAF Little Application Framework). Ona cosna1enLno cpenana ropaspo npoue, ueH
6onLunnc1ao koHHepuecknx cpep, nockonLky paccun1ana 1onLko na o6Lncnenne konuenunn
cpepm paspa6o1kn. LAF a uenoH copepxn1 Henee pkxnnm knaccoa, a 1o apeHn kak a
6onLunnc1ae koHHepuecknx cpep nx 6onee co1nn. HecHo1pn na a1o, cpepa LAF anonne
sakonuena n peHonc1pnpye1 6onLunnc1ao aaxnmx konuenun, neo6xopnHmx pnn nonnHannn
cpepc1a paspa6o1kn rpaqnuecknx npnnoxenn.
Cpepa LAF 6mna cospana c yue1oH 1pe6oaann nepenocnHoc1n na pasnnunme nna1qopHm. B
1o apeHn kak pe1ann peannsaunn Hory1 cnnLno pasnnua1Lcn, nn1epqec LAF pnn pasnnunmx
nna1qopH oc1ae1cn noc1onnnmH. Cyuec1ayk1 aepcnn cpepm LAF pnn Macintosh, PC n UNIX-
cnc1eH.
19.1. KoHnoueu1m GUI
Cpepa LAF noppepxnaae1 cnepykune koHnonen1m GUI: epnnc1aennoe okno, Henk, knonkn,
1ekc1oame nonn, pa6o1y c HmuLk n knaana1ypo.
converted to PDF by BoJIoc
Ocnoano cnoco6, nocpepc1aoH ko1oporo nporpaHHnc1 sapae1 konkpe1nm anp aneHen1a
ynpaanennn (knonkn, Henk, okna), a1o nacnepoaanne. Tak, cyuec1aye1 o6un knacc
knonok button, ns ko1oporo nopoxpak1cn nopknaccm, 1o ec1L knonkn konkpe1noro 1nna. To
xe caHoe cnpaaepnnao pnn Henk, nynk1oa Henk n okon.
[o 1oro kak o6Lncnn1L c1pyk1ypy a1nx koHnonen1oa a cpepe LAF, naH cnepye1 anauane
npno6pec1n o6uee nonnHanne nporpaHH, ynpaanneHmx co6m1nnHn, k ko1opmH o1nocn1cn n
nn1epqecm.
19.2. Bmnonueuue, ynpaanneHoe co6m1unHu
Hepaoe konuen1yanLnoe npenn1c1ane, ko1opoe ponxen npeopone1L nporpaHHnc1 a nonm1ke
nonn1L 1exnnky paspa6o1kn rpaqnueckoro nn1epqeca, a1o npnnunn noc1poennn
nporpaHHm na ocnoae co6m1n.
Tpapnunonnan nporpaHHa cnepye1 caoeHy co6c1aennoHy cuenapnk. Mm HoxeH npepc1aan1L
ce6e amnonnenne kak ykasky, panrakuykcn no nporpaHHe o1 nauana po konua. Ha pasnmx
a1anax nporpaHHa asanHopec1aye1 c nonLsoaa1eneH, opnako na6op o1ae1oa, c ko1opmHn
ona cnoco6na cnpaan1Lcn, aecLHa orpannuen. HanpnHep, nporpaHHa Hoxe1 sapaaa1L
nonLsoaa1enk aonpocm n pearnpoaa1L 1onLko na npoc1me o1ae1m 1nna pa/ne1, aaopnHme
c knaana1ypm.
HporpaHHa, ynpaanneHan co6m1nnHn, kon1ponnpye1cn nonLsoaa1eneH. Ona ponxna
pearnpoaa1L 1onLko na pec1ann nonLsoaa1enn, n ee ncnonnenne a ocnoanoH caopn1cn k
epnnc1aennoHy unkny. Cnepykun npnHep onncmaae1 c noHouLk nceapokopa rnaanm
6nok 1nnnuno nporpaHHm, ynpaanneHo co6m1nnHn. Hnnunannsaunn naunnae1, a unc1ka
naHn1n sakanunaae1 npnnoxenne. B npoHexy1ke Hexpy nnHn nporpaHHa npoc1o noa1opne1
unkn, oxnpan, noka nonLsoaa1enL cpenae1 cnepykun uar nanpnHep, nepepanne1 HmuL,
naxHe1 na knaanuy nnn ac1aan1 pncke1y a pnckoaop. B o1ae1 na ak1nanoc1L nonLsoaa1enn
cnepye1 peakunn nporpaHHm, a sa1eH ona sac1maae1 a oxnpannn cnepykuero pec1ann.
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
Hn1epecnme aeun, ko1opme penae1 nonLsoaa1enL, nasmaak1cn co6m1nnHn. HockonLky
no1ok ynpaanennn pnk1ye1cn nocnepoaa1enLnoc1Lk co6m1n, a1o nasmaae1cn amnonnenneH,
ynpaanneHmH co6m1nnHn. He ace co6m1nn ponxnm coo1ae1c1aoaa1L pec1annH
nonLsoaa1enn. HanpnHep, 1akne pec1ann, kak nonanenne pncke1m a pnckoaope, Hory1
amsaa1L nocnepoaa1enLnoc1L co6m1n, renepnpyeHmx nsny1pn nporpaHHm. HporpaHHa
o1pearnpye1 na nnx 1aknH xe o6pasoH, kak n na co6m1nn, amsaannme nonLsoaa1eneH.
O6Lek1no-opnen1npoaannoe nporpaHHnpoaanne ec1ec1aennmH o6pasoH coo1ae1c1aye1
pa6o1e, ynpaanneHo co6m1nnHn, no1oHy u1o no1ok ynpaanennn xopouo c1pyk1ypnpoaan.
O1nocn1enLno npoc1o cospa1L 6n6nno1eky knaccoa, ko1opme nHn1npyk1 peakunk na
1nnnunme co6m1nn, no ne amnonnnk1 nnkaknx peanLnmx pec1an. Hy1eH nopoxpennn
nopknaccoa ns knaccoa 1ako 6n6nno1ekn n nepeonpepenennn neko1opmx He1opoa Hm
po6aanneH pa6ouyk uac1L npnnoxennn 6es neo6xopnHoc1n nepenncmaa1L
ynpaannkune paspenm. D1o a 1ounoc1n 1o, u1o penae1cn cpepo paspa6o1kn GUI-
npnnoxenn.
C1na Bap6ek ns qnpHm Apple onncan 1nnnunyk 6n6nno1eky, ncnonLsyeHyk pnn noc1poennn
ynpaanneHoro co6m1nnHn npnnoxennn, kak 6n6nno1eky nansnanky [Wilson, 1990]. B
converted to PDF by BoJIoc
1papnunonno nporpaHHe 6n6nno1eka noc1aanne1 uac1n kopa, a sapaue nporpaHHnc1a
nanne1cn cknen1L nx a epnnoe uenoe. B cnc1eHe GUI npnnoxenne yxe ckneeno, a
nporpaHHnc1 npoc1o napenne1 konkpe1nmHn caoc1aaHn o1penLnme uac1n 6n6nno1ekn,
ncnonLsyn nopoxpenne nopknaccoa n nepeonpepenenne He1opoa, u1o6m nonyun1L
noaepenne, xapak1epnoe pnn pannoro npnnoxennn.
19.3. Hac1poka vepes uacnepoaauue
Ocnoanoe cpepc1ao, nocpepc1aoH ko1oporo cpepa paspa6o1kn nac1panaae1cn na cospanne
noao nporpaHHm, a1o nacnepoaanne. Cpepa npepoc1aanne1 opnn nnn neckonLko
a6c1pak1nmx napknaccoa, ko1opme 6ypy1 nepeonpepenenm nporpaHHnc1oH. 1o6m
o6Lncnn1L HexannsH nac1pokn, Hm paspennH He1opm, onpepenennme a a1nx knaccax, na
1pn ka1eropnn:
1. Basoame He1opm. Onn o6ecneunaak1 amnonnenne peanLnmx pec1an, ko1opme
nonesnm pnn apan1aunn nporpaHHm nop sapauy, no ko1opme c 6onLuo
aepon1noc1Lk ne 6ypy1 nepeonpepenn1Lcn nporpaHHnc1oH. HpnHepoH cnyxa1
He1opm, pncykune okpyxnoc1n, npnHme nnnnn n ppyrne rpaqnueckne o6Lek1m a
okne.
2. Anropn1Hnueckne He1opm. Onn onncmaak1 a6c1pak1nm anropn1H, a konkpe1nsaunn
oc1aanne1cn na ponk ppyrnx He1opoa. B kauec1ae npnHepa Hoxno npnaec1n He1op,
ko1opm peannsye1 onncannm amue unkn oxnpannn co6m1nn. Takne He1opm
o6ecneunaak1 c1pyk1ypy 1nnnunoro npnnoxennn 6es amnonnennn kako-nn6o
qak1nuecko pa6o1m. Kak n a cnyuae 6asoamx He1opoa, anropn1Hnueckne He1opm
o6muno ne nepeonpepennk1cn noamH npnnoxenneH.
3. A6c1pak1nme He1opm. Onn amnonnnk1 qak1nueckyk pa6o1y npnnoxennn. B
6n6nno1eke cpepm paspa6o1kn a6c1pak1nm He1op, kak npaanno, nanne1cn npoc1o
nyc1o npouepypo, ko1opan nnuero ne penae1. Hy1eH nepeonpepenennn
a6c1pak1nmx He1opoa o6ecneunaak1cn peanLnme pec1ann konkpe1noro
npnnoxennn.
Hpn noc1poennn noao nporpaHHm a cpepe paspa6o1kn nporpaHHnc1 npoc1o nopoxpae1
neckonLko nopknaccoa 1ex knaccoa, ko1opme noc1aannk1cn cpepo npnnoxennn,
nepeonpepennn npn a1oH a6c1pak1nme He1opm c uenLk sapannn neo6xopnHoro noaepennn,
cneunqnunoro pnn panno sapaun. Cpepa paspa6o1kn npepoc1aanne1 saro1oaky ocnoano
nporpaHHm (aepxnnn uac1L npnnoxennn) n sauac1yk o6ecneunaae1 onpepenennyk
6asoayk qynkunonanLnoc1L (nnxnnn uac1L), oc1aannn nporpaHHnc1y sanonnenne
pe1ane nocepepnne.
19.4. Knaccm a cpepe LAF
B cpepe LAF nHee1cn uec1L ocnoanmx knaccoa: application, button, menu, menuItem,
staticText n editText. Tpn knacca, na ocnoae ko1opmx o6muno nopoxpak1cn nopknaccm,
a1o application, button n menuItem. Onn noppo6no onncanm nnxe.
Mm cospaeH npnnoxenne ny1eH nopoxpennn nopknaccoa knacca application, nepeonpepennn
a nnx neko1opme He1opm. Ocnoano unkn oxnpannn co6m1n naunnae1cn c amsoaa He1opa
run, ynacnepoaannoro o1 knacca application. D1o1 He1op peannsye1 unkn oxnpannn,
onncannm amue. Bmnonnenne sakanunaae1cn, korpa nonLsoaa1enL am6npae1 aneHen1
Henk quit, ko1opm sakpmaae1 okno npnnoxennn, 1o ec1L amsmaae1 He1op quit(),
nacnepyeHm ns knacca application.
Hpoc1euee aosHoxnoe npnnoxenne nokasano nnxe. O6pa1n1e annHanne, u1o ero kop
coc1on1 ns:
onpepenennn knacca npnnoxennn;
converted to PDF by BoJIoc
cospannn akseHnnnpa pannoro knacca;
amsoaa He1opa run pnn akseHnnnpa knacca npnnoxennn:
class simpleApp : public application
{
public:
simpleApp() : application("simple application")
{ }
};
void main()
{
simpleApp theApp;
theApp.run();
};
B pannoH cnyuae npnnoxenne npoc1o o1o6paxae1 okno na akpane n xpe1, noka
nonLsoaa1enL ero sakpoe1.
19.5. Knacc application
Oynkunn, npepoc1aanneHme knaccoH application, nokasanm a 1a6n. 19.1. Oynkunn
run() o6muno nanne1cn nocnepne qynkune, amsmaaeHo a npouepype main. Oynkunn
run() ne nHee1 aosapauaeHoro pesynL1a1a. Me1op quit() cnyxn1 pnn npekpauennn pa6o1m
npnnoxennn.
Me1opm update() n clearAndUpdate() amsmaak1cn kaxpm pas, korpa pec1ann
nonLsoaa1enn Hory1 npnaec1n k o6noanennk akpana. aue acero a1o neo6xopnHo
acnepc1ane kakoro-nn6o co6m1nn, nanpnHep nepeHeuennn Hmun nnn naxa1nn
Ta6nua 19.1. Me1opm knacca application
converted to PDF by BoJIoc

knaanun na knaana1ype. Me1op clearAndUpdate() cnauana ounuae1 akpan nepep
o6noanenneH. 1o6m amnonnn1L o6noanenne, amsmaae1cn He1op paint(). O6muno
nonLsoaa1enL ne o6pauae1cn k He1opy paint() nenocpepc1aenno.
Me1op paint() nepepncoamaae1 nso6paxenne na akpane pncnnen. O6muno a knaccax
npnnoxennn on nepeonpepenne1cn, u1o6m o6ecneun1L 1pe6yeHoe noaepenne. On ne ponxen
nenocpepc1aenno amsmaa1Lcn nonLsoaa1eneH pannm He1op sanyckae1cn kak peakunn na
amsoa He1opoa update() nnn clearAndUpdate().
Ecnn naxa1a knonka Hmun, amsmaae1cn He1op mouseButtonDown(). [aa ero uenouncnennmx
apryHen1a npepc1aannk1 co6o kooppnna1m kypcopa o1nocn1enLno okna npnnoxennn na
HoHen1 naxa1nn knonkn. Hoaepenne no yHonuannk, cansannoe c a1nH He1opoH, ne
pena1L nnuero. 1o6m amnonnn1L kakoe-nn6o pec1ane, pannm He1op ponxen 6m1L
nepeonpepenen a nopknacce knacca application.
Co6m1ne, cansannoe c naxa1neH Hmun, o6muno ne amsmaae1 kaknx-nn6o pec1an,
nenocpepc1aenno annnkunx na nso6paxenne na akpane. Coo1ae1c1aykun He1op npoc1o
sanncmaae1 ack neo6xopnHyk nnqopHaunk n sa1eH amsmaae1 nn6o update(), nn6o
clearAndUpdate() pnn nepepncoakn akpana.
Korpa nonLsoaa1enL naxnHae1 knaanuy na knaana1ype, amsmaae1cn He1op keyPressed(). B
kauec1ae apryHen1a on nonyuae1 cnHaon, akanaanen1nm naxa1o knaanue.
converted to PDF by BoJIoc
Me1opm top(), bottom(), left() n right() aosapauak1 coo1ae1c1aykune kooppnna1m okna
npnnoxennn o1nocn1enLno akpana, ampaxennme a nnkcennx. O6pa1n1e annHanne na 1o, u1o
a1n aennunnm pak1cn a rno6anLno cnc1eHe kooppnna1, rpe 1ouka (0,0) a1o aepxnn
neam yron akpana. Hou1n ace npoune qynkunn, ko1opme onepnpyk1 c kooppnna1aHn,
ncnonLsyk1 snauennn a cnc1eHe kooppnna1 okna npnnoxennn, rpe 1ouka (0,0) a1o
aepxnn neam yron okna.
B knacce application poc1ynnm neckonLko aneHen1apnmx npouepyp pncoaannn n amaopa na
neua1L. O6muno amsoam rpaqnuecknx qynkun amnonnnk1cn ao apeHn o6noanennn akpana.
Ecnn ncnonLsye1cn He1op clearAndUpdate(), 1o nk6me pec1ann, cansannme c o1o6paxenneH
rpaqnkn, amnonnennme po amsoaa a1o npouepypm, 6ypy1 no1epnnm. TaknH o6pasoH,
amsoam rpaqnuecknx nopnporpaHH o6muno nponsaopn1cn (npnHo nnn kocaenno) ns He1opa
paint(). Kooppnna1m pnn pncoaannn pak1cn a 1o xe cnc1eHe o1cue1a, u1o n pnn naxa1nn
Hmun. Hnaue roaopn, aennunna (0,0) npepc1aanne1 co6o aepxnn neam yron okna,
snauennn no ocn OX yaennunaak1cn anpaao, a snauennn no ocn OY anns. Hocnepnee
uac1o npo1naopeun1 oxnpannnH naunnakunx nporpaHHnc1oa.
19.5.1. Knacc button
Pasnoanpnoc1n knonok cospak1cn ny1eH nopoxpennn nopknaccoa ns knacca button n
nepeonpepenennn anp1yanLnoro He1opa pressed. Kak 1onLko knonka npncoepnnena k okny,
a1o1 He1op amsmaae1cn aa1oHa1nueckn npn naxa1nn knonkn:
class quitButtonClass : public button
{
public:
quitButtonClass (window * win)
: button (win, "Quit", 5, 5, 20, 50)
{ }
protected:
pressed (window * win)
{
...
}
};
Okno npnnoxennn nepepae1cn a kauec1ae apryHen1a konc1pyk1opy knonkn. B npouecce
nnnunannsaunn knonke npnnncmaak1cn nekne kooppnna1m o1nocn1enLno okna, pa6m
rapan1npoaa1L, u1o knonka 6ype1 nso6paxa1Lcn npaannLno. aue acero knonka
onncmaae1cn kak uac1L coc1onnnn noaoro knacca npnnoxennn n nnnunannsnpye1cn a
konc1pyk1ope knacca npnnoxennn, kak a1o nokasano nnxe:
class newApplication : public application
{
public :
newApplication : application ("new program"),
quitButton(this)
{ }
...
private:
quitButtonClass quitButton;
};
O6muno npn naxa1nn na knonky peakunn saknkuae1cn a amsoae qynkunn-unena knacca
npnnoxennn. Mm HoxeH onpepenn1L epnnc1aennm knacc o6uero nasnauennn, ko1opm
noppepxnaae1 a1o pec1ane, n 1aknH o6pasoH ns6exnH neo6xopnHoc1n onpepenn1L noame
knaccm pnn kaxporo 1nna knonkn. HockonLky C++ nanne1cn nsmkoH co c1pornH kon1poneH
1nnoa pannmx, noam knacc ponxen ncnonLsoaa1L ua6non n napaHe1pnsnpoaa1Lcn c
noHouLk noaoro knacca npnnoxennn1. La6non amrnnpn1 cnepykunH o6pasoH:
template
converted to PDF by BoJIoc
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) ();
}
};
Hocnepnn ns apryHen1oa, f, cnyxn1 ykasa1eneH na qynkunk-unen n ponxen
coo1ae1c1aoaa1L kakoHy-nn6ypL He1opy knacca T. Mm ponxnm npnsna1L, u1o cnn1akcnc,
ncnonLsyeHm pnn onncannn ykasa1enn na qynkunk-unen, nanne1cn poaonLno
6ec1onkoamH. D1a qynkunn sanoHnnae1cn a nepeHenno fun n amsmaae1cn npn naxa1nn
knonkn. C npnHenenneH a1oro knacca npnnoxenne c payHn knonkaHn Hoxe1 6m1L cospano
cnepykunH o6pasoH:
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;
};
19.5.2. Knaccm menu u menuItem
Menk ao Hnornx o1nouennnx noxoxn na knonkn. B cpepe LAF cyuec1ayk1 paa knacca pnn
o6cnyxnaannn Henk: menu n menuItem. Hepam knacc npepc1aanne1 co6o ka1eropnk
nnneek Henk, a a1opo konkpe1nm aneHen1 a1o ka1eropnn. Korpa nonLsoaa1enL
ykasmaae1 na Henk, na akpane amcaeunaak1cn coo1ae1c1aykune aneHen1m Henk. Korpa
HmuLk am6npae1cn aneHen1 Henk, amnonnne1cn He1op selected pnn aneHen1a Henk.
Hyxnoe pec1ane aneHen1a Henk o6ecneunaae1cn sa cue1 cospannn nopknacca knacca
menuItem n nepeonpepenennn He1opa selected. D1o nponcxopn1 1ak xe, kak n a cnyuae c
knonkaHn.
Menk o6muno o6Lnannk1cn kak nonn pannmx a knacce npnnoxennn n nnnunannsnpyk1cn
konc1pyk1opoH, u1o nnnkc1pnpye1cn cnepykunH npnHepoH:
class clearMenuItem : public menuItem
{
public:
clearMenuItem (menu & m, char * t) : menuItem(m, t)
{ }
protected:
virtual void selected (window *);
converted to PDF by BoJIoc
};
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 () ;
}
Knacc tmenuItem ananornuen knaccy tbutton. Ecnn am noHnn1e, knacc tbutton yc1panne1
neo6xopnHoc1L cospannn noaoro knacca knonok, 1ak kak ace, u1o penae1 knonka a1o
amsmaae1 qynkunk-unen. AnanornunmH o6pasoH knacc tmenuItem nacnepye1 o1 knacca
menuItem n po6aanne1 noam apryHen1, ko1opm ponxen 6m1L qynkune-unenoH.
Oynkunn, nepepaaaeHan a kauec1ae po6aaounoro apryHen1a, ponxna amsmaa1Lcn npn
am6ope coo1ae1c1aykuero aneHen1a Henk.
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)();
}
};
Hpoune knaccm cpepm LAF nosaonnk1 nporpaHHnc1y pasHeua1L a okne 1ekc1oame nonn (a
1oH uncne pepak1npyeHme). D1n caoc1aa cpepm LAF specL ne onncanm: noppo6nyk
pokyHen1aunk no cpepe LAF, a 1akxe ncxopnme 1ekc1m Hoxno na1n no appecy
ftp://ftp.cs.orst.edu/pub/budd/laf.
19.6. PesmHe
Baxnoc1L cpepm LAF saknkuae1cn ne c1onLko a qynkunonanLnoc1n, ko1opyk ona
o6ecneunaae1 (xo1n ona nanne1cn npoc1o a ncnonLsoaannn cpepo pnn cospannn nporpaHH
c npoc1mH rpaqnuecknH nn1epqecoH), ckonLko a ee 1nnnunoc1n a kauec1ae cpepm
npnnoxennn. H1ak, cyHHnpyeH:
Cpepm paspa6o1kn npepnasnauenm pnn peuennn yskoro kpyra npo6neH. B cnyuae
cpepm LAF a1o cospanne npoc1mx opnookonnmx npnnoxenn c knonkaHn n Henk.
Cpepa o6ecneunaae1 o6uyk c1pyk1ypy nporpaHHm, opnako onyckae1 ace
konkpe1nme pe1ann. B cnyuae cpepm LAF onpepenne1cn unkn, ynpaanneHm
converted to PDF by BoJIoc
co6m1nnHn, opnako nporpaHHa ne snae1, kak konkpe1no ona 6ype1 pearnpoaa1L na
co6m1nn (nanpnHep, na uenuok HmuLk nnn naxa1ne knaanun na knaana1ype).
Cpepa paspa6o1kn 1akxe o6ecneunaae1 cpepc1aa nnskoro ypoann. HanpnHep, cpepa
LAF npepoc1aanne1 rpaqnueckne npnHn1nam o6uero nasnauennn, aosHoxnoc1L
pepak1npoaannn 1ekc1a n 1. p.
Hacnepoaanne a1o ocnoanan 1exnnka, ncnonLsyeHan nporpaHHnc1oH pnn
cneunannsaunn 1nnoaoro npnnoxennn. B cpepe LAF nporpaHHnc1 npn6erae1 k
nacnepoaannk pnn nepeonpepenennn He1opoa, onncmaakunx peakunk na uenuok
HmuLk, naxa1ne knaanun, am6op aneHen1a Henk n 1. p.
Ynpaxueuun
1. Hoc1po1e a cpepe LAF npoc1oe npnnoxenne c 1peHn 1nnaHn knonok.
2. Kakne 1pn rpynnm He1opoa Hoxno ampenn1L a 1nnnunoH npnnoxennn cpepm?
Hpnaepn1e npnHepm He1opoa kaxpo ka1eropnn pnn knacca application a cpepe LAF.
3. Hpocnepn1e nepepauy ynpaanennn pasnnunmH knaccaH, naunnan c naxa1nn
nonLsoaa1eneH koHanpno knonkn n sakanunaan HoHen1oH, korpa nporpaHHa
aosapauae1cn k oxnpannk cnepykuero co6m1nn. Hpepnonoxn1e, u1o nporpaHHnc1
ampae1 koHanpy update a konue nopnporpaHHm, cansanno c uenukoH HmuLk,
u1o6m amsaa1L o6noanenne akpana.
Fnaaa 20

Hoam asrnnp ua knaccm
Bmue Hm nonaranncL a ocnoanoH na nn1yn1nanoe nonnHanne 1oro, u1o snaun1 cnoao knacc.
K coxanennk, nn1ynunn ne y acex opnnakoaa. Heko1opme nkpn pyHak1 o knaccax kak o
ua6nonax, no ko1opmH u1aHnyk1cn ro1oame nspennn, nnn kak o c6opounoH konaeepe,
amnyckakueH kakne-1o npepHe1m. [pyrne nonarak1, u1o knacc a1o o6o6uenne sanncn,
1o ec1L c1pyk1ypa c nonnHn pannmx n nonnHn qynkun. Cyuec1ayk1 n ppyrne Hnennn. B
a1o rnaae Hm noroaopnH o knaccax a nsmkax nporpaHHnpoaannn.
1o xe a 1ounoc1n npepc1aanne1 co6o knacc? O1ae1 na a1o1 aonpoc saancn1, a uac1noc1n,
o1 1oro, kako nsmk nporpaHHnpoaannn am paccHa1pnaae1e. B unpokoH cHmcne ec1L pae
ocnoanme ukonm. Opnn nsmkn, 1akne kak C++ nnn Object Pascal, paccHa1pnaak1 knacc kak
1nn pannmx, nopo6nm uenmH uncnaH nnn sanncnH. [pyrne nsmkn, 1akne kak Smalltalk nnn
Objective-C, cun1ak1, u1o knacc a1o o6Lek1. B cnepykunx payx paspenax Hm paccHo1pnH
neko1opme aapnaunn a1nx payx ocnoanmx 1ouek spennn.
20.1. Knaccm kak 1unm
1o6m nonn1L cHmcn amckasmaannn knaccm cy1L 1nnm, Hm npexpe acero ponxnm
nonm1a1Lcn yncnn1L snauenne 1epHnna 1nn a nsmkax nporpaHHnpoaannn. K coxanennk,
nonn1ne 1nna pannmx ncnonLsye1cn pnn ouenL Hnornx uene. Cnepykun cnncok, asn1m
ns [Wegner 1986], nnnkc1pnpye1 neko1opme o1ae1m na aonpoc 1o 1akoe 1nn pannmx?:
Touka spennn npnknapnoro nporpaHHnc1a. Tnnm pannmx pas6naak1 snauennn na
knaccm akanaanen1noc1n, o6napakune o6unHn a1pn6y1aHn n onepaunnHn.
converted to PDF by BoJIoc
Daonkunonnc1ckn nopxop (o6Lek1no-opnen1npoaannan 1ouka spennn). Tnnm
pannmx nannk1cn cneunqnkaunnHn noaepennn, ko1opme Hory1 o6Lepnnn1Lcn n
Hopnqnunpoaa1Lcn pnn nonyuennn noaoro noaepennn. Hacnepoaanne ec1L HexannsH
Hopnqnkaunn noaepennn, npnaopnun k aaonkunn cnc1eHm.
Touka spennn cnn1akcnueckoro anannsa1opa. Tnn pannmx noppasyHeaae1 cnn1akcnc,
ko1opm Hoxe1 6m1L ncnonLsoaan a konkpe1noH ampaxennn. HanpnHep, nnpekcm
npnHennk1cn 1onLko a Haccnaax, 1ouka a sanncnx nnn o6Lek1ax, kpyrnme
cko6kn a qynkunnx.
Touka spennn kon1ponn 1nnoa. Tnnm pannmx nanarak1 ceHan1nueckne orpannuennn
na ampaxennn: onepa1opm n onepanpm coc1aanmx ampaxenn ponxnm 6m1L
coaHec1nHm. Cnc1eHa 1nnoa ec1L na6op npaann, cansmaakunx 1nn pannmx n nk6me
nHekune cHmcn nopampaxennn a nsmke nporpaHHnpoaannn.
Bepnqnkaunonnan 1ouka spennn. Tnnm pannmx onpepennk1 nnaapnan1m noaepennn,
ko1opmH ypoane1aopnk1 akseHnnnpm.
Cnc1eHnoe nporpaHHnpoaanne n o6ecneuenne 6esonacnoc1n. Tnnm pannmx ec1L
koHnnek1 pocnexoa, ko1opm npepoxpanne1 cmpyk nnqopHaunk (6n1oame c1pokn)
o1 neaepno nn1epnpe1aunn.
Touka spennn peannsaunn. Tnnm onpepennk1 cnoco6m pacnpepenennn naHn1n pnn
snauenn.
Hpnaepennm amue cnncok ne npe1enpye1 na nonno1y. Opnako c nero Hoxno naua1L
o6cyxpenne knaccoa kak 1nnoa pannmx.
Takne nsmkn nporpaHHnpoaannn, kak C++ n Object Pascal, paccHa1pnaak1 knaccm kak
o6o6uenne c1pyk1ypm nnn sanncn. Knacc 1akxe onpepenne1 nonn, n kaxpm akseHnnnp
knacca copepxn1 caon co6c1aennme snauennn none. B o1nnune o1 sanncn knacc nHee1 nonn
noaoro 1nna, ko1opme npepc1aannk1 co6o qynkunn nnn npouepypm (a o1nnune o1 none
pannmx, nHee1cn 1onLko opna konnn 1akoro nonn, coaHec1no ncnonLsyeHan aceHn
akseHnnnpaHn knacca).
Takan nn1epnpe1aunn knaccoa xopouo cornacye1cn c 6onLunnc1aoH nepeuncnennmx amue
nopxopoa. C 1oukn spennn npenHyuec1a pnn npnknapnoro nporpaHHnpoaannn n aaonkunn
cnc1eHm ace akseHnnnpm onpepenennoro 1nna nHek1 epnnoo6pasnme nonn, n onn no
kpane Hepe pearnpyk1 na opnn n 1o1 xe na6op koHanp. Takxe necoHnenno, u1o
o6Lek1no-opnen1npoaannm nopxop nyuue, ueH c1anpap1nme He1opm, nopxopn1 pnn
saun1m nec1pyk1ypnpoaannmx 6n1oa a naHn1n koHnLk1epa o1 npnHmx Hannnynnun co
c1oponm nporpaHHnc1a.
Opnako kak 1onLko Hm po6aanneH nacnepoaanne k naue konuenunn knacca, 1nn pannmx
npeapauae1cn a neu1o 6onee cnoxnoe. B neko1opoH cHmcne Hm HoxeH pyHa1L o
nacnepoaannn npoc1o kak o cpepc1ae, pacunpnkueH cyuec1aykun 1nn sanncn, no 1akan
nn1epnpe1aunn ne nanne1cn nonnoc1Lk apekaa1no no pnpy npnunn.
20.1.1. Kak uacnepoaauue ycnoxune1 noun1ue 1una
Hepeonpepenennm He1op ne npoc1o saHenne1 none a sanncn, no, ckopee, nsHenne1
noaepenne o6Lek1a, npnueH nponsaonLnmH o6pasoH. [aaa1e paccHo1pnH annnnne 1ako
saHenm na npouecc aepnqnkaunn
1
. HporpaHHnc1 paspa6a1maae1 na6op knaccoa,
onpepenne1 pnn kaxporo He1opa axopnme n amxopnme ycnoann n, npoaepnn nx amnonnenne,
pokasmaae1 npaannLnoc1L nporpaHHm. B1opo nporpaHHnc1 sa1eH cospae1 nopknaccm
ncxopnmx knaccoa, nepeonpepennn neko1opme He1opm. Ecnn naua 1ouka spennn na
nopknaccm coo1ae1c1aye1 ycnoannH aepnqnkaunn, 1o pnn akseHnnnpa nopknacca ycnoann
1akxe 6ypy1 amnonnenm. Torpa Hm saHennH a nporpaHHe akseHnnnpm knacca akseHnnnpaHn
converted to PDF by BoJIoc
nopknacca n HoxeH napen1Lcn, u1o nonyunauancn nporpaHHa oc1ane1cn npaannLno. Mm
npnunn k npnnunny nopc1anoakn, o6cyxpaaueHycn a rnaae 10.
B 1o apeHn kak o6munan nporpaHHnc1ckan npak1nka n sppaam cHmcn pnk1yk1, u1o
nepeonpepenennm He1op ne ponxen cnnukoH papnkanLno o1knonn1Lcn o1 popn1enLckoro,
a o6ueH cnyuae ne1 rapan1nn, u1o noaepenne nepeonpepenennoro He1opa 6ype1 kak-nn6o
cansano c noaepenneH ncxopnoro He1opa a napknacce. (Kak npaanno, pouepnn knacc n ne
6ype1 aec1n ce6n 1ouno 1ak xe, kak popn1enLckn nnaue ne 6mno 6m nyxpm a
nepeonpepenennn.) TaknH o6pasoH, y nac ne1, aoo6ue roaopn, yaepennoc1n, u1o neko1opme
epnnme axopnme n amxopnme ycnoann 6ypy1 cnpaaepnnam pnn o6onx He1opoa. Ecnn opno ns
a1nx ycnoan napyueno, ocnoaa naue aepm a npaannLnoc1L nporpaHHm okasmaae1cn
nopopaanno, n nporpaHHa, aepon1no, pac1 c6o. TnnnunmH nc1ounnkoH npo6ne

1
Bepnqnunpoaanne cneunanLnan 1exnnka paspa6o1kn nporpaHH, noppo6no
onncannan a [Gries 1981]. Hpn 1akoH nopxope a nporpaHHy po6aannk1cn
cneunanLnme ycnoann, oqopHnennme a anpe nceapokoHHen1apnea. Bmnonnenne
ycnoan cun1ae1cn neo6xopnHmH pnn npaannLno pa6o1m nporpaHHm. Onn
ncnonLsyk1cn (nano nnn nenano) nporpaHHnc1oH npn paspa6o1ke n o1napke
nporpaHHm n npnHennk1cn pnn Ha1eHa1nueckn c1pororo pokasa1enLc1aa
npaannLnoc1n nporpaHHm. Bepnqnkaunn (n a oco6ennoc1n nx uac1nm cnyua
nnaapnan1 unkna) aHec1e c 1exnnko qopHanLnoro pokasa1enLc1aa npaannLnoc1n
nporpaHH npepnoxenm T. XoapoH. HHek1cn cneunanLnme koHnnnn1opm, ko1opme a
pexnHe o1napkn npeapauak1 nceapokoHHen1apnn ao qparHen1m kopa,
npoaepnkune ao apeHn amnonnennn npaannLnoc1L sapannmx ycnoan. HpnHeu.
nepea.
Hm neopnosnaunoc1n (cH. rnaay 7) nanne1cn saHena nporpaHHnc1oH (aosHoxno,
nenpepnaHepenno) opnoro He1opa ppyrnH, pnn ko1oporo ne coxpanenm neko1opme aaxnme
acnek1m noaepennn.
HpuHep: nacunuc
Ecnn Hm xo1nH pokasa1L npaannLnoc1L nporpaHHm kap1ounoro nacLnnca, npepc1aanenno a
rnaae 8, 1o Hm ponxnm o6Lncnn1L pa6o1y nopnporpaHHm draw a knacce TablePile, ko1opan
o1o6paxae1 c1onky kap1 na kap1ounoH c1one. 1o6m cospa1L nso6paxenne, nopnporpaHHa
npoc1o npoxopn1 a unkne no c1onke kap1 cnnsy aaepx, ounuan noaepxnoc1L nop kaxpo
kap1o n nepepncoamaan nso6paxenne. TaknH o6pasoH, nnxenexaune kap1m
npopncoamaak1cn, a sa1eH uac1nuno c1npak1cn no Hepe 1oro, kak pncye1cn c1onka kap1.
HpepnonoxnH, u1o pannoe npnnoxenne cospano n Hm o6ocnoaann qopHanLno nnn
neqopHanLno ero koppek1noc1L. TenepL noam nporpaHHnc1 peuae1, u1o cnepye1
ynyuun1L aqqek1nanoc1L nopnporpaHH pncoaannn, ncnonLsyn napannennsH. HockonLky
rpaqnueckne onepaunn nannk1cn o1nocn1enLno pnn1enLnmHn, 1o, korpa o6Lek1y-kap1e
1pe6ye1cn o1o6pasn1L ce6n, on nonpoc1y sanyckae1 qonoam npouecc, ocyuec1annkun
onepaunk pncoaannn, n npoponxae1 pa6o1y a napannenLnoH pexnHe. Me1opnkn, nopo6nme
converted to PDF by BoJIoc

Puc. 20.1. HpaannLnoe n nckaxennoe o1o6paxenne c1onkn kap1
onncannmH naHn a rnaae 7, penak1 1akyk saHeny nerko pnn nporpaHHnpoaannn. Bce, u1o
1pe6ye1cn a1o cospa1L noam knacc (nanpnHep, ParallelCard), ko1opm nacnepye1 ace ns
knacca Card n nepeonpepenne1 1onLko He1op draw. Onncanne 1akoro knacca nokasano nnxe.
3a1eH nporpaHHnc1 Hoxe1 cHenn1L ccmnkn na knacc Card a nnnunannsnpykue uac1n
nporpaHHm, u1o6m ncnonLsoaa1L aHec1o nero knacc ParallelCard.
class ParallelCard : public Card
{
// nacnezyei sce nonnocisx, no wsmennei pwcosanwe
public:
void draw()
{ if (!fork()) // nopo+zaem npouecc
{ Card::draw(); exit(0); }
}
};
K coxanennk, a pesynL1a1e 1akoro nsHenennn nponapae1 yaepennoc1L a 1oH, u1o k 1oHy
apeHenn, korpa pncye1cn kap1a, ace kap1m a c1onke nop ne yxe npopncoaanm. TaknH
o6pasoH, ne ncknkueno (a na caHoH pene anonne aepon1no), u1o 6ype1 na6nkpa1Lcn
cnyuanm nopnpok pncoaannn, ko1opm npnaepe1 k nckaxennk nso6paxennn,
nokasannoHy na pnc. 20.1.
Cyuec1aennmH HoHen1oH specL nanne1cn 1o, u1o nn1epqec He1opa n pesynL1a1 ero pa6o1m
ne nsHennnncL (kak ncxopnm, 1ak n nepeonpepenennm He1opm no-npexneHy pncyk1
kap1y). Opnako 6mno nsHeneno noaepenne He1opa draw. Haue pokasa1enLc1ao
npaannLnoc1n nporpaHHm, ocnoamaaaueecn na npepnonoxennn, u1o nsHenenn ne 6ype1,
6onLue ne npoxopn1.
[annm npnHep nnnkc1pnpye1 npo6neHm, aosnnkakune c nonanenneH nacnepoaannn.
Hcxopnan nporpaHHa 6mna paspa6o1ana n 1ua1enLno npo1ec1npoaana, n, aosHoxno, nekn
nporpaHHnc1 noppac1cn co6nasny pyHa1L, u1o po 1ex nop, noka ampepxnaae1cn kpn1epn
6m1L akseHnnnpoH, panee npoaepennoe 1ec1npoaanne 6ype1 oc1aaa1Lcn cnpaaepnnamH. K
coxanennk, pannoe npepnonoxenne a o6ueH cnyuae necnpaaepnnao, n nk6oe nsHenenne
ponxno nopaepra1Lcn perpeccnonnoHy 1ec1npoaannk [Perry 1990].
converted to PDF by BoJIoc
Hcnonusoaauue kou1ponupyeHmx ycnoau
O6Lek1no-opnen1npoaannm nsmk nporpaHHnpoaannn Eiffel [Meyer 1988a, Rist 1995] no
kpane Hepe uac1nuno peuae1 a1y npo6neHy. K He1opaH npncoepnnnk1cn 1ak nasmaaeHme
ycnoann. Onn nacnepyk1cn n ne Hory1 nepeonpepenn1Lcn a nopknaccax (xo1n Hory1
pononnn1Lcn). KoHnnnn1op renepnpye1 kop, npoaepnkun amnonnenne a1nx ycnoan ao
apeHn amnonnennn nporpaHHm. TeH caHmH HnnnHanLnm ypoaenL qynkunonanLnoc1n
He1opa rapan1npye1cn nesaancnHo o1 nk6mx aosHoxnmx nepeonpepenenn. Koneuno,
nnorpa sa1pypnn1enLno cqopHynnpoaa1L neko1opme ycnoann 1akne, kak y1aepxpenne o
1oH, u1o nrpanLnan kap1a nonnoc1Lk napncoaana nepep amxopoH ns He1opa.
3aHe1nH, u1o Java npepnarae1 nn1epecnm aapnan1 peuennn a1o npo6neHm:
ncnonLsoaanne Hopnqnka1opa final. Knacc, ko1opm o6Lnanen kak final, ne Hoxe1
nopoxpa1L nopknaccm, n 1eH caHmH rapan1npye1cn coxpanenne ero qynkunonanLnoc1n.
20.1.2. Hacnepoaauue u naHn1u
Hakoneu paccHo1pnH cansL Hexpy 1nnaHn pannmx n ynpaanenneH naHn1Lk. 3pecL 1oxe
nacnepoaanne npnaopn1 k 1onknH nkancaH, ko1opme o1cy1c1ayk1 pnn 1papnunonnmx 1nnoa
pannmx (1aknx, kak sanncn n Haccnam). HockonLky c 1oukn spennn knaccm cy1L sanncn
akseHnnnp nopknacca neko1oporo knacca nanne1cn pacunpenneH akseHnnnpa ncxopnoro
knacca, 1o on Hoxe1, koneuno xe, sannHa1L 6onLue Hec1a a naHn1n. Opnako, kak Hm
anpenn a rnaae 12, a1o nsHenenne a pasHepe ycnoxnne1 kaxyuncn 1pnananLnmH npepHe1
o6cyxpennn npncaanaanne. BcnoHnnH, u1o npn npncaanaannn nepeHenno
popn1enLckoro knacca snauennn 1nna nopknacca 1pe6ye1cn, u1o6m:
nnn npn npncaanaannn o6pesanncL nonn nopknacca, ko1opme ne noHeuak1cn a
o6nac1n naHn1n appeca1a;
nnn naHn1L pnn nepeHenno ponxna ampenn1Lcn ns kyun, no ne ns c1eka.
CyHHnpyn, saknkuaeH, u1o nonn1ne knacca nou1n 1ouno coo1ae1c1aye1 6onLunnc1ay naunx
nn1yn1nanmx npepc1aanenn o 1nnax, no coanapenne ne nanne1cn nonnmH. B1opo asrnnp
na aeun knaccm cy1L o6Lek1m yc1panne1 neko1opme npo6neHm, aoo6ue o6xopn
c1opono pnckyccnk o 1nnax pannmx.
20.2. Knaccm kak o61ek1m
Mm nopuepknaann amue, u1o ocnoanan qnnocoqnn o6Lek1no-opnen1npoaannoro
nporpaHHnpoaannn penernpoaanne nonnoHoun nnpnanpyanLnoHy o6Lek1y. On
o1ae1c1aenen sa caoe any1pennee coc1onnne n nsHenne1 ero a coo1ae1c1ann c neckonLknHn
qnkcnpoaannmHn npaannaHn noaepennn. C ppyro c1oponm, kaxpoe pec1ane ponxno 6m1L
o6nsannoc1Lk neko1oporo o6Lek1a, nnn xe
converted to PDF by BoJIoc

Puc. 20.2. CRC-kap1ouka pnn knacca Class

Puc. 20.3. CansL akseHnnnpoa n cansL nopknaccoa
ono ne 6ype1 amnonneno. HecoHnenno, cospanne noamx akseHnnnpoa knacca ec1L nekoe
pec1ane. Bonpoc a 1oH, k1o (1o ec1L kako o6Lek1) ponxen sa a1o o1aeua1L?
20.2.1. 4a6puku no cospauum o61ek1oa
[onyc1nH, y nac ec1L uen1pannsoaannm o6Lek1 pnn cospannn o6Lek1oa. 3anpoc na
pnnaHnueckoe cospanne noaoro o6Lek1a npeo6pasoamaae1cn a o6pauenne k a1oHy o6Lek1y.
B kauec1ae apryHen1oa nepepae1cn pasHep cospaaaeHoro o6Lek1a n cnncok ero He1opoa.
Xo1n 1ako HexannsH pa6o1ocnoco6en, on neckonLko neypauen: any1pennkk nnqopHaunk o
knacce (ero pasHep n He1opm) ponxen noHnn1L nekn aneunn o6Lek1.
converted to PDF by BoJIoc
Bonee ypaunoe peuenne nnkancynnpye1 a1y nnqopHaunk a caHoH knacce, noHeuan Hexpy
nonLsoaa1eneH, ko1opm xoue1 cospa1L noam o6Lek1, n kopoH, ko1opm ocyuec1anne1
pacnpepenenne naHn1n, ynpaannkuyk npocnoky. Ona snae1 pasHepm o6Lek1oa n nx
He1opm, 1eH caHmH a1o ne 1pe6ye1cn noHnn1L nn nonLsoaa1enk, nn kopy pacnpepenennn
naHn1n.
[ec1ayn 1aknH o6pasoH, npnxopnH k cxeHe, a ko1opo Hm nHeeH opnn noam o6Lek1 pnn
kaxporo knacca cnc1eHm. Ocnoanan qynkunn pannoro o6Lek1a coc1on1 a cospannn noamx
akseHnnnpoa knacca. 1o6m cpena1L a1o, on ponxen noppepxnaa1L nnqopHaunk o pasHepe
knacca n ero He1opax. B npak1nueckoH o1nouennn a1o1 o6Lek1 n ec1L knacc.
20.2.2. Knacc Class
Kaxpm o6Lek1 ponxen 6m1L akseHnnnpoH neko1oporo knacca, n onncannm amue o6Lek1
ne nanne1cn ncknkuenneH. DkseHnnnpoH kakoro xe knacca on nanne1cn? O1ae1 pnn nsmkoa
Smalltalk, Objective-C, Java n ananornunmx nH coc1on1 a 1oH, u1o on nanne1cn akseHnnnpoH
knacca, nasmaaeHoro Class. Ha pnc. 20.2 nokasana CRC-kap1ouka (o6e c1oponm) knacca
Class a cnc1eHe Little SmallTalk. Ho cornauennk, snauenne a1oro o6Lek1a copepxn1cn a
nepeHenno, ko1opan nHee1 1o xe caHoe nHn, u1o n caH knacc. To ec1L nepeHennan Set
copepxn1 o6Lek1 co c1pyk1ypo, ananornuno nso6paxenno na pnc. 20.3. On o1aeuae1 sa
cospanne noamx akseHnnnpoa knacca.
Cospanne o6Lek1a nnnunnpye1cn coo6uenneH new, ko1opoe onpepeneno kak He1op knacca
Class. Kaxpm cospaaaeHm o6Lek1 nanne1cn akseHnnnpoH neko1oporo knacca n copepxn1
ykasa1enL na o6Lek1, npepc1aannkun a1o1 knacc. Bo apeHn nepecmnkn coo6uennn
ynoHnny1m ykasa1enL ncnonLsye1cn pnn 1oro, u1o6m na1n He1op, coo1ae1c1aykun
cenek1opy o6pa6a1maaeHoro coo6uennn.
1o6m nonn1L ace a1o, am ponxnm pasnnua1L asanHocansL nopknaccoa n asanHocansL
akseHnnnpoa. Knacc Class nanne1cn nopknaccoH knacca Object 1eH caHmH o6Lek1 Class
ykasmaae1 na o6Lek1 Object kak na cao napknacc. C ppyro c1oponm, o6Lek1 Object
nanne1cn akseHnnnpoH knacca Class 1eH caHmH Object ykasmaae1 o6pa1no na Class. Knacc
Class nanne1cn knaccoH caH no ce6e n 1eH caHmH nanne1cn akseHnnnpoH caHoro ce6n. Ecnn
Hm nccnepyeH 1nnnunm knacc, ckaxeH Set, 1o o6Lek1 Set 6ype1 nann1Lcn akseHnnnpoH
knacca Class, no, kpoHe 1oro, on 6ype1 (nenano) n nopknaccoH knacca Object. Konkpe1noe
Hnoxec1ao set nanne1cn akseHnnnpoH knacca Set. D1n asanHoo1nouennn
nponnnkc1pnpoaanm na pnc. 20.3, na ko1opoH cnnounme nnnnn npepc1aannk1 asanHocansL
akseHnnnpoa, a nynk1npnme nnnnn o6osnauak1 cansL nopknaccoa.
20.2.3. Me1aknaccm u knacc-He1opm
Bmue Hm o1Heuann, u1o nnnunannsaunn nanne1cn aaxno uac1Lk npouecca cospannn
o6Lek1a. HockonLky o6Lek1m o1aeuak1 sa noppepxanne co6c1aennoro coc1onnnn, 6mno 6m
nonesno, ecnn 6m o6Lek1, saaepykun cospanneH noamx akseHnnnpoa knacca, Hor 6m
o6ecneun1L 1akxe napnexauyk nnnunannsaunk o6Lek1oa. 3nauenne rnarona
nnnunannsnpoaa1L, opnako, pasnnuae1cn o1 knacca k knaccy. Ecnn ace o6Lek1m knacca
nannk1cn akseHnnnpaHn opnoro n 1oro xe knacca, Hm ne ponxnm oxnpa1L oco6ennoc1e a
nx noaepennn: ace onn o6nsanm amnonnn1L opnn n 1o1 xe na6op He1opoa n 1eH caHmH
aec1n ce6n opnnakoao.
Cansannan c a1nH npo6neHa coc1on1 a 1oH, u1o cospanne n nnnunannsaunn uac1o 1pe6yk1
6onLue nnqopHaunn, ueH nokasano na pnc. 20.2. HanpnHep, u1o6m cospa1L Haccna, naH
nyxno sna1L uncno nosnun, ko1opme napo sapesepanpoaa1L pnn nero. [pyrne knaccm Hory1
1pe6oaa1L eue 6onee noppo6no nnqopHaunn.
O6e a1n npo6neHm 1pe6yk1, u1o6m Hm cpenann cneunqnunmH noaepenne o6Lek1a knacca,
noa1oHy ne ypnan1enLno, u1o opnn n 1o1 xe HexannsH peuae1 o6e npo6neHm. Mm acerpa
nac1anaann na 1oH, u1o6m noaepenne 6mno cansano c knaccoH, a ne c nnpnanpyanLnmHn
o6Lek1aHn (ace akseHnnnpm knacca 6ypy1 coaHec1no ncnonLsoaa1L opnn n 1e xe qynkunn).
converted to PDF by BoJIoc
Ecnn Hm xo1nH, u1o6m akseHnnnpm knacca nHenn caoe co6c1aennoe noaepenne,
epnnc1aennoe peuenne cpena1L nx akseHnnnpaHn caonx co6c1aennmx knaccoa.
Me1aknacc nanne1cn knaccoH knaccoa. B Smalltalk-80 He1aknacc nenano n aa1oHa1nueckn
konc1pynpye1cn pnn nk6oro knacca, onpepenennoro nonLsoaa1eneH. Kaxpm He1aknacc
nHee1 1onLko opnn akseHnnnp, ko1opm nanne1cn co6c1aenno knaccoH. Me1aknaccm
oprannsoaanm a nepapxnk knaccnopknacc, ko1opan o1paxae1 ananornunyk nepapxnk
pnn ncxopnoro knacca. D1a nepapxnn copepxn1 He1aknaccm n nHee1 kopenL a knacce Class,
a ne a knacce Object.
Hnxe nokasana uac1L nepapxnn knaccoa a Smalltalk-80:
Object nazxnacc scex o+exios
Collection acipaxin-n nazxnacc scex cosoxynnocien
Bag xnacc mynsiwmno+ecis
Coo1ae1c1aykuan nepapxnn He1aknaccoa amrnnpn1 1ak:
Object nazxnacc scex o+exios
Class nosezenwe, omee znn scex xnaccos
Metaclass-Object wnwuwanwsauwn scex o+exios
Metaclass-Collection wnwuwanwsauwn xonnexuwn collections
Metaclass-Bag wnwuwanwsauwn mynsiwmno+ecis bags
Kop, cneunqnueckn pnn o1penLnoro knacca, cansmaae1cn c He1aknaccoH a1oro knacca.
HanpnHep, akseHnnnpm knacca Bag copepxa1 cnoaapL, ko1opm ncnonLsye1cn pnn xpanennn
qak1nuecknx snauenn. Me1aknacc pnn knacca Bag nepeonpepenne1
Ruc1uur 20.1. Me1op newDay:year: knacca Date

newDay: dayCount year: referenceYear
" Bosspamaei zaiy, oicionmyx na dayCount znen "
" oi nauana roza referenceYear "
S day year daysInYear S
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

He1op new, u1o6m nponsaopn1L nnnunannsaunk cnoaapn npn cospannn noamx akseHnnnpoa.
D1o pec1ane ocyuec1anne1cn He1opoH new, onpepenennoH a knacce Metaclass-Bag n
nepeonpepennkueH He1op knacca Class. CaH He1op npnaepen nnxe:
new
" coszais w wnwuwanwswposais nos-n oxsemnnnp "
super new setdictionary
HockonLky knacc Bag nanne1cn akseHnnnpoH knacca Metaclass-Bag,



converted to PDF by BoJIoc
Ruc1uur 20.2. Me1op daysInYear: knacca Date
daysInYear: yearInteger
" sosspamaei uwcno znen s rozy, yearInteger "
365 + (self leapYear: yearInteger)
leapYear: yearInteger
" 1, ecnw roz yearInteger swcoxocn-n, "
" wnaue 0 "
( yearInteger \\ 4 ~= 0 or:
[ yearInteger \\ 100 = 0 and:
[ yearInteger \\ 400 ~= 0 ]])
ifTrue: [ 0 ]
ifFalse: [ 1 ]

1o ykasannm He1op 6ype1 amsmaa1Lcn a o1ae1 na coo6uenne new. Hpexpe acero, He1op
nepecmnae1 coo6uenne napknaccy, ko1opm ocyuec1anne1 pec1ann, ananornunme
o6nsannoc1nH, nokasannmH na CRC-kap1e, npnaepenno panee. TaknH cnoco6oH napknacc
cospae1 noam o6Lek1. Kak 1onLko noam o6Lek1 aosapauen, eHy nocmnae1cn coo6uenne
setDictionary. Coo1ae1c1aykun He1op, nokasannm nnxe, yc1anaannaae1 snauennn none
akseHnnnpa pnn anoaL cospannoro cnoaapn:
setDictionary
" ycianoswis nos-n cnosaps "
contents <- Dictionary new
He am opnn cun1ae1e a1n paccyxpennn sany1annmHn. Konuenunn He1aknaccoa nHee1
peny1aunk opnoro ns nan6onee 1pypnopoc1ynnmx acnek1oa a Smalltalk-80. HecHo1pn na
a1o, ona nonesna 1eH, u1o nosaonne1 naH npnpa1L konkpe1nme caoc1aa qynkunn
nnnunannsaunn pnn nnpnanpyanLnmx knaccoa, ne amxopn sa paHkn unc1oro o6Lek1no-
opnen1npoaannoro nopxopa. Opnako, yun1maan sany1annyk npnpopy He1aknaccoa,
6onLunnc1ao nporpaHHnc1oa ouenL 6naropapnm cnc1eHe, ko1opan nosaonne1 nx ne
saHeua1L. HanpnHep, a Smalltalk-80 knacc-He1opm onpepennk1cn npn npocHo1pe 6poysepoH
6asoaoro knacca, a ne He1aknacca. enuok knacca nnn akseHnnnpa ao a1opoH okouke
nokasmaae1, onncmaae1cn nn He1aknacc nnn co6c1aenno knacc. Ananornuno a nsmke
Objective-C He1opm, nepep ko1opmHn a nepao kononke c1on1 snak nnkc (1ak
nasmaaeHme He1opm-qa6pnkn), cansanm c He1aknaccaHn, a 1o apeHn kak He1opm,
ko1opme naunnak1cn c Hnnyca, nannk1cn knacc-He1opaHn.
[anLneuan nnqopHaunn o He1aknaccax n He1anporpaHHnpoaannn Hoxe1 6m1L napena a
pa6o1e [Kiczales 1991].
20.2.4. Huuuanusaun o61ek1oa
Mm npopeHonc1pnpoaann, kak He1opm knaccoa, onpepenneHme nocpepc1aoH He1aknaccoa,
ncnonLsyk1cn pnn peuennn opno ns npo6neH, ouepuennmx a nauane a1oro paspena
npo6neHm cneunannsnpoaanno nnnunannsaunn. BepneHcn 1enepL ko a1opo npo6neHe,
nnnunannsaunn o6Lek1oa, pnn ko1opmx 1pe6ye1cn 6onLue nnqopHaunn, ueH npoc1o nx
pasHep. Mm nokaxeH (napnpy c ppyrnHn aonpocaHn), kak He1opm knacca Hory1 6m1L
ncnonLsoaanm pnn peuennn a1o npo6neHm.
B kauec1ae npnHepa ncnonLsyeH knacc Date pnn nsmka Smalltalk-80. DkseHnnnpm knacca
npepc1aannk1 co6o pa1y onpepenennoro ropa. Kaxpm akseHnnnp knacca Date xpann1 paa
snauennn: noHep ropa n uncno a pnanasone o1 1 po 366, 1o ec1L penL. Hoam akseHnnnp
knacca Date Hoxe1 6m1L cospan pasnnunmHn cnoco6aHn. HanpnHep, coo6uenne Date today
nopoxpae1 akseHnnnp knacca Date, npepc1aannkun 1ekyuyk pa1y. [a1m Hory1 6m1L 1akxe
a nanoH anpe onpepenenm nonLsoaa1eneH ny1eH sapannn ropa n pnn. B a1oH cnyuae
amsmaae1cn kop, nokasannm a nnc1nnre 20.1. On ocyuec1anne1 ne6onLuyk npoaepky,
rapan1npyn, u1o noHep pnn nonoxn1enen n ne npeaocxopn1 uncna pne a ropy. Korpa ec1L
converted to PDF by BoJIoc
yaepennoc1L, u1o snauennn ponyc1nHm, kop ncnonLsye1 He1op new pnn cospannn noaoro
o6Lek1a n nnnunannsaunn ero sapannmHn snauennnHn.
Coo6uenne daysInYear:, amsmaaeHoe a He1ope, nokasannoH a nnc1nnre 20.1, nnnkc1pnpye1
ppyroe ncnonLsoaanne He1opoa knacca: specL o6ecneunaae1cn noaepenne, cansannoe c
knaccoH a uenoH, a ne c kaknH-nn6o konkpe1nmH akseHnnnpoH. O6Lek1 Date na sanpoc o
konnuec1ae pne a onpepenennoH ropy aosapauae1 1pe6yeHoe uenoe uncno 6es
qak1nueckoro noc1poennn noaoro akseHnnnpa knacca. On penae1 a1o, ncnonLsyn 1o1 xe
caHm HexannsH onpepenennn He1opa knacca sa ncknkuenneH 1oro, u1o a pannoH cnyuae
He1op knacca aosapauae1 uenoe uncno, a ne noaoe snauenne. Me1opm daysInYear n leapYear
(ancokocnm-rop), ko1opme amsmaak1cn npn a1oH, nokasanm a nnc1nnre 20.2.
20.2.5. Hopc1auoaku a Objective-C
Hn1epecno nnnkc1paune 1oukn spennn knaccm cy1L o6Lek1m nanne1cn nonn1ne
nopc1anoakn knacca a nsmke Objective-C. Mm uac1o anpenn cn1yaunn, korpa nporpaHHnc1
xenae1 nopc1aan1L aHec1o opnoro knacca ppyro a yxe cyuec1aykueH npnnoxennn.
O6muno noam knacc nacnepye1 ace ns ncxopnoro knacca n Hopnqnunpye1 1onLko
ne6onLuyk uac1L noaepennn. HpnHepaHn nannk1cn knaccm GraphicalReactor ns rnaam 12 n
ParallelCard, onncannm amue a a1o rnaae. B o6onx cnyuanx 6mno 6m xena1enLno
nsHenn1L ace coo6uennn o cospannn o6Lek1oa, u1o6m ncnonLsoaa1L noam knacc aHec1o
ncxopnoro.
Hsmk Objective-C o6ecneunaae1 ynnkanLnyk anL1epna1nay pnn a1nx pec1an, ko1opan
snaun1enLno HenLue sa1parnaae1 ncxopnm kop. k6oHy knaccy Hoxno pa1L nnc1pykunk
nopc1aan1L ce6n aHec1o ppyroro knacca. B pesynL1a1e nopc1aanneHm o6Lek1 sannHae1
Hec1o o6Lek1a ncxopnoro knacca. HanpnHep, nonLsoaa1enL Hoxe1 nannca1L cnepykun
onepa1op:
[ GraphicalReactor poseAs: [ Reactor class ]];
Bce nocnepykune ccmnkn na knacc Reactor, aknkuan coo6uennn o cospannn akseHnnnpoa
knacca, 6ypy1 o1cmna1Lcn k knaccy GraphicalReactor. Han6onee uac1o o6Lek1,
nopc1aanneHm aHec1o ppyroro, a1o nopknacc saHeuaeHoro knacca. TeH caHmH
6onLunnc1ao coo6uenn 6ype1 nepeappecoaano o6pa1no ncxopnoHy
Ruc1uur 20.3. Onncanne knacca co c1a1nuecknHn nonnHn
//
// --------- xnacc Card
//
class Card
{
public:
// xoncipyxiop
Card(int s, int c);
// xonciani-
static const int CardWidth;
static const int CardHeight;
...
};
const int Card::CardWidth = 68;
const int Card::CardHeight = 75;

knaccy (nannkueHycn 1enepL napknaccoH).

converted to PDF by BoJIoc
20.3. Qauume knacca
HesaancnHo o1 1oro, kakoro asrnnpa na knaccm Hm npnpepxnaaeHcn, uac1o xena1enLno
nHe1L o6nac1L pannmx, ko1opan nanne1cn o6ue pnn acex akseHnnnpoa knacca. HanpnHep,
ace okna Windows Hoxno pasHec1n1L a opnoH cansnoH cnncke nnn ace kap1m Cards a
epnno konope.
C1anpap1noe peuenne coc1on1 a ncnonLsoaannn rno6anLno nepeHenno. Ona necoHnenno
poc1ynna n nanne1cn o6ue pnn acex akseHnnnpoa knacca, nockonLky ona nanne1cn 1akoao
pnn acex o6Lek1oa. Opnako 1akan unpokan poc1ynnoc1L ockop6nne1 o6Lek1nyk
qnnocoqnk, ko1opan coc1on1 a orpannuennn poc1yna n nepepaun o1ae1c1aennoc1n sa
noaepenne o1penLnmH nnpnanpyyHaH. TaknH o6pasoH, o6Lek1no-opnen1npoaannan 1ouka
spennn 1pe6ye1 na1n ppyryk anL1epna1nay. Ona coc1on1 a 1oH, u1o6m cospa1L snauennn,
poc1ynnme pnn akseHnnnpoa knacca, no nepoc1ynnme pnn o6Lek1oa ppyrnx 1nnoa. Takne
snauennn nasmaak1cn nepeHennmHn knacca.
Tonkoc1L coc1on1 a nnnunannsaunn nepeHennmx knacca. B onpepenennoH cHmcne ace
akseHnnnpm knacca paanm: onn opnnakoao qynkunonnpyk1, n nHee1cn 1onLko opna konnn
nk6o nepeHenno knacca. Opnako neo6xopnHo:
nnnunannsnpoaa1L pannme knacca;
cpena1L a1o ne 6onee opnoro pasa.
Kak a nsmke Smalltalk, 1ak n a Objective-C cnc1eHa rapan1npye1, u1o coo6uenne initialize
nepecmnae1cn npexpe nk6oro ppyroro. O1knnk na He1op initialize Hoxe1 6m1L ncnonLsoaan
sa1eH pnn yc1anoanennn snauennn nk6o nepeHenno knacca. 3a6o1a o coo6uennn initialize
o6ecneunaae1cn nenanmH o6pasoH, n specL ne1 neo6xopnHoc1n aoo6ue a nanoH anpe
amsmaa1L He1op initialize. Hsmk Java nHee1 ananornunoe cpepc1ao, 1onLko a a1oH cnyuae
6nok nnnunannsaunn paxe ne nHee1 nHenn.
Peannsaunn nepeHennmx knacca noppepxnaae1cn ne aceHn nsmkaHn nporpaHHnpoaannn,
ko1opme Hm paccHa1pnaaeH, a 1aH, rpe a1o aosHoxno, HexannsHm nannk1cn pasnnunmHn. B
nocnepykunx paspenax Hm onnueH, kak cospak1cn nepeHennme knacca a pasnnunmx
nsmkax nporpaHHnpoaannn.
20.3.1. HepeHeuume knacca a Smalltalk
Mm onpepenneH nepeHennme knacca, npoc1o nepeuncnnn nx no nHenaH npn cospannn
noaoro knacca. HanpnHep, nnxe nokasano onncanne knacca Date. Kak ckasano a
noppaspene 20.2.4, akseHnnnpm knacca Date ncnonLsyk1cn pnn npepc1aanennn pa1m.
HepeHennme knacca copepxa1 neckonLko Haccnaoa nnqopHaunn, ko1opan nonesna npn
Hannnynnpoaannn c pa1aHn, aknkuan uncno pne a Hecnuax neancokocnmx ne1, nasaannn
Hecnuea n pne nepenn n 1. p. Bny1pennnH o6pasoH 1akne nepeHennme o6pa6a1maak1cn
kak nepeHennme akseHnnnpa He1aknacca, cansannoro c knaccoH. Hnnunannsaunn
nepeHennmx knacca amnonnne1cn kak uac1L He1opa knacca initialize.
Magnitude subclass: #Date
instanceVariableNames: 'day year'
classVariableNames:
'DaysInMonth FirstDayOfMonth MonthNames SecondsInDay
WeekDayNames'
poolDictionaries: ''
category: 'Numeric-Magnitudes'
20.3.2. HepeHeuume knacca a C++
Hsmk C++ oco6mH o6pasoH aocnpnnnHae1 knkueaoe cnoao static, korpa ono ncnonLsye1cn a
onncannn knacca. 3pecL a1o cnoao noppasyHeaae1, u1o cospae1cn opna konnn snauennn,
converted to PDF by BoJIoc
ko1opan ncnonLsye1cn coaHec1no aceHn akseHnnnpaHn knacca. Takne snauennn
(nepeHennme knacca a naue 1epHnnonornn) nasmaak1cn c1a1nuecknHn aneHen1aHn a C++.
O6munme npaanna anpnHoc1n (onpepenneHme knkueamHn cnoaaHn private, protected nnn
public) npnHennk1cn k c1a1nuecknH aneHen1aH pnn orpannuennn poc1yna k nnH nsane
He1opoa, cansannmx c knaccaHn.
C1a1nueckn aneHen1 nnnunannsnpye1cn ane onpepenennn knacca cnoco6oH, noxoxnH na
nnnunannsaunk rno6anLnmx nepeHennmx, npnueH opnosnaunoc1L nHenn knacca noHorae1
cansa1L onpepenenne c nyxnmH knaccoH (nnc1nnr 20.3). Kak n a cnyuae rno6anLnmx
nepeHennmx, nnnunannsaunn c1a1nueckoro aneHen1a nponsaopn1cn a nporpaHHe 1onLko
opnn pas.
HockonLky a knacce ncnonLsye1cn 1onLko opna konnn c1a1nueckoro aneHen1a, poc1yn k
o1kpm1oHy (public) c1a1nueckoHy aneHen1y Hoxe1 ocyuec1ann1Lcn nanpnHyk. HanpnHep,
knacc CardPile o1o6paxae1 c1onky kap1 cnepykunH o6pasoH:
void CardPile::display()
{
if (top == nilLink)
game->clearArea(x, y, x+Card::CardWidth,
y+Card::CardHeight);
else
top->draw();
}
Onpepenna nonn CardWidth n CardHeight knacca Card 1aknH o6pasoH, Hm ns6eraeH
cospannn o1penLnmx konc1an1 a kaxpoH akseHnnnpe knacca.
3aHe1L1e, u1o c1a1nueckne aneHen1m ne o6nsanm 6m1L o6Lnanenm o1kpm1mHn ecnn onn
ne o1kpm1m, 1o nx poc1ynnoc1L nopunnne1cn o6munmH npaannaH anpnHoc1n. Hsmk C++
1akxe ponyckae1, u1o6m He1opm 6mnn o6Lnanenm kak static. C1a1nueckne He1opm Hory1
o6paua1Lcn 1onLko k c1a1nuecknH pannmH n ao Hnornx o1nouennnx noxoxn na knacc-
He1opm a nsmkax Smalltalk n Objective-C.
20.3.3. HepeHeuume knacca a Java
Hsmk Java, cnepyn C++, ncnonLsye1 knkueaoe cnoao static pnn ykasannn nepeHenno
knacca. Honn pannmx n He1opm, onncannme a c1eke, Hory1 6m1L noHeuenm kak static, 1orpa
onn 6ypy1 npnHennHm k caHoHy knaccy, a ne k ero akseHnnnpaH.
C1a1nueckne nepeHennme Hory1 nHe1L nnnunannsa1opm, ko1opme amnonnnk1cn npn
sarpyske onpepenennn knacca. KpoHe 1oro, 6nok kopa paspeuae1cn noHeua1L kak static,
1orpa on 1akxe 6ype1 amnonnn1Lcn ao apeHn sarpyskn. Cnepykun npnHep nnnkc1pnpye1
a1o. 3pecL onncmaae1cn c1a1nueckn Haccna snauenn, ko1opm nnnunannsnpye1cn uncnaHn
o1 1 po 12:
class statTest
{
static final size = 12;
static int arr[] = new int[size]; // o+nswis maccws
static { // oiw xomanz- s-nonnnxicn npw sarpysxe xnacca
for (int i = 0; i < arr.length; i++)
{
arr[i] = i + 1;
}
}
}
KoH6nnaunn knkueamx cnoa static n final cospae1 nnnunannsnpoaannoe none, ko1opoHy ne
Hoxe1 6m1L npncaoeno snauenne.
converted to PDF by BoJIoc
Kak c1a1nueckne nepeHennme, 1ak n c1a1nueckne He1opm o6muno poc1ynnm uepes nHn
knacca. Opnako pnn ypo6c1aa k nnH Hoxno o6pa1n1Lcn n uepes akseHnnnp knacca.
20.3.4. HepeHeuume knacca a Objective-C
B nsmke Objective-C ne1 nano noppepxkn nepeHennmx knacca. Mm HoxeH nonyun1L neu1o
noxoxee, o6Lnannn npoc1me c1a1nueckne (static) nepeHennme nsmka C a uac1n
implementation onncannn knacca. Takne nepeHennme poc1ynnm 1onLko any1pn qana,
copepxauero peannsaunk, n k nnH ne1 poc1yna y knnen1oa-nopknaccoa n y knnen1oa-
nonLsoaa1ene. HanpnHep, He1opm a nnxecnepykueH knacce Date Hory1 ccmna1Lcn na
c1a1nueckn Haccna dayNames. (D1o1 1pkk pa6o1ae1 1akxe a C++ po 1ex nop, noka ace
qynkunn,

Puc. 20.4. CxeHm penernpoaannn
o6pauakunecn k pannmH, naxopn1cn a opnoH qane.)
# import "Date.h"
static char *dayNames[ ] = {"Bocxpecense",
"Honezensnwx", "Biopnwx", "Cpeza", "-eisepr",
"Hninwua", "Cyoia"};
@implementation Date
...
@end
20.4. Hyxum nu knaccm?
Yun1maan ace nkancm o6Lek1oa, akseHnnnpoa, knaccoa, He1aknaccoa n 1oHy nopo6noro,
neaonLno sapyHaeuLcn, nenLsn nn cospa1L o6Lek1no-opnen1npoaannm nsmk 6es
npnaneuennn knaccoa? Okasmaae1cn, Hoxno, xo1n ne anonne ncno, ueH nporpaHHnpoaanne
a 1aknx peknaccnpoaannmx o6Lek1no-opnen1npoaannmx nsmkax okasmaae1cn npoue nnn
6mc1pee, ueH a nsmkax c knaccaHn. Takxe ne ncno, nannk1cn nn nonyuakunecn nporpaHHm
a ueH-1o 6onee aqqek1nanmHn.
converted to PDF by BoJIoc

Puc. 20.5. PesynL1a1 pa6o1m kanepockonnueckoro nepa
20.4.1. H1o 1akoe suauue?
O6Lek1no-opnen1npoaannm nopxop cansan c 1eH npepc1aanenneH, u1o snanne ec1L
o1necenne konkpe1no aeun k a6c1pak1noHy 1nny. Hna1on, nanpnHep, y1aepxpan, u1o naue
nonn1ne nouapn ne ocnoamaae1cn na kako-nn6o konkpe1no nouapn, no amaopn1cn ns
npen nouapnoc1n. Bce qnsnueckne o6Lek1m 1onLko npn6nnxennn k 6onee
coaepuenno a6c1pakunn; onn acero nnuL 6nepnan 1enL npen.
AnL1epna1nanoe npepc1aanenne y1aepxpae1, u1o nkpn nonyuak1 nnqopHaunk ny1eH
nsyuennn konkpe1nmx o6Lek1oa, a sa1eH o16pacmaak1 necyuec1aennme uac1noc1n, u1o6m
noc1pon1L o6uyk a6c1pakunk. HanpnHep, konuenunn cnona ocnoamaae1cn na konkpe1noH
cnone, ko1oporo pannm uenoaek a Honopoc1n anpen a soonapke. 3a1eH uenoaek cospae1
o6pas a1oporo cnona, coo1nocn ero c 6onee pannnH o6pasuoH. Y a1oro noaoro cnona yun
Hory1 6m1L HenLue, n, 1aknH o6pasoH, nosnakun saknkuae1, u1o pasHep yxa ne nanne1cn
cyuec1aenno xapak1epnc1nko cnonoaoc1n. Hy1eH Hnorokpa1noro noa1opennn 1akoro
npouecca cpaanennn uenoaek ampa6a1maae1 o6uee npepc1aanenne. Kaxpan a6c1pakunn
npnaopn1 o6pa1no k konkpe1nmH akseHnnnpaH, na ko1opmx ona n ocnoamaae1cn.
20.4.2. Qenerupoaauue nonuoHovu
B nsmkax nporpaHHnpoaannn npen o6unoc1n Hexpy konkpe1nmHn akseHnnnpaHn nsaec1na
kak penernpoaanne [Lieberman 1986]. Hpn penernpoaannn ne1 knaccoa aHec1o a1oro
nporpaHHnc1 cospae1 konkpe1nme akseHnnnpm o6Lek1oa, n acn qynkunonanLnoc1L
cansmaae1cn c konkpe1nmHn o6Lek1aHn. Bcnkn pas, korpa o6Lek1 okasmaae1cn noxoxnH na
yxe cyuec1aykun o6Lek1, nporpaHHa Hoxe1 penernpoaa1L uac1L noaepennn noaoro
o6Lek1a o6Lek1y-opnrnnany. k6oe coo6uenne, ko1opoe ne pacnosnae1cn noamH o6Lek1oH,
6ype1 nepeappecoaano o6Lek1y, ko1opoHy ocyuec1aneno penernpoaanne. On a caok
ouepepL Hoxe1 penernpoaa1L caoe noaepenne ppyroHy o6Lek1y n 1. p.
TaknH o6pasoH, Hm HoxeH noc1pon1L nsmk nporpaHHnpoaannn ns o6Lek1oa (ko1opme
copepxa1 nepeHennme n He1opm) n penernpoaannn (nocpepc1aoH ko1oporo o6Lek1
nepeappecoamaae1 amnonnenne nk6mx nepacnosnaaaeHmx He1opoa ppyroHy o6Lek1y).
CoaHec1noe ncnonLsoaanne kopa nponcxopn1 a 1aknx nsmkax uepes npnHenenne o6unx
penera1oa.
B kauec1ae nnnkc1paunn noc1ponH npoc1yk rpaqnueckyk cnc1eHy. HpepnonoxnH, u1o y nac
ec1L o6Lek1, ko1opm pncye1 nnnnn n pearnpye1 na epnnc1aennoe coo6uenne
converted to PDF by BoJIoc
drawFromTo(a, b, c, d). B kauec1ae peakunn na a1o coo6uenne pncye1cn o1pesok cnnouno
nnnnn ns 1oukn c kooppnna1aHn (a,b) k 1ouke c kooppnna1aHn (c,d).
Hpexpe acero Hm noc1ponH nepo, nannkueecn nnc1pyHen1oH pncoaannn, ko1opoe noHnn1
caon kooppnna1m. O6Lek1-nepo nnkancynnpye1 pae nepeHennme, x n y, a 1akxe onpepenne1
He1opm yc1anoakn n coo6uennn a1nx nepeHennmx: getX(), getY(), setX(a), setY(b). 3a1eH
o6Lek1-nepo onpepenne1 paa He1opa pncoaannn a nHenno, moveTo(a,b), ko1opm 1onLko
nepeHeuae1 nepo 6es pncoaannn, n drawTo(a,b), ko1opm pncye1 nnnnk. D1n He1opm Hory1
6m1L onpepenenm c noHouLk cnepykuero nceapokopa:
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
O6Lek1-nepo penernpye1 o1ae1c1aennoc1L sa He1op drawFromTo o6Lek1y-nnnnn (pnc. 20.4).
HpepnonoxnH, u1o nporpaHHnc1 xoue1 cospa1L a1opoe nepo. HcnonLsyn 1exnnky
penernpoaannn, on npexpe acero o6ecneunaae1 onncanne o6Lek1a, coo1nocn ero (no
aosHoxnoc1n) c yxe cyuec1aykunHn o6Lek1aHn. Opna ns qopH onncannn Hoxe1 amrnnpe1L
1ak: a1opoe nepo ponxno aec1n ce6n a 1ounoc1n kak nepaoe, no noppepxnaa1L caon
co6c1aennme kooppnna1m. Hs a1oro onncannn ncno, u1o a1opoe nepo o6nsano copepxa1L
caon co6c1aennme nepeHennme n onpepenn1L He1opm pnn setX n 1. p. Opnako nockonLky ono
penernpye1 ce6n nepaoHy nepy, a1o 6ypy1 epnnc1aennme He1opm, ko1opme napo
onpepenn1L; oc1aaunecn pe1ann noaepennn 6ypy1 ynacnepoaanm o1 nepaoro nepa. Korpa
a1opoHy nepy nocmnae1cn coo6uenne, 1o nonyua1enL (a1opoe nepo) nepecmnae1cn kak
coc1aanan uac1L coo6uennn panLue no ny1n penernpoaannn. Korpa cnepykune coo6uennn
nocmnak1cn o6Lek1y self (knnen1y a 1epHnnonornn n6epHana), nonck naunnae1cn cnoaa c
ncxopnoro nonyua1enn. TeH caHmH coo6uennn setX n getX, ncnonLsyeHme, nanpnHep, a
He1ope drawTo, 6ypy1 conoc1aann1Lcn c He1opaHn a1oporo nepa, a ne nepaoro. D1o1 npouecc
conoc1aanennn ananornuen cnoco6y, npn ko1opoH cansmaanne He1opa acerpa naunnae1cn c
6asoaoro knacca nonyua1enn. Hpo6neHm, aosnnkakune npn a1oH cnoco6e, nponannk1cn n a
caoeH penernpoaannoH akanaanen1e.
[enernpykune o6Lek1m ne acerpa ponxnm nepeonpepenn1L nepeHennme. HpepnonoxnH, Hm
xo1nH cospa1L kanepockonnueckoe nepo, ko1opoe nponsaopn1 o1paxenne o1nocn1enLno
oce x n y, pncyn ue1mpe nnnnn aHec1o opno nnnnn pnn ncxopnoro nepa (pnc. 20.5). Mm
HoxeH aaec1n o6Lek1, ko1opm nepeonpepenne1 1onLko He1op drawTo; ace oc1anLnoe
noaepenne penernpye1cn nepaonauanLnoHy nepy. HockonLky kooppnna1m x n y nannk1cn
kooppnna1aHn ncxopnoro nepa, nsHenennn a nepe 1nna kanepockon npnaopn1 k
nsHenennnH a nepaonauanLnoH nepe. Hoam He1op drawTo amrnnpn1 cnepykunH o6pasoH:
method drawTo(a, b)
self drawFromTo(self getX(), self getY(), a, b)
self drawFromTo(- self getX(), self getY(), a, b)
self drawFromTo(self getX(), self getY(), a, b)
self drawFromTo(- self getX(), self getY(), a, b)
self moveTo(a,b)
end
TenepL npepnonoxnH, u1o nporpaHHnc1 xoue1 onpepenn1L nepo 1nna uepenauka, ko1opoe
coxpanne1 ne 1onLko kooppnna1m, no n opnen1aunk [Abelson 1981]. KpoHe co6c1aenno
pncoaannn, uepenauky Hoxno nayun1L noaopaunaa1Lcn n panra1Lcn anepep nnn nasap
o1nocn1enLno 1ekyue opnen1aunn. Ecnn Hm ncnonLsyeH cyuec1aykuee nepo pnn
coxpanennn kooppnna1 uepenaukn, e no1pe6ye1cn onpepenn1L epnnc1aennyk
nepeHennyk, a 1akxe He1opm turn(amount), forward(amount) n backward(amount).
converted to PDF by BoJIoc
Hn1epecnmH caoc1aoH cnc1eH, ocnoaannmx na npnnunne penernpoaannn, nanne1cn nx
cnoco6noc1L pnnaHnueckn nsHenn1L penera1oa. Hocne 1oro kak 6mno ckonc1pynpoaano
yc1poc1ao uepenauka, a sa1eH nonLsoaa1enL nepenec penernpoaanne c o6munoro nepa
na nepo dasedPen, uepenauka anesanno pasanaae1 a ce6e cnoco6noc1L pncoaa1L ne
1onLko cnnounme, no n nynk1npnme nnnnn. (Ec1ec1aenno, penernpoaanne k o6Lek1y,
ko1opm ne nonnHae1 acex 1pe6yeHmx coo6uenn, Hoxe1 npnaec1n k kpaxy nporpaHHm.)
B onpepenennoH cHmcne o1nouennn o6Lek1/penera1 nopo6nm o1nouennnH
akseHnnnp/knacc, sa ncknkuenneH 1oro, u1o a nepaoH cnyuae ne1 payx cyunoc1e
penera1 npoc1o nanne1cn ppyrnH o6Lek1oH. TeH ne Henee cyuec1aye1 pacnpoc1panennan
npak1nka cospannn knacco-nopo6nmx o6Lek1oa-qa6pnk, ko1opme ne penak1 nnuero, kpoHe
py6nnpoaannn cyuec1aykuero o6Lek1a. HanpnHep, ypae1cn nponsaec1n qa6pnky
uepenauek, ko1opan no sanpocy amnyckae1 noayk uepenauky, nesaancnHyk o1 acex
ppyrnx uepenauek.
Ocnoano nn1epa1ypo no penernpoaannk nanne1cn pa6o1a [Lieberman 1986]. Ero c1a1Ln
nokasmaae1, kak c noHouLk penernpoaannn Hm HoxeH cHopennpoaa1L HexannsH
nacnepoaannn. O6pa1noe y1aepxpenne c noHouLk nacnepoaannn ypae1cn cHopennpoaa1L
penernpoaanne 1akxe 6mno npopeHonc1pnpoaano [Stein 1987]. Hsmk nporpaHHnpoaannn
Self, ocnoaannm na penernpoaannn, 6mn onncan YnrapoH [Ungar 1987]. ToHnnncon
[Tomlinson 1990] npnaopn1 nn1epecnm ananns sa1pa1 apeHenn n naHn1n npn
penernpoaannn n npn nacnepoaannn, npnxopn k saknkuennk, u1o nacnepoaanne a o6ueH
cnyuae nanne1cn 6onee 6mc1pmH n, kak nn ypnan1enLno, 1pe6ye1 HenLue naHn1n.
Ynpaxueuun
1. Kakne acnek1m nonn1nn 1nn pannmx ne onncmaak1cn ka1eropnnHn Bernepa
[Wegner 1986]? Onpepenn1e noame 1oukn spennn, u1o6m o1pasn1L a1n acnek1m.
2. Hsyun1e 1exnnueckne npneHm aepnqnkaunn a c1anpap1nmx nsmkax
nporpaHHnpoaannn (xopouee o6Lncnenne nx npnaopn1cn a pa6o1ax [Gries 1981,
Dijkstra 1976]). C kaknHn npo6neHaHn am c1anknaae1ecL, korpa nm1ae1ecL
npnHenn1L a1n nopxopm k
Fnaaa 21

Peanusaun o61ek1uo-opueu1upoaauumx nsmkoa
UenLk panno knnrn ne nanne1cn cna6pn1L un1a1enn noppo6nmHn nnc1pykunnHn no
peannsaunn nsmkoa nporpaHHnpoaannn. TeH ne Henee o6uee nonnHanne npo6neH,
aosnnkakunx npn aonnouennn o6Lek1no-opnen1npoaannmx nsmkoa, a 1akxe pasnnunme
cnoco6m 6opL6m c nnHn ao Hnornx cnyuanx noHory1 un1a1enk nyuue nonn1L o6Lek1no-
opnen1npoaannme 1exnonornn. B uac1noc1n, c1ane1 nonn1no, a ueH nHenno o6Lek1no-
opnen1npoaannme cnc1eHm o1nnuak1cn o1 6onee 1papnunonnmx cpepc1a. B a1o rnaae
copepxn1cn o6sop neko1opmx nan6onee aaxnmx 1exnnk peannsaunn, a 1akxe ccmnkn na
neo6xopnHyk nn1epa1ypy pnn un1a1ene, ko1opme saxo1n1 npopanny1Lcn panLue.
21.1. KoHnunn1opm u uu1epnpe1a1opm
B unpokoH cHmcne nHek1cn paa ocnoanmx nopxopa k aonnouennk nsmkoa
nporpaHHnpoaannn: koHnnnn1opm n nn1epnpe1a1opm. KoHnnnn1op nepeaopn1 nporpaHHy
nonLsoaa1enn a Haunnnm kop koHnLk1epa, na ko1opoH 6ype1 amnonnn1Lcn nporpaHHa.
Bmsoa koHnnnn1opa ocyuec1anne1cn kak npouecc, ne saancnun o1 amnonnennn
converted to PDF by BoJIoc
nporpaHHm. Hn1epnpe1a1op xe o6nsa1enLno npncy1c1aye1 ao apeHn ncnonnennn nporpaHHm
n nanne1cn co6c1aenno 1o cnc1eHo, ko1opan amnonnne1 nporpaHHy
1
.

1
Kak n a cnyuae 6onLunnc1aa knaccnqnunpykunx pas6nenn, Hexpy unc1mHn
koneunmHn 1oukaHn nocepepnne nHee1cn 6onLuan cepan o6nac1L. Cyuec1ayk1
koHnnnn1opm, ko1opme koHnnnnpyk1 a nn1epak1nanoH pexnHe, nnorpa paxe ao
apeHn amnonnennn nporpaHHm (n no kpane Hepe na c1apnn nouaroao
o1napkn). Takne koHnnnn1opm o6napak1 neko1opmHn poc1onnc1aaHn
nn1epnpe1a1opoa, a 1o xe apeHn o6ecneunaan ao apeHn amnonnennn nporpaHHm
npenHyuec1aa, caoc1aennme koHnnnnpykunH 1exnonornnH. Ananornuno
neko1opme nn1epnpe1a1opm Hory1 nepeaopn1L nporpaHHy a npoHexy1ounoe
npepc1aanenne nnn nceapokop.
B o6ueH cnyuae nporpaHHa, o11pancnnpoaannan koHnnnn1opoH, 6ype1 amnonnn1Lcn
6mc1pee, ueH nporpaHHa, amnonnneHan nop ynpaanenneH nn1epnpe1a1opa. Ho nonnoe
apeHn, sa1pauennoe na npoek1npoaanne, aaop 1ekc1a n sanyck na amnonnenne pnn
koHnnnnpykue cnc1eHm Hoxe1 6m1L 6onLue, ueH pnn nn1epnpe1a1opa. Bonee 1oro, korpa
ao apeHn amnonnennn nporpaHHm aosnnkae1 oun6ka, koHnnnn1op npak1nueckn acerpa
Hoxe1 npepnoxn1L pnn nokannsaunn Hec1a aepon1no oun6kn acero nnuL
crenepnpoaannm acceH6nepnm 1ekc1. Hn1epnpe1a1op xe o6muno nokaxe1 oun6ky a
ncxopnoH 1ekc1e nporpaHHm, aaepennoH nonLsoaa1eneH. TeH caHmH nHek1cn poc1onnc1aa n
nepoc1a1kn y o6onx nopxopoa.
Xo1n opnn nsmkn o6muno koHnnnnpyk1cn, a ppyrne, kak npaanno, nn1epnpe1npyk1cn, a
co6c1aenno nsmke nporpaHHnpoaannn ne1 any1pennnx npnunn, ko1opme amnyxpak1
nporpaHHnc1a, peannsykuero nsmk, am6npa1L opnn ny1L, a ne ppyro. C++ o6muno
koHnnnnpye1cn, no nHek1cn n nn1epnpe1a1opm C++. C ppyro c1oponm, nsmk Smalltalk
nou1n acerpa nn1epnpe1npye1cn, opnako cospanm n akcnepnHen1anLnme Smalltalk-
koHnnnn1opm.
21.2. KoHnunn1opm
Tnnnuno o1nnun1enLno uep1o koHnnnn1opoa nanne1cn 1o, u1o neko1opan nnqopHaunn
1epne1cn npn nepeaope ncxopnoro 1ekc1a nporpaHHm a Haunnnm kop. D1o nan6onee
saHe1no npn npeo6pasoaannn cnHaonnuecknx nHen a appeca nueek naHn1n. Tak, k
nokanLnmH nepeHennmH any1pn npouepypm ckoHnnnnpoaannm kop appecye1cn ne no nx
nHenaH, a uepes qnkcnpoaannm cpanr o1nocn1enLno nauana 6noka naHn1n, cospaaaeHoro
npn axope a npouepypy1. Ananornuno nonn pannmx a sanncn onncmaak1cn ny1eH ykasannn
nx cpanra o1nocn1enLno nauana 6noka, a ne uepes nHena.
HpepnonoxnH, u1o npouepypa copepxn1 nepeHennyk x n sanncL d, ko1opan a caok ouepepL
nHee1 none pannmx y. [onyc1nH, u1o x sanoHnnae1cn a nueke nokanLnoro 6noka
ampenenno naHn1n c nnpekcoH 20, a d naunnae1cn c nuekn 28. Hyc1L none pannmx y
naunnae1cn c aocLHoro 6a1a sanncn d. Hpn a1nx npepnonoxennnx onepa1op npncaanaannn
x:= d.y
Hoxe1 6m1L o11pancnnpoaan a opny acceH6nepnyk koHanpy, ko1opan nepeHeuae1
copepxnHoe cnoaa, pacnonoxennoro a 1pnpua1L uec1oH 6a1e o1 nauana 6noka, a
paapua1yk nueky o1 nauana 6noka:
move AR+36, AR+20
O6pa1n1e annHanne: onepa1opm acceH6nepa ncnonLsyk1 ne cnHaonnueckne nHena
nepeHennmx, a 1onLko nx cHeuennn o1nocn1enLno nauana 6noka.
Kak Hm saHe1nnn a npepmpyunx rnaaax, o6Lek1 nanoHnnae1 sanncL nnn c1pyk1ypy a
1papnunonnmx nsmkax nporpaHHnpoaannn. Hopo6no sanncnH, nonnH pannmx (nepeHennmH
akseHnnnpa) npnnncmaak1cn qnkcnpoaannme cHeuennn o1nocn1enLno nauana o6Lek1a.
Hopknaccm Hory1 1onLko pacunpn1L a1y o6nac1L naHn1n, no ne cokpaua1L ee, 1ak u1o o6LeH
converted to PDF by BoJIoc
naHn1n, ampenenno pnn knacca-no1oHka, c1poro 6onLue, ueH y knacca-npepka. CHeuennn
pannmx pnn nopknaccoa ponxnm coo1ae1c1aoaa1L pacnonoxennk ananornunmx none a
napknaccax (pnc. 21.1).

Puc. 21.1. Coo1ae1c1ane Hexpy nonnHn pannmx a popn1enLckoH n pouepneH knaccax
PaccHo1pnH knaccm GraphicalObject n Ball pnn Hopenn 6nnLnppa, onncanno a rnaae 6.
Knacc GraphicalObject copepxn1 nonn link n region, a knacc Ball po6aanne1 k nnH nonn
direction n energy, opnoapeHenno coxpannn pnn none knacca-npepuec1aennnka a 1ounoc1n
1e xe cHeuennn. To, u1o cHeuennn none pnn popn1enLckoro knacca coxpannk1cn a
pouepneH, ouenL aaxno: a1o nosaonne1 He1opaH, onpepenennmH pnn popn1enLckoro knacca,
o6pa6a1maa1L pannme akseHnnnpa c ncnonLsoaanneH noc1onnnmx cHeuenn, 1ak u1o a1n
qynkunn 6ypy1 pa6o1a1L npaannLno nesaancnHo o1 1oro, k kakoHy knaccy (popn1enk nnn
no1oHky) o1nocn1cn apryHen1. HanpnHep, He1op moveTo knacca GraphicalObject 6ype1 aec1n
ce6n kak nonarae1cn nesaancnHo o1 knacca o6Lek1a-nonyua1enn, nockonLky a1o1 He1op
nonLsye1cn 1onLko o6nac1Lk region o6Lek1a.
21.2.1. Hpo6neHa cpesku
To o6c1on1enLc1ao, u1o pouepnn knacc Hoxe1 1onLko pacunpn1L o6nac1L pannmx,
onpepenennyk popn1enLcknH knaccoH, peuae1 npo6neHy, onncannyk a npepmpyueH
paspene. D1o nosaonne1 koHnnnn1opy 1ak renepnpoaa1L kop pnn npouepypm popn1enLckoro
knacca, u1o ona 6ype1 pa6o1a1L n pnn o6Lek1oa, npnnapnexaunx k pouepneHy knaccy. Ho
a1o xe caoc1ao cospae1 ppyryk npo6neHy.
Kak Hm anpenn a npepmpyunx rnaaax, nonnHopqnan nepeHennan a1o nepeHennan,
ko1opan o6Lnanena kak npnnapnexauan k opnoHy 1nny pannmx, 1orpa kak na caHoH pene
ona copepxn1 snauenne, o1nocnueecn k ppyroHy 1nny. B o6Lek1no-opnen1npoaannmx
nsmkax nepeHennan 1nna popn1enLckoro knacca, kak npaanno, Hoxe1 copepxa1L snauennn
1nna no1oHkoa.
Korpa koHnnnn1op yc1anaannaae1 pasHep nokanLnoro 6noka ampenneHo naHn1n, on,
aoo6ue roaopn, snae1 1onLko peknapnpoaannm 1nn nepeHenno, a ne 1nn, ko1opm ona
6ype1 nHe1L ao apeHn ncnonnennn nporpaHHm. TaknH o6pasoH, aosnnkae1 aonpoc: ckonLko
naHn1n ponxno 6m1L ampeneno nop nokanLnm 6nok? Kak Hm amncnnnn a rnaae 12,
6onLunnc1ao nsmkoa nporpaHHnpoaannn am6npak1 opno ns payx peuenn a1o npo6neHm:
6nok ampenneHo naHn1n copepxn1 ykasa1enn, a ne caHn snauennn;
6nok ampenneHo naHn1n copepxn1 1onLko nonn pannmx popn1enLckoro knacca. Hpn
onepaunn npncaanaannn 1e nonn pannmx no1oHka, ko1opme amxopn1 sa npepenm
popn1enLckoro knacca, o16pacmaak1cn (cpesak1cn).
Y kaxpo ns payx anL1epna1na nHek1cn caon npenHyuec1aa, 1ak u1o Hm ne 6ypeH
koHHen1npoaa1L, kakan ns nnx nyuue. Opnako pnn aac kak pnn nporpaHHnc1a aaxno
nonnHa1L, kakan nHenno 1exnnka ncnonLsye1cn a cnc1eHe, a ko1opo am pa6o1ae1e.

converted to PDF by BoJIoc
21.2.2. Coo1ae1c1aue Hexpy He1opaHu u coo6euunHu
Han6onee noaa1opckoe caoc1ao o6Lek1no-opnen1npoaannoro nporpaHHnpoaannn c 1oukn
spennn peannsaunn coc1on1 a 1oH, u1o nn1epnpe1aunn coo6uennn Hoxe1 saance1L o1 1nna
(knacca) nonyua1enn. To ec1L pasnnunme knaccm o6Lek1oa Hory1 amnonnn1L pasnnunme
npouepypm a kauec1ae peakunn na opno n 1o xe coo6uenne. [nn naue rpaqnuecko
Hopenn knacc Wall pearnpye1 na coo6uenne draw nnmH o6pasoH, ueH knacc Ball.
Ho a1o npnunne kaxpm o6Lek1 o6nsan copepxa1L kako-1o cnoco6 onpepenennn, kakan
npouepypa ponxna amsmaa1Lcn pnn coo6uennn, aocnpnnnHaeHoro o6Lek1oH. Bonee 1oro,
Hm xo1nH, u1o6m HexannsH, ko1opm ncnonLsye1cn pnn cansmaannn He1opa n npouepypm,
pa6o1an kak Hoxno 6mc1pee. [nn koHnnnnpykunx nsmkoa nporpaHHnpoaannn 1exnnka,
nan6onee uac1o ncnonLsyeHan c uenLk o6ecneun1L ckopoc1L, nasmaae1cn 1a6nnua
anp1yanLnmx He1opoa.
21.2.3. Ta6num aup1yanuumx He1opoa
Opno ns aosHoxnmx peuenn npo6neHm coo1ae1c1ann Hexpy He1opaHn n coo6uennnHn
coc1on1 a 1oH, u1o6m pasHec1n1L nonn pnn He1opoa a 1ounoc1n 1eH xe cnoco6oH, kak
ampenne1cn naHn1L pnn none pannmx. 3nauennnHn none He1opoa nannk1cn ykasa1enn na
coo1ae1c1aykune qynkunn, kak a1o nokasano na pnc. 21.2.
[nn 1oro u1o6m amsaa1L nyxnm He1op, poc1a1ouno asn1L snauenne no npaannLnoHy
cHeuennk any1pn o6Lek1a, pasmHenoaa1L ero, u1o6m nonyun1L npouepypy, n sa1eH amsaa1L
ee. Opnako 1ako nopxop, xo1n on n npneHneH no ckopoc1n amnonnennn, nanne1cn
sa1pa1nmH c 1oukn spennn ppyroro aaxnoro pecypca a nHenno naHn1n. Kaxpm o6Lek1
ponxen o1aopn1L naHn1L (opnn ykasa1enL) pnn kaxporo He1opa. Bonee 1oro, cospanne
o6Lek1a aknkuae1 a ce6n nnnunannsaunk acex none He1opoa, u1o npepc1aanne1 co6o
nenyxnme sa1pa1m. BosHoxen koHnpoHncc Hexpy ckopoc1Lk n naHn1Lk, ko1opm ocnoaan
na 1oH, u1o ace akseHnnnpm opnoro knacca ponxnm coaHec1no ncnonLsoaa1L opnn n 1e xe
He1opm. Hpn 1akoH nopxope pnn kaxporo knacca cospae1cn epnnc1aennan 1a6nnua,
nasmaaeHan 1a6nnue anp1yanLnmx He1opoa, n ace akseHnnnpm knacca copepxa1 opnn
ykasa1enL na nee (pnc. 21.3). Hnnunannsaunn noaoro akseHnnnpa noppasyHeaae1 yc1anoaky
a1oro ykasa1enn na 1a6nnuy anp1yanLnmx He1opoa.

Puc. 21.2. Me1opm, peannsoaannme kak nonn
converted to PDF by BoJIoc

Puc. 21.3. [aa 6nnLnppnmx uapa c o6ue 1a6nnue anp1yanLnmx He1opoa
3nauennn none a 1a6nnue anp1yanLnmx He1opoa a1o ykasa1enn na npouepypm. Ecnn Hm
npepnonoxnH, u1o a1n npouepypm nsaec1nm koHnnnn1opy n ne nsHennk1cn ao apeHn
amnonnennn nporpaHHm, 1o 1a6nnua Hoxe1 6m1L cospana c1a1nueckn ao apeHn koHnnnnunn.
1o6m amnonnn1L He1op, naH nyxno sna1L 1onLko ero cHeuenne a 1a6nnue anp1yanLnmx
He1opoa.
Kak n o6nac1L pannmx, 1a6nnua anp1yanLnmx He1opoa knacca-npepuec1aennnka axopn1 ao
ace 1a6nnum knaccoa-no1oHkoa, a cHeuennn He1opoa a 1a6nnue popn1enn 6ypy1 1eHn xe
caHmHn a 1a6nnuax no1oHkoa. Knacc, ko1opm nacnepye1 He1opm napknacca, npoc1o
konnpye1 o6uyk uac1L ns ero 1a6nnum anp1yanLnmx He1opoa a caok. Korpa a nopknacce
nepeonpepenne1cn He1op, neo6xopnHo 1onLko nsHenn1L sanncL pnn a1oro He1opa. Ha
pnc. 21.4 nokasana 1a6nnua anp1yanLnmx He1opoa pnn knaccoa Wall n Ball, kaxpm ns
ko1opmx nanne1cn no1oHkoH knacca GraphicalObject. 3aHe1L1e, u1o y nnx o6une ykasa1enn
na He1opm, ynacnepoaannme o1 popn1enLckoro knacca, n u1o nopnpok He1opoa coanapae1 c
1eH, ko1opm sapan a popn1enLckoH knacce.
converted to PDF by BoJIoc

Puc. 21.4. Ta6nnum anp1yanLnmx He1opoa pnn knaccoa Wall n Ball
Pas yx koHnnnn1op snae1, rpe na1n ykasa1enL na He1op, 1o He1op Hoxe1 6m1L amsaan kak
c1anpap1nan npouepypa. Honyua1enL paccHa1pnaae1cn kak ecnn 6m on 6mn nepamH
napaHe1poH a cnncke apryHen1oa, n 1eH caHmH on poc1ynen kak snauenne nepeHenno self
(this a C++). HpepnonoxnH, k npnHepy, u1o vtab a1o any1pennee nHn nonn,
npepc1aannkuee ykasa1enL na 1a6nnuy anp1yanLnmx He1opoa a o6Lek1e x, n u1o cHeuenne
pnn He1opa hitBy a 1a6nnue paano 12. Torpa amsoa He1opa
x.hitBy(y)
6ype1 npeo6pasoaan a cnepykuee any1pennee npepc1aanenne:
(*(*(x.vtab))[12]) (x,y)
3aHe1L1e, u1o nHn He1opa ne nonanne1cn a amxopnoH kope, n napnexaun He1op 6ype1
am6npa1Lcn nesaancnHo o1 1oro, nanne1cn nn x o6Lek1oH knacca Ball, nnn knacca Wall, nnn
kaknH-nn6o ppyrnH rpaqnuecknH o6Lek1oH GraphicalObject. B 1epHnnax amnonnennn
nporpaHHm saronoaok nocmnaeHoro coo6uennn 1pe6ye1 pae onepaunn c ykasa1ennHn n opny
onepaunk naxoxpennn aneHen1a a Haccnae.
21.2.4. Kopupoaauue uHeu
HockonLky ace He1opm nsaec1nm ao apeHn koHnnnnunn n ne Hory1 6m1L nsHenenm ao apeHn
amnonnennn, 1o 1a6nnum anp1yanLnmx He1opoa a1o npoc1o c1a1nueckne o6nac1n pannmx,
yc1anaannaaeHme koHnnnn1opoH. Onn coc1on1 ns ykasa1ene na coo1ae1c1aykune He1opm.
converted to PDF by BoJIoc
HockonLky pepak1opm canse (linkers) n sarpysunkn (loaders) npeapauak1 ccmnkn a
cHeuennn (paspeuak1 ccmnkn), ncxopn ns cnHaonnuecknx nHen, neo6xopnHo o6ecneun1L
neko1opm HexannsH, ko1opm nosaonnn 6m ns6exa1L npo1naopeunn a cn1yaunn, korpa paa
He1opa nHek1 opno n 1o xe nHn. Tnnnunan cxeHa koH6nnnpye1 nHn knacca n nHn He1opa.
Tak, He1op draw ns knacca Ball npeapauae1cn, nanpnHep, a Ball::draw npn any1penneH
npepc1aanennn. O6muno nonLsoaa1enk nnkorpa ne 1pe6ye1cn sna1L a1o nHn, ecnn 1onLko
ne1 neo6xopnHoc1n anannsnpoaa1L acceH6nepnm kop, cospannm koHnnnn1opoH.
B nsmkax nporpaHHnpoaannn, nopo6nmx C++, nosaonnkunx eue 6onee neperpyxa1L
He1opm sa cue1 cnn1nn neopnosnaunoc1n ny1eH anannsa 1nnoa apryHen1oa, 1pe6ye1cn 6onee
cnoxnoe kopnpoaanne a c1nne Ipenn1 c ncnonLsoaanneH nHenn knacca, nHenn He1opa n
1nnoa apryHen1oa. HanpnHep, 1pn konc1pyk1opa knacca Complex, onncannme a npepmpyunx
rnaaax, nonyua1 coo1ae1c1aenno any1pennne nHena Complex::Complex,
Complex::Complex_float n Complex::Complex_float_float. Takne any1pennne nHena
nasmaak1cn kycouno-coc1aanmHn (mangled). Onn 6maak1 ouenL pnnnnmHn. Kak Hm anpenn,
any1pennee nHn ne ncnonLsye1cn pnn nepecmnkn coo6uennn; ono npnHenne1cn 1onLko npn
konc1pynpoaannn 1a6nnu anp1yanLnmx He1opoa c uenLk cpena1L nHena ynnkanLnmHn pnn
pepak1opa canse.
Mnoxec1aennoe nacnepoaanne neckonLko ycnoxnne1 ncnonLsoaanne 1a6nnu anp1yanLnmx
He1opoa. [e1ann, opnako, amxopn1 sa paHkn panno knnrn. 3ann1epecoaaaunecn un1a1enn
Hory1 na1n 6onee nonnoe onncanne a [Ellis 1990].
21.2.5. Ta6num pucne1vepusauu
HockonLky nsmkn nporpaHHnpoaannn, nopo6nme C++ n Object Pascal, nannk1cn nsmkaHn co
c1a1nuecknHn 1nnaHn pannmx, onn Hory1 onpepenn1L na a1ane koHnnnnunn no kpane
Hepe 1nn popn1enLckoro knacca nk6oro o6Lek1noro ampaxennn. TeH caHmH 1a6nnua
anp1yanLnmx He1opoa ponxna 6m1L poano 1ako, u1o6m aHec1n1L He1opm, peannsoaannme
pnn knacca. [nn nsmkoa nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx (apope
Objective-C) 1a6nnua anp1yanLnmx He1opoa o6nsana aknkua1L ace coo6uennn, nonnHaeHme
aceHn knaccaHn, n ona noa1opne1cn pnn kaxporo knacca. K npnHepy, ecnn npnnoxenne
copepxn1 20 knaccoa n a cpepneH kaxpm knacc ncnonLsye1 10 He1opoa, 1o naH
1pe6ye1cn 20 1a6nnu, kaxpan ns ko1opmx coc1on1 ns 200 sannce. Tpe6oaannn k pasHepy
1a6nnu 6mc1po c1anoan1cn upesHepnmHn, n 1orpa aosnnkae1 no1pe6noc1L a 6onee ypaunoH
nopxope.

Puc. 21.5. O6Lek1 n ero 1a6nnua pncne1uepnsaunn
AnL1epna1nanm nopxop coc1on1 a 1oH, u1o6m cansa1L c kaxpmH knaccoH 1a6nnuy, ko1opan,
a o1nnune o1 1a6nnum anp1yanLnmx He1opoa, coc1on1 ns nap cenek1op/He1op. Ona
nasmaae1cn 1a6nnue pncne1uepnsaunn. Cenek1opm coo1ae1c1ayk1 1onLko 1eH He1opaH,
ko1opme qak1nueckn peannsoaanm pnn pannoro knacca. K nacnepyeHmH He1opaH poc1yn
ocyuec1anne1cn uepes ykasa1enL ns a1o 1a6nnum, ko1opm ykasmaae1 na 1a6nnuy
pncne1uepnsaunn popn1enLckoro knacca (pnc. 21.5).
converted to PDF by BoJIoc
Kak n a cnc1eHe c 1a6nnuaHn anp1yanLnmx He1opoa, npn ncnonLsoaannn 1a6nnu
pncne1uepnsaunn kaxpm o6Lek1 copepxn1 any1pn ce6n nenanm (1o ec1L neo6Lnanennm)
ykasa1enL na 1a6nnuy pncne1uepnsaunn, cansannyk c ero knaccoH. D1o1 nenanm ykasa1enL
nasmaae1cn ykasa1eneH cansn isa (isa link) (ne cHeunaa1L c ycnoaneH 6m1L akseHnnnpoH
(is-a relation) pnn knaccoa). Hepecmnka coo6uennn a nsmke Objective-C apope cnepykuero
ampaxennn ns sapaun o aocLHn qepsnx
[neighbor checkrow: row column: column]
npeo6pasye1cn koHnnnn1opoH nsmka Objective-C1 a kop
objc_msgSend(neighbor, "checkrow:column:", row, column)
Oynkunn objc_msgSend, nasmaaeHan qynkune nepecmnkn coo6uenn, cnepye1 no
ykasa1enk cansn isa pnn nepaoro apryHen1a c 1eH, u1o6m na1n coo1ae1c1aykuyk 1a6nnuy
pncne1uepnsaunn. 3a1eH qynkunn nepecmnkn coo6uenn npocHa1pnaae1 1a6nnuy
pncne1uepnsaunn a nonckax sanncn, coo1ae1c1aykue cenek1opy. Ecnn 1akan sanncL
napena, amsmaae1cn nyxnm He1op. Ecnn nopo6noro He1opa ne o6napyxeno, nonck
npoponxae1cn a 1a6nnue pncne1uepnsaunn napknacca. Ecnn a konue konuoa poc1nrny1
kopneao knacc Object, a He1op 1ak n ne napen, ampae1cn coo6uenne o6 oun6ke a1ana
amnonnennn.
Ksmupoaauue He1opoa
Xo1n pnn nsmkoa nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx 1a6nnum
pncne1uepnsaunn nannk1cn 6onee akonoHnunmHn no o6LeHy, ueH 1a6nnum anp1yanLnmx
He1opoa, sa1pa1m apeHenn na amsoa He1opa snaun1enLno 6onLue. KpoHe 1oro, a1n sa1pa1m
nponopunonanLnm rny6nne nacnepoaannn. Ecnn 6m a1n naknapnme pacxopm 6mnn
nenpeoponnHm, 1o paspa6o1unkn ckopee o1kasanncL 6m aoo6ue o1 HexannsHa
nacnepoaannn, cornacnauncL na no1epk Houn papn amnrpmua a aqqek1nanoc1n.
K cuac1Lk, Hm HoxeH a snaun1enLno c1enenn cnnsn1L a1n no1epn ao apeHn amnonnennn
nporpaHHm sa cue1 cnepykuero npoc1oro nopxopa. BypeH xpann1L epnnyk pnn ace
cnc1eHm kau-1a6nnuy He1opoa, k ko1opmH nepaano ocyuec1annncn poc1yn. Ona
nnpekcnpye1cn xau-kopoH
2


2
Xaunpoaanne (hashing) He1op npn6nnxennoro nnpekcnpoaannn pnn
cokpauennn noncka nyxnmx pannmx. Hpn xaunpoaannn kaxpoHy pannoHy
c1aan1cn a coo1ae1c1ane xau-kop (hash-code), ko1opm ncnonLsye1cn pnn
ynopnpounaannn pannmx a xau-1a6nnue n cnyxn1 cenek1opoH npn nx noncke.
Xapak1epnan uep1a xau-kopa coc1on1 a 1oH, u1o on, aoo6ue roaopn, ne nanne1cn
ynnkanLnmH (1o ec1L pasnnunme pannme Hory1 nopoxpa1L npn amuncnennnx
opnnakoam xau-kop). Opnako on 6mc1po amuncnne1cn n nosaonne1 snaun1enLno
cokpa1n1L o6nac1L noncka pannmx. HpnHepoH xaunpoaannn cnyxn1 cnoaapL c
saknapkaHn no Hec1y cHenm nepao 6ykam cnoa. 3pecL nepaan 6ykaa cnoaa
amc1ynae1 a kauec1ae ero xau-kopa. HpnHeu. nepea.
onpepenneHmH no cenek1opy He1opa. Kaxpan sanncL a kau-1a6nnue npepc1aanne1 co6o
1poky, coc1onuyk ns ykasa1enn na knacc (pnn a1o uenn cnyxn1 co6c1aenno 1a6nnua
pncne1uepnsaunn), snauennn cenek1opa n ykasa1enn na He1op.
Korpa qynkunk nepecmnkn coo6uenn npocn1 na1n He1op, ko1opm coo1ae1c1aye1 nape
knacccenek1op, ona npexpe acero ocyuec1anne1 nonck a kau-1a6nnue (pnc. 21.6). Ecnn
sanncL a kaue no Hec1y pacnonoxennn xau-kopa coo1ae1c1aye1 1pe6yeHmH cenek1opy n
knaccy, 1o coo1ae1c1aykun He1op Hoxe1 6m1L amnonnen neHepnenno. Ecnn ne1
npoaopn1cn npouecc noncka, onncannm amue. B pesynL1a1e noncka nenocpepc1aenno
nepep amnonnenneH napennoro He1opa nponcxopn1 o6noanenne kau-1a6nnum. Hpn a1oH
sanncL, copepxaauancn no Hec1y coo1ae1c1aykuero xau-kopa (paccun1maaeHoHy no
cenek1opy coo6uennn), nepesanncmaae1cn noamHn pannmHn. O6pa1n1e annHanne, u1o
converted to PDF by BoJIoc
snauenne knacca a kaue coo1ae1c1aye1 knaccy, c ko1oporo 6mn naua1 nonck He1opa, a ne
knaccy, a ko1opoH He1op 6mn a konue konuoa napen.

Puc. 21.6. Oynkunn nepecmnkn coo6uennn, npoaepnkuan kau 1a6nnuy
Hpn napnexaueH am6ope qynkun amuncnennn xau-kopa n pasHepa kaua Hoxno poc1nuL
uac1o1m nonapannn a kau okono 9095 npouen1oa, u1o yHenLuae1 naknapnme sa1pa1m na
nepepauy coo6uennn po ypoann, nnuL npnHepno a paa pasa 6onLuero, ueH pacxopm na
c1anpap1nm amsoa npouepypm [Cox 1986]. D1o1 nokasa1enL anonne 6naronpnn1no
cpaannaae1cn c sa1pa1aHn npn ncnonLsoaannn 1a6nnu anp1yanLnmx He1opoa.
21.3. Hu1epnpe1a1opm
Hn1epnpe1a1opm o6muno npepnou1n1enLnee koHnnnn1opoa, korpa nsHenunaoc1L pec1an
nporpaHHm npeamuae1 neko1opm nopor, npneHneHm pnn qnkcnpoaannoro acceH6nepnoro
kopa. HsHenunaoc1L pec1an nopoxpae1cn pasnoo6pasnmHn nc1ounnkaHn. HanpnHep, pnn
nsmkoa c pnnaHnuecknHn 1nnaHn pannmx na a1ane koHnnnnunn nenLsn npepckasa1L, k
kakoHy 1nny pannmx 6ype1 o1nocn1Lcn snauenne nepeHenno (xo1n nsmk Objective-C cnyxn1
npnHepoH nsmka nporpaHHnpoaannn c pnnaHnuecknHn 1nnaHn pannmx, ko1opm 1eH ne
Henee, koHnnnnpye1cn). [pyro nc1ounnk nsHenunaoc1n aosnnkae1, ecnn nonLsoaa1enk
nosaoneno nepeonpepenn1L He1opm ao apeHn amnonnennn nporpaHHm.
Hopxop, o6muno ncnonLsyeHm pnn nn1epnpe1a1opoa, npeo6pasoamaa1L ncxopnyk
nporpaHHy a acceH6nepnm nsmk amcokoro ypoann, uac1o nasmaaeHm 6a1-kopoH,
nockonLky o6muno kaxpan nnc1pykunn sakopnpoaana a opnoH 6a1e. Ha pnc. 21.7 nokasan
6a1-kop cnc1eHm Little Smalltalk. C1apune ue1mpe 6n1a ncnonLsyk1cn pnn kopnpoaannn
onepaunn, a Hnapune ncnonLsyk1cn pnn ykasannn noHepa onepanpa. Ecnn 1pe6yk1cn
noHepa onepanpoa 6onLune uec1napua1n, npnHenne1cn pacunpennan qopHa nnc1pykunn, n
nocnepykun 6a1 uennkoH copepxn1 snauenne onepanpa. HeHnorne nnc1pykunn (1nna
nocna1L coo6uenne n neko1opme cneunanLnme) 1pe6yk1 pononnn1enLnmx 6a1oa.
converted to PDF by BoJIoc

Puc. 21.7. Ba1-kop a cnc1eHe Little Smalltalk
CeppueH nn1epnpe1a1opa nanne1cn unkn, ko1opm oxaa1maae1 orpoHnm onepa1op
nepeknkuennn switch (case, select n 1. p.). Unkn cun1maae1 nocnepoaa1enLnme 6a1-kopm, a
onepa1op am6opa nepepae1 ynpaanenne 1o uenouke kopa, ko1opan amnonnne1 1pe6yeHoe
pec1ane. Mm ne 6ypeH apaaa1Lcn a pnckyccnn o any1penneH npepc1aanennn nporpaHHm
(sann1epecoaannme un1a1enn Hory1 o6pa1n1Lcn k pa6o1e [Budd 1987]) n ckonuen1pnpyeHcn
ncknkun1enLno na o6pa6o1ke nepepaun coo6uenn.
while (timeslice-- > 0)
{
high = nextByte(); // cuwiais c