Programming MethodologyLecture09.docx

上传人:b****8 文档编号:11131795 上传时间:2023-02-25 格式:DOCX 页数:24 大小:31.84KB
下载 相关 举报
Programming MethodologyLecture09.docx_第1页
第1页 / 共24页
Programming MethodologyLecture09.docx_第2页
第2页 / 共24页
Programming MethodologyLecture09.docx_第3页
第3页 / 共24页
Programming MethodologyLecture09.docx_第4页
第4页 / 共24页
Programming MethodologyLecture09.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

Programming MethodologyLecture09.docx

《Programming MethodologyLecture09.docx》由会员分享,可在线阅读,更多相关《Programming MethodologyLecture09.docx(24页珍藏版)》请在冰豆网上搜索。

Programming MethodologyLecture09.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1