COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt

上传人:marr****208 文档编号:133977551 上传时间:2020-06-01 格式:PPT 页数:49 大小:171.50KB
返回 下载 相关 举报
COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt_第1页
第1页 / 共49页
COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt_第2页
第2页 / 共49页
COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt_第3页
第3页 / 共49页
COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt_第4页
第4页 / 共49页
COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt_第5页
第5页 / 共49页
点击查看更多>>
资源描述

《COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt》由会员分享,可在线阅读,更多相关《COS240Lec15_JAVAAbstractClassesInterfacespptcos240lec15_javaabstractclassesinterfaces.ppt(49页珍藏版)》请在金锄头文库上搜索。

1、1 COS240O OLanguagesAUBG COSdept Lecture15Title AbstractClassesandInterfacesinJavaReference COS240Syllabus 2 LectureContents ReviewohInheritanceandPolymorphismAbstractClassesWhyAbstractMethods InterfacesExample theComparableInterfaceExample theComparableInterfaceInterfacesvs AbstractClasses 3 Inheri

2、tance Review 4 Polymorphism Review 5 Interfaces AbstractClasses Briefpresentation 6 Interfaces AbstractClasses TheInterfaceconcept 7 Interface publicinterfaceDriver voidturnWheel doubleangle voidpressAccelerator doubleamount voidpressBrake doubleamount publicclassBusDriverimplementsDriver mustinclud

3、eimplementationforeachofthethreemethodsfromDriver Nomethodbodies justmethodheader Mustprovideimplementationwhenused publicinterfaceDriver voidturnWheel doubleangle voidpressAccelerator doubleamount voidpressBrake doubleamount 8 publicclassBusDriverextendsPersonimplementsDriver mustincludeimplementat

4、ionforeachofthethreemethodsfromDriver Mayalsohave Thisisa backdoor approachtomultipleinheritanceforJavaviaextendsandimplements Singleinheritanceviaextendsandextrainheritanceviaimplements 9 Interfaces AbstractClasses TheAbstractClassconcept 10 AbstractClasses Aclasswheresomemethodsareunspecified like

5、inaninterface Theunspecifiedmethodsaredeclaredabstract Theclassalsoisdeclaredabstract Anabstractclassmustbesub classed i e extended youcannotinstantiateobjectsofanabstracttype Usefulifyouwanttospecifyan interface alongwithsomedefaultbehaviors SimilarforC 11 AbstractClassExample abstractclassCacheFil

6、e Stringfilename byte contents voidflush Writefiletodisk voidrefresh Loadfilefromdisk abstractStringdeserialize abstractbyte serialize Strings Thesemethodsaredefined Thesemethodsareabstractbecausehowyouwanttostoredataintothefileisapplication dependent Mayhavedefinedmethodsandabstractmethods 12 Abstr

7、actClasses Interfaces Comprehensivepresentation 13 Inheritancehierarchy Ifyoumovefromsuperclassdowntosubclass classesbecomemorespecificandmoreconcrete Ifyoumovefromsubclassuptosuperclass classesbecomemoregeneralandlessspecificSometimessuperclassissoabstractthatitcannothaveanyspecificinstances Suchac

8、lassisreferredtoasanABSTRACTCLASS 14 GeometricObject Circle Rectangle Object GeometricObject CircleRectangleCommonproperties Color filled nofilled dateCreatedCircle specificproperties radiusRectangle specificproperties width height 15 GeometricObject Circle Rectangle GeometricObject CircleRectangleC

9、ommonproperties Color filled nofilled dateCreatedCircle specificpropertiesandbehavior Radius getArea getPerimeter Rectangle specificpropertiesandbehavior width height getArea getPerimeter 16 Inheritancehierarchy IsitreasonabletogeneralizegetArea getPerimeter methods Fromoneside Sincewecancomputearea

10、andperimeterforallgeometricobjects itisbettertodefinegetArea getPerimeter asmethodsinsuperclassGeometricObject Fromotherside Bothmethodscannotbeimplementedinthebaseclassbecausetheirimplementationdependsonspecificproperties radiusorheight width ofthegeometricobjectdefinedinthesubclasses Suchmethodsar

11、ereferredtoasabstractmethodsandaredenotedinthesuperclassusingmodifierabstract Classwithabstractmethodsbecomesanabstractclassandismusttobealsodenotedabstract Seenextslide 17 OpenfileProgGeometricObjectAbstractClass java abstractclassGeometricObject publicabstractdoublegetArea publicabstractdoublegetP

12、erimeter Youcannotcreateinstancesofabstractclassesusingnewoperator Constructorsinabstractclassesareprotectedbecausetheyareusedonlybysubclasses Superclassdefinescommonfeatures incl methodsgetArea andgetPerimeter Becauseyoudon tknowhowtocomputeareaandperimeterofgeometricobjects bothmethodsaredefinedas

13、abstractmethods Thesemethodsareimplemented overriddeninthesubclasses 18 OpenfileProgGeometricObjectAbstractClass java classCircleextendsGeometricObject publicdoublegetArea returnMath PI radius radius publicdoublegetPerimeter return2 Math PI radius publicStringtoString overriddenmethodreturn CircleCi

14、rcle n super toString 19 OpenfileProgGeometricObjectAbstractClass java classRectangleextendsGeometricObject publicdoublegetArea returnwidth height publicdoublegetPerimeter return2 width height publicStringtoString overriddenmethodreturn RectangleRectangle n super toString 20 OpenfileProgGeometricObj

15、ectAbstractClass java Whydoweneedabstractmethods Whatbenefitsifany Browsethemain method Itcreatestwogeometricobjects circle rectangleInvokesequalArea method haveacarefullookattheformalparameterstypeInvokesdisplayGeometricObject method haveacarefullookattheformalparametertype 21 OpenfileProgGeometric

16、ObjectAbstractClass java publicclassProgGeometricObjectAbstractClass publicstaticvoidmain Stringargs GeometricObjectgeo1 newCircle 5 GeometricObjectgeo2 newRectangle 5 3 System out println Thetwoobjecthavesamearea equalArea geo1 geo2 displayGeometricObject geo1 displayGeometricObject geo2 endofmainpublicstaticbooleanequalArea GeometricObjecto1 GeometricObjecto2 returno1 getArea o2 getArea publicstaticvoiddisplayGeometricObject GeometricObjecto System out println Theareais o getArea System out pr

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号