您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业财务 > java语言程序设计-基础篇--第八版--英文课件(第14章)
1Chapter14AbstractClassesandInterfaces2MotivationsYoulearnedhowtowritesimpleprogramstodisplayGUIcomponents.Canyouwritethecodetorespondtouseractionssuchasclickingabutton?HandleEventRun3ObjectivesTodesignanduseabstractclasses(§14.2).ToprocessacalendarusingtheCalendarandGregorianCalendarclasses(§14.3).Tospecifycommonbehaviorforobjectsusinginterfaces(§14.4).Todefineinterfacesanddefineclassesthatimplementinterfaces(§14.4).TodefineanaturalorderusingtheComparableinterface(§14.5).ToenableobjectstolistenforactioneventsusingtheActionListenerinterface(§14.6).TomakeobjectscloneableusingtheCloneableinterface(§14.7).Toexplorethesimilaritiesanddifferencesbetweenanabstractclassandaninterface(§14.8).Tocreateobjectsforprimitivevaluesusingthewrapperclasses(Byte,Short,Integer,Long,Float,Double,Character,andBoolean)(§14.9).Tocreateagenericsortmethod(§14.10).Tosimplifyprogrammingusingautomaticconversionbetweenprimitivetypesandwrapperclasstypes(§14.11).TousetheBigIntegerandBigDecimalclassesforcomputingverylargenumberswitharbitraryprecisions(§14.12).TodesigntheRationalclassfordefiningtheRationaltype(§14.13).4AbstractClassesandAbstractMethodsGeometricObjectCircleRectangleTestGometricObjectRunGeometricObject-color:String-filled:boolean-dateCreated:java.util.Date#GeometricObject()#GeometricObject(color:string,filled:boolean)+getColor():String+setColor(color:String):void+isFilled():boolean+setFilled(filled:boolean):void+getDateCreated():java.util.Date+toString():String+getArea():double+getPerimeter():doubleCircle-radius:double+Circle()+Circle(radius:double)+Circle(radius:double,color:string,filled:boolean)+getRadius():double+setRadius(radius:double):void+getDiameter():doubleRectangle-width:double-height:double+Rectangle()+Rectangle(width:double,height:double)+Rectangle(width:double,height:double,color:string,filled:boolean)+getWidth():double+setWidth(width:double):void+getHeight():double+setHeight(height:double):voidThe#signindicatesprotectedmodifierAbstractclassAbstractmethodsareitalicizedMethodsgetAreaandgetPerimeterareoverriddeninCircleandRectangle.SuperclassmethodsaregenerallyomittedintheUMLdiagramforsubclasses.5abstractmethodinabstractclassAnabstractmethodcannotbecontainedinanonabstractclass.Ifasubclassofanabstractsuperclassdoesnotimplementalltheabstractmethods,thesubclassmustbedefinedabstract.Inotherwords,inanonabstractsubclassextendedfromanabstractclass,alltheabstractmethodsmustbeimplemented,eveniftheyarenotusedinthesubclass.6objectcannotbecreatedfromabstractclassAnabstractclasscannotbeinstantiatedusingthenewoperator,butyoucanstilldefineitsconstructors,whichareinvokedintheconstructorsofitssubclasses.Forinstance,theconstructorsofGeometricObjectareinvokedintheCircleclassandtheRectangleclass.7abstractclasswithoutabstractmethodAclassthatcontainsabstractmethodsmustbeabstract.However,itispossibletodefineanabstractclassthatcontainsnoabstractmethods.Inthiscase,youcannotcreateinstancesoftheclassusingthenewoperator.Thisclassisusedasabaseclassfordefininganewsubclass.8superclassofabstractclassmaybeconcreteAsubclasscanbeabstractevenifitssuperclassisconcrete.Forexample,theObjectclassisconcrete,butitssubclasses,suchasGeometricObject,maybeabstract.9concretemethodoverriddentobeabstractAsubclasscanoverrideamethodfromitssuperclasstodefineitabstract.Thisisrare,butusefulwhentheimplementationofthemethodinthesuperclassbecomesinvalidinthesubclass.Inthiscase,thesubclassmustbedefinedabstract.10abstractclassastypeYoucannotcreateaninstancefromanabstractclassusingthenewoperator,butanabstractclasscanbeusedasadatatype.Therefore,thefollowingstatement,whichcreatesanarraywhoseelementsareofGeometricObjecttype,iscorrect.GeometricObject[]geo=newGeometricObject[10];11TheAbstractCalendarClassandItsGregorianCalendarsubclassjava.util.GregorianCalendar+GregorianCalendar()+GregorianCalendar(year:int,month:int,dayOfMonth:int)+GregorianCalendar(year:int,month:int,dayOfMonth:int,hour:int,minute:int,second:int)ConstructsaGregorianCalendarforthecurrenttime.ConstructsaGregorianCalendarforthespecifiedyear,month,anddayofmonth.ConstructsaGregorianCalendarforthespecifiedyear,month,dayofmonth,hour,minute,andsecond.Themonthparameteris0-based,thatis,0isforJanuary.java.util.Calendar#Calendar()+get(field:int):int+set(field:int,value:int):void+set(year:int,month:int,dayOfMonth:int):void+getActualMaximum(field:int):int+add(field:int,amount:int):void+getTime():java.util.Date+setTime(date:java.util.Date):voidConstructsadefaultcalendar.Returnsthevalueofthegivencalendarfield.Setsthegivencalendartothespecifiedvalue.Setsthecalendarwiththespecifiedyear,month,anddate.Themonthparameteris0-based,thatis,0isforJanuary.Returnsthemaximumvaluethatthespecifiedcalendarfieldcouldhave.Addsorsubtractsthespecifiedamountoftimetothegivencalendarfield.ReturnsaDateobjectrepresentingthiscalendar’stimevalue(millionsecondoffsetfromtheUnixepoch).Setsthiscalendar’stimewiththegivenDateobject.12TheAbstractCalendarClassandItsGregorianCalendarsubclassAninstanceofjava.util.Daterepresentsaspecificinstantintim
本文标题:java语言程序设计-基础篇--第八版--英文课件(第14章)
链接地址:https://www.777doc.com/doc-2881525 .html