Programming MethodologyLecture09.docx
《Programming MethodologyLecture09.docx》由会员分享,可在线阅读,更多相关《Programming MethodologyLecture09.docx(24页珍藏版)》请在冰点文库上搜索。
ProgrammingMethodologyLecture09
ProgrammingMethodology-Lecture09
Instructor(MehranSahami):
Alrighty,welcomeback.Wow,that'sprettyloud.WelcomebacktoCS106a.IhopeIdidn'tjustshatteryoureardrums.Andthanksformakingitoutintherain.Ithinktherainmighthavestoppedafewpeoplefrommakingittoday.But,actually,todayisoneofthemostimportantlecturesofthewholequarter,soit'stoobadthatitwouldhappentohappenthatway.
Soacoupleofquickannouncements:
Oneisthatthere'stwohandouts,oneoncodingstyle,andoneontheuseofvariables.I'dencourageyoutoreadbothofthembecausetheyarebothextremelycriticalconceptsinthisclass.Therearesomethingsthat?
it'slike,"Yeah,it'snotsoimportant."Thesetwoarereallyimportantsopleasemakesuretoreadthehandout.
ThereisacoupleofnewconceptsIwanttoshowyoubrieflyinthebeginning,andthenwe'regonnabegintoputawholebunchofthingstogether.Sosofarinclass,whatwe'vedoneis,whenyousawCarolinewedidabunchofstuffwithJavasofar,we'veshownyouabunchofbitsandpiecesofthingsandputsomelargerpiecestogether.Andtoday'sthedaywhereitreallyallcomestogether,andawholebunchofstuffthatwe'vetalkedaboutbefore,whereIsaid,"Oh,lateronthis'llmakemoresense,"today'sthedaywhenhopefullythatwillallmakesense.
Sofirstthingtocover,justverybriefly,issomethingcalled,"Strings."Andifyou'vebeenreadingalonginthebookyou'veseensomereferencestoStrings.Wehaven'ttalkedabouttheminclass,andnowit'stimethatwespentalittlebitoftimetalkingaboutStrings.Inaboutanotherweekandahalfwe'llspendawholebunchoftimetalkingaboutStrings,butyoushouldatleastgetalittleintroductiontothem.
SowhatisaString?
AStringisjustatype,likewehadintsandwehaddoubles,andthoseboolean,stufflikethat.String,exceptStringstartswithacapital"S,"it'snotlowercase,isactuallyatype,andsowecandeclarevariablesofthistype;like,wecanhavesomevariablecalled"str,"whichisaString.
AndwhatdoesaStringhold?
WhataStringreallysortofmeansisastringofcharacters,it'sapieceoftextisallaStringis.AndthewayweusuallydenoteaStringisitsinsidedouble-quotes,orthe""thatwe'reusedto.SowemighthavesomeString,andinitializeitbysettingitequalto,"Hello,space,there."Soit'sperfectlyfinetohavespacesandotherkindsofcharactersinsideofaString.And,basically,it'sjustapieceoftextthatsays,"Hellothere."It'sjustonevariablethathappenstohavethisvalue,"Hellothere."Andsotheimportantthingisit'stextenclosedindoublequotes.
AndyoucanthinkofthetextthatyouactuallyassigntotheString.Youcanusethingsonitlikeconcatenation,justlikeyoudidinaprintlin,whenyouactuallywanttoprintsomestuffonthescreen,weusetheplusoperationtoconcatenatepiecesoftexttogether.Well,wecandothatwithStrings.
SowecouldhavesomeString,we'llcallthisString,"name."Andwemightsetname="Bob."Andwemighthavesomeintegerage,whichisjustsetto"20."AndthenwemighthavesomeotherString,sosomeotherStrings,whichwewanttosettobesomethinglike"name:
+,"andthenwhateverthenameactuallyisinthisotherString,soname.Whatthat'lldoisconcatenatethewordBobontoname:
.Andthenmaybewewanttoconcatenateontothatsomethinglikeage,andthenconcatenateontothatthevalueofthevariableage.
Andsowhatwe'llgethereiswe'llgetsomethingthatintheendsisjustsomevariable,soit'sjustsomeboxthatcontainssomething.Andwhatitwillcontainis"name:
"thenconcatenatedonwithwhatevernameevaluatedto.Well,name:
whenwelookitupintheboxisjustBob,sowe'llsay,"name:
Bob."Andthenconcatenateontospace"age:
"andthenthevalueofwhateveragewas,whichis"20."Justlikethiswouldworkinaprintlin,ifyouweretosayprintline,andhaveparensaroundthiswholething,youwouldexpectittoprintthisout.YoucouldactuallyjustassignthattoaString.Sosamesortofconcatenationworks.
Andthenyoumightwonder,howdoyouassigntheseStringsorgettheseStrings?
Youcanassignwhatwerefertoas"literal,"whichisanactualvaluetotheString.YoucanbuildupaStringbyconcatenatingabunchofotherstufftogether.
Sometimesyouwanttoreadinsometextfromtheuser,andthere'salittlemethodcalled"readLine,"justlikereadintandreaddouble,whereyougiveitsomepieceoftextbutit'sgonnadisplayaquestionmarkbecauseit'sgonnaaskforsomething.Andwhatitgivesyoubackisbasicallythewholelinethattheusertypedin.Soit'snotjustlookingforanintoradouble,butiftheytypeinawholelineoftext,includingcharacters,itgivesyouthatbackasaString,that'swhatitreturns,andyoucanassignitsomewhere.Likesomevariablewemightcall"line,"that'satypeString.
Soyoushouldjust,atthislevel,seewhataStringis,understandit,beabletogetthemfromusers,inaboutaweekandahalfwe'lldoawholebunchoffunkythingswithStringsandgetintothenitty-gritty.Butatthispoint,thisisjusttogiveyoualittlebitofanideaastowhattheyarebecausewe'regonnaactuallyusethemlaterontoday.Andtherearesomereferencesinthebookthathopefullywillmakealittlebitmoresense,butupuntilnow,wedidn'tactuallyneedthem.Sowejustkindofdeferredthediscussion.
IsthereanyquestionsaboutString?
Allright.
Sothenextbigthing,hereistherealbigtopicthatwe'regonnacovertoday,iswritingourown"classes."Sothiswholetime,whenwe'vebeenwritingclasses,sowewouldwritesomethinglike,"MyProgram,"anditwouldextendConsoleProgram,andwe'dhaveallofourmethodsandstuffinsideofthatclassforMyProgram.Wouldn'titbekindofinterestingtoactuallyhavemultipleclasses.Becauseinthedaysofyore,wetalkedaboutaJavaprogramasreallyjustcomprisedofmultipleclasses.Sotoday'sthedaywhereyoulearnhowtowriteotherclassesotherthanjustthemainprogramthatyou'reactuallywriting.
Sowhat'sthegeneralformforactuallywritingaclass.Theideaiswesay"public."Forrightnowallofourclassesaregonnabepublic.Thewordclass,andthenwegivethenameoftheclass.Sothisshouldbefamiliartoyou,justlikewhenwewroteprogramsthatextendedConsoleProgram,wehadsomenamehere,I'llputanunderlinetoindicatethatthat'snotactuallyaliteralnameyoujustgiveitaname,andthenpotentiallyyoucouldalsoaddontothis,"extends,"andthensomesuperclass.
Andsowhatyou'resayingisthatyou'recreatinganewclasswiththisnamethatpotentiallyextendssomeotherclass.Andtheninsideyouhavesomeopenbrace,youhavethebodyinsidehere,sothiscouldbeallyourmethods.Itcouldalsobevariablesthataredefined,aswetalkedaboutbefore,variousinstancevariablesorivar'sintheclass,andthenthecloseoftheclass.
Now,thispartoverhere,I'llputsortofindashedparensbecauseit'soptional.Youactuallydon'thavetoextendsomethingaspartofaclass.Ifyoudon'textendsomethingaspartofaclass,whatJavasortofsaysisthedefaultiseverythinginmyuniverse,atleastforthetimebeing,areclasses.Soifyoudon'ttellmethatyou'reextendingsomeexistingclass,therearesomedefaultclasscalled"object,"whichbydefaultyouwillextendifyoudon'tsayextendsomeotherexistingclass.SoeverythingattheendofthedayinJava'sworldsortofmovesupthechain,orthehierarchythatwetalkedaboutacoupleweeksago,andendsupanobject.Soeverythingattheendofthedayreallyisanobject,andifyou'renotextendingsomethinginparticularthenyou'rejustdirectlyanobject.
Soifyouwanttowritethisclass,whatyouendupdoingisyouwanttocreateanewfile.AndI'llshowyouhowtodothatinEclipseinjustasecond.But,basically,that'swhyIputitintheboxhere,thenameofthefileshouldbe?
wekindofdefineittobewhatevertheclassnamewasandthena.javaattheend.Andthe.javalet'susknowthatthisisaJavaprogram.Soifthisthingoverhere,thisclass,happenstobeclassBob,wewouldhavesomefileBob.javathatwouldbewhereallthecodethatwewritefortheclassBobactuallylives.
Andthestuffthat'sgonnabeinsidehereisallthestufflikethemethods,andvariables,andconstants,whicharejustaparticularformofvariable,they'rejustafinalvariablesotheygetnoothervalue.Andthestuffthatyouhaveinsidehere,themethodsandthevariables,havecertainvisibilityassociatedwiththem,aswetalkedaboutbefore.Thesethingscaneitherbepublicortheycanbeprivate.
Andthedifferencebetweenthosetwo,hopefullyit'llmakealittlebitmoresense,ismethodsorvariablesthatarepublicarewhatwerefertoasbeing"exportedoutoftheclass."Soifyouwriteawholebunchofclasses,let'ssaywewritethreeclasses.AndsoIhavemyclassBob,andBobmighthaveinitsomepublicmethod.Well,ifIhavesomeotherclassMary,MarycanactuallycallthepublicmethodsofBob.Soifit'spublicanyonecanaccessthem.
Ifthey'reprivate,theycanonlybeaccessedbyothermethodsinsideoftheclass.SoifIhavesomemethodinsideBob,orsomevariableinsideBobthat'sprivate,theonlythingsthatcanrefertoitareothermethodsinsideBob.IfIhavesomeotherclassMary,themethodsinMarycannotrefertotheprivatemembers,ortheprivateelementsofBob.Sothat'stheimportantthingtokeepinmind.
There'sacoupleothernotions.There'sanotionknownas"protected."Andwe'lltalkaboutthatpotentially