Sunteți pe pagina 1din 28

Ob}cct-Oricntco Progrumming

in
1lc REAL Dcul
Iava |s an CC |anguage whlch means LhaL !ava
programs are developed as groups of
lnLerrelaLed ob[ecLs
1hls module lnLroduces you Lo ob[ecLorlenLed
programmlng (CC) and Lhe role lL plays ln
!ava
CC concepLs such as ects c|asses |nher|tance
and meths wlll be dlscussed
1he coverage of each concepL ls followed up by
example !ava code
1he CC skllls you wlll learn ln Lhls module provlde
a background LhaL ls necessary when lL comes Lo
developlng effecLlve !ava programs
,u|e |earn|ng ect|ves
fLer compleLlng Lhe module you wlll have Lhe
skllls and knowledge necessary Lo
1 undersLand Lhe relaLlonshlp beLween classes
and ob[ecLs
2 CreaLe slmple class deflnlLlons
3 dd behavlor Lo classes uslng meLhods
Cect funamenta|s
xplore Lhe baslcs of ob[ecLorlenLed programmlng
W1he goal of all programmlng languages ls Lo make lL easler for
humans Lo communlcaLe wlLh machlnes
W Cb[ecLorlenLed programmlng languages exLend Lhls noLlon by
allowlng programmers Lo Lhlnk ln Lerms of realworld ob[ecLs
W !ava has only added Lo Lhe exclLemenL surroundlng ob[ecL
orlenLed programmlng or 9 (rhymes wlLh hoop) for shorL
WCC aLLempLs Lo narrow Lhe gap beLween problems ln Lhe real
world and soluLlons ln Lhe compuLer world
W n Lhe real world we naLurally Lhlnk ln Lerms
of Lhlngs buL preCC programmlng
languages forced programmers Lo Lhlnk ln
Lerms of blocks of code (procedures) and Lhe
daLa Lhey manlpulaLe
W CC suggesLs LhaL programmers Lhlnk ln
Lerms of ob[ecLs or Lhlngs much llke we Lhlnk
of Lhlngs ln Lhe real world
Cect funamenta|s (cnt'%
undersLandlng ob[ecLs and classes
W 1he whole basls of CC ls Lhe object whlch ls
a comblnaLlon of daLa and Lhe procedures
LhaL acL on Lhe daLa
W rocedures ln an ob[ecL are referred Lo as
etboJs whlle daLa ls referred Lo as member
daLa 1hls merger of daLa and meLhods ls
known as eocopsolotloo and provldes a
means of more accuraLely modellng real
world ob[ecLs
xplaln Lhe concepLs of ob[ecLs and classes
Jhat Is an Cect?
W Cb[ecLs are key Lo undersLandlng object
otleoteJ Lechnology Look around rlghL now
and youll flnd many examples of realworld
ob[ecLs your dog your desk your Lelevlslon
seL your blcycle
enef|t f Cects
W 1o geL a beLLer grasp
of Lhe beneflL of
ob[ecLs Lhlnk for a
momenL abouL Lhe
common
characLerlsLlcs of real
world ob[ecLs
Cb[ecLs (conL'd)
enef|t f Cects
Cb[ecLs (conL'd)
enef|t f Cects
Cb[ecLs (conL'd)
enef|t f Cects
Cb[ecLs (conL'd)
enef|t f Cects
Cb[ecLs (conL'd)
enef|t f Cects
Cb[ecLs (conL'd)
Cb[ecLs (conL'd)
W 8ealworld ob[ecLs share Lwo characLerlsLlcs
1hey all have stote and bebovlot
xample
1 uogs have sLaLe (name color breed hungry) and
behavlor (barklng feLchlng wagglng Lall)
2 8lcycles also have sLaLe (currenL gear currenL
pedal cadence currenL speed) and behavlor
(changlng gear changlng pedal cadence applylng
brakes)
denLlfylng Lhe sLaLe and behavlor for realworld
ob[ecLs ls a greaL way Lo begln Lhlnklng ln Lerms
of ob[ecLorlenLed programmlng
Cb[ecLs (conL'd)
W sk yourself Lwo quesLlons WhaL posslble sLaLes
can Lhls ob[ecL be ln? and WhaL posslble
behavlor can Lhls ob[ecL perform?
xample
1 ?our deskLop lamp may have only Lwo posslble
sLaLes (on and off) and Lwo posslble behavlors
(Lurn on Lurn off)
2 ?our deskLop radlo mlghL have addlLlonal sLaLes
(on off currenL volume currenL sLaLlon) and
behavlor (Lurn on Lurn off lncrease volume
decrease volume seek scan and Lune)
Cb[ecLs (conL'd)
W 1hese realworld observaLlons all LranslaLe
lnLo Lhe world of ob[ecLorlenLed
programmlng
sofLware ob[ecL
ofLware Cb[ecLs
W ofLware ob[ecLs are concepLually slmllar Lo realworld
ob[ecLs Lhey Loo conslsL of sLaLe and relaLed behavlor
W n ob[ecL sLores lLs sLaLe ln lelJs (varlables ln some
programmlng languages) and exposes lLs behavlor Lhrough
etboJs (funcLlons ln some programmlng languages)
W ,eLhods operaLe on an ob[ecLs lnLernal sLaLe and serve as
Lhe prlmary mechanlsm for ob[ecLLoob[ecL communlcaLlon
W Pldlng lnLernal sLaLe and requlrlng all lnLeracLlon Lo be
performed Lhrough an ob[ecLs meLhods ls known as Joto
eocopsolotloo a fundamenLal prlnclple of ob[ecLorlenLed
programmlng
ofLware Cb[ecLs (conL'd)
W Conslder a blcycle for example
blcycle modeled as a sofLware ob[ecL
W 8y aLLrlbuLlng sLaLe (currenL speed currenL pedal
cadence and currenL gear) and provldlng meLhods for
changlng LhaL sLaLe Lhe ob[ecL remalns ln conLrol of
how Lhe ouLslde world ls allowed Lo use lL lor
example lf Lhe blcycle only has 6 gears a meLhod Lo
change gears could re[ecL any value LhaL ls less Lhan 1
or greaLer Lhan 6
W 8undllng code lnLo lndlvldual sofLware ob[ecLs provldes
a number of beneflLs lncludlng
,u|ar|ty 1he source code for an ob[ecL can be
wrlLLen and malnLalned lndependenLly of Lhe source
code for oLher ob[ecLs Cnce creaLed an ob[ecL can be
easlly passed around lnslde Lhe sysLem
ofLware Cb[ecLs (conL'd)
Infrmat|nh||ng 8y lnLeracLlng only wlLh an
ob[ecLs meLhods Lhe deLalls of lLs lnLernal
lmplemenLaLlon remaln hldden from Lhe ouLslde
world
3 Ce reuse f an ob[ecL already exlsLs (perhaps
wrlLLen by anoLher sofLware developer) you can use
LhaL ob[ecL ln your program 1hls allows speclallsLs Lo
lmplemenL/LesL/debug complex Laskspeclflc ob[ecLs
whlch you can Lhen LrusL Lo run ln your own code
4 |ugga|||ty an eugg|ng ease f a parLlcular ob[ecL
Lurns ouL Lo be problemaLlc you can slmply remove lL
from your appllcaLlon and plug ln a dlfferenL ob[ecL as
lLs replacemenL 1hls ls analogous Lo flxlng mechanlcal
problems ln Lhe real world f a bolL breaks you replace
lt noL Lhe enLlre machlne
ofLware Cb[ecLs (conL'd)
Jhat Is a C|ass?
W n Lhe real world youll ofLen flnd many
lndlvldual ob[ecLs all of Lhe same klnd
W 1here may be Lhousands of oLher blcycles ln
exlsLence all of Lhe same make and model
W ach blcycle was bullL from Lhe same seL of
blueprlnLs and Lherefore conLalns Lhe same
componenLs n ob[ecLorlenLed Lerms we say
LhaL your blcycle ls an lostooce of Lhe closs o
objects known as blcycles
W nf |s the |uepr|nt frm wh|ch |n|v|ua|
ects are create
W 1he followlng 8lcycle class ls one posslble lmplemenLaLlon of a blcycle
c|ass |cyc|e
|nt caence 0
|nt spee 0
|nt gear
v| changeCaence(|nt newVa|ue%
caence newVa|ue

v| changeGear(|nt newVa|ue%
gear newVa|ue

v| speeDp(|nt |ncrement%
spee spee + |ncrement

v| app|yrakes(|nt ecrement%
spee spee ecrement

v| pr|ntStates(%
Systemutpr|nt|n(caence+caence+ spee+spee+ gear+gear%

W ?ou may have noLlced LhaL Lhe 8lcycle class


does noL conLaln a maln meLhod
W 1haLs because lLs noL a compleLe appllcaLlon
lLs [usL Lhe blueprlnL for blcycles LhaL mlghL be
oseJ ln an appllcaLlon
W 1he responslblllLy of creaLlng and uslng new
8lcycle ob[ecLs belongs Lo some oLher class ln
your appllcaLlon
W Peres a 8lcycleuemo class LhaL creaLes Lwo separaLe 8lcycle
ob[ecLs and lnvokes Lhelr meLhods
c|ass |cyc|eDem
pu||c stat|c v| ma|n(Str|ng args%
]] Create tw |fferent |cyc|e ects
|cyc|e |ke new |cyc|e(%
|cyc|e |ke new |cyc|e(%
]] Invke meths n thse ects
|kechangeCaence(S0% |kespeeDp(0%
|kechangeGear(% |kepr|ntStates(%
|kechangeCaence(S0% |kespeeDp(0%
|kechangeGear(% |kechangeCaence(40%
|kespeeDp(0% |kechangeGear(3%
|kepr|ntStates(%


Jhat |s the utput f
the prgram?
W 1he ouLpuL of Lhls LesL prlnLs Lhe endlng pedal
cadence speed and gear for Lhe Lwo blcycles
cadence30 speed10 gear2
cadence40 speed20 gear3
ct|v|ty
1 CreaLe a class erson
2 dd aLLrlbuLes such as name age address
Lelephone
3 dd C118 and 118 meLhods LhaL geL
and seL Lhe aLLrlbuLes correspondlngly
x seLge() geLge()
4 CreaLe 10 dlfferenL lnsLances of Class erson

S-ar putea să vă placă și