您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业财务 > java语言程序设计-基础篇--第八版--英文课件(第10章)
Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Chapter10ThinkinginObjects1Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807MotivationsYouseetheadvantagesofobject-orientedprogrammingfromtheprecedingtwochapters.Thischapterwilldemonstratehowtosolveproblemsusingtheobject-orientedparadigm.Beforestudyingtheseexamples,wefirstintroduceseverallanguagefeaturesforsupportingtheseexamples.2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ObjectivesTocreateimmutableobjectsfromimmutableclassestoprotectthecontentsofobjects(§10.2).Todeterminethescopeofvariablesinthecontextofaclass(§10.3).Tousethekeywordthistorefertothecallingobjectitself(§10.4).Toapplyclassabstractiontodevelopsoftware(§10.5).Toexplorethedifferencesbetweentheproceduralparadigmandobject-orientedparadigm(§10.6).Todevelopclassesformodelingcompositionrelationships(§10.7).Todesignprogramsusingtheobject-orientedparadigm(§§10.8-10.10).Todesignclassesthatfollowtheclass-designguidelines(§10.11).3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ImmutableObjectsandClasses4Ifthecontentsofanobjectcannotbechangedoncetheobjectiscreated,theobjectiscalledanimmutableobjectanditsclassiscalledanimmutableclass.IfyoudeletethesetmethodintheCircleclassintheprecedingexample,theclasswouldbeimmutablebecauseradiusisprivateandcannotbechangedwithoutasetmethod.Aclasswithallprivatedatafieldsandwithoutmutatorsisnotnecessarilyimmutable.Forexample,thefollowingclassStudenthasallprivatedatafieldsandnomutators,butitismutable.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Example5publicclassStudent{privateintid;privateBirthDatebirthDate;publicStudent(intssn,intyear,intmonth,intday){id=ssn;birthDate=newBirthDate(year,month,day);}publicintgetId(){returnid;}publicBirthDategetBirthDate(){returnbirthDate;}}publicclassBirthDate{privateintyear;privateintmonth;privateintday;publicBirthDate(intnewYear,intnewMonth,intnewDay){year=newYear;month=newMonth;day=newDay;}publicvoidsetYear(intnewYear){year=newYear;}}publicclassTest{publicstaticvoidmain(String[]args){Studentstudent=newStudent(111223333,1970,5,3);BirthDatedate=student.getBirthDate();date.setYear(2010);//Nowthestudentbirthyearischanged!}}Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807WhatClassisImmutable?6Foraclasstobeimmutable,itmustmarkalldatafieldsprivateandprovidenomutatormethodsandnoaccessormethodsthatwouldreturnareferencetoamutabledatafieldobject.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ScopeofVariablesThescopeofinstanceandstaticvariablesistheentireclass.Theycanbedeclaredanywhereinsideaclass.Thescopeofalocalvariablestartsfromitsdeclarationandcontinuestotheendoftheblockthatcontainsthevariable.Alocalvariablemustbeinitializedexplicitlybeforeitcanbeused.7Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ThethisKeywordThethiskeywordisthenameofareferencethatreferstoanobjectitself.Onecommonuseofthethiskeywordisreferenceaclass’shiddendatafields.Anothercommonuseofthethiskeywordtoenableaconstructortoinvokeanotherconstructorofthesameclass.8Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ReferencetheHiddenDataFields9publicclassFoo{privateinti=5;privatestaticdoublek=0;voidsetI(inti){this.i=i;}staticvoidsetK(doublek){Foo.k=k;}}Supposethatf1andf2aretwoobjectsofFoo.Invokingf1.setI(10)istoexecutethis.i=10,wherethisrefersf1Invokingf2.setI(45)istoexecutethis.i=45,wherethisrefersf2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807CallingOverloadedConstructor10publicclassCircle{privatedoubleradius;publicCircle(doubleradius){this.radius=radius;}publicCircle(){this(1.0);}publicdoublegetArea(){returnthis.radius*this.radius*Math.PI;}}Everyinstancevariablebelongstoaninstancerepresentedbythis,whichisnormallyomittedthismustbeexplicitlyusedtoreferencethedatafieldradiusoftheobjectbeingconstructedthisisusedtoinvokeanotherconstructorLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ClassAbstractionandEncapsulationClassabstractionmeanstoseparateclassimplementationfromtheuseoftheclass.Thecreatoroftheclassprovidesadescriptionoftheclassandlettheuserknowhowtheclasscanbeused.Theuseroftheclassdoesnotneedtoknowhowtheclassisimplemented.Thedetailofimplementationisencapsulatedandhiddenfromtheuser.11ClassContract(Signaturesofpublicmethodsandpublicconstants)ClassClassimplementationislikeablackboxhiddenfromtheclientsClientsusetheclassthroughthecontractoftheclassLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsrese
本文标题:java语言程序设计-基础篇--第八版--英文课件(第10章)
链接地址:https://www.777doc.com/doc-2881522 .html