1 / 22
文档名称:

Abstract classes抽象类.ppt

格式:ppt   大小:739KB   页数:22页
下载后只包含 1 个 PPT 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

Abstract classes抽象类.ppt

上传人:放射辐射 11/26/2022 文件大小:739 KB

下载得到文件列表

Abstract classes抽象类.ppt

相关文档

文档介绍

文档介绍:该【Abstract classes抽象类 】是由【放射辐射】上传分享,文档一共【22】页,该文档可以免费在线阅读,需要了解更多关于【Abstract classes抽象类 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。Abstractclasses抽象类
*
Whyhaveabstractmethods?
SupposeyouhaveaclassShape,butitisn’tabstract
Shapeshouldnothaveadraw()method
EachsubclassofShapeshouldhaveadraw()method
NowsupposeyouhaveavariableShapefigure;wherefigurecontainssomesubclassobject(suchasaStar)
(),becausetheJavacompilercan’ttellinadvancewhatkindofvaluewillbeinthefigurevariable
Aclass“knows”itssuperclass,butdoesn’tknowitssubclasses
Anobjectknowsitsclass,butaclassdoesn’tknowitsobjects
Solution:GiveShapeanabstractmethoddraw()
NowtheclassShapeisabstract,soitcan’tbeinstantiated
Thefigurevariablecannotcontaina(generic)Shape,becauseitisimpossibletocreateone
Anyobject(suchasaStarobject)thatisa(kindof)Shapewillhavethedraw()method
()beingalegalcallanddoesnotgiveasyntaxerror
*
Aproblem
classShape{...}
classStarextendsShape{ voiddraw(){...} ... }
classCrescentextendsShape{ voiddraw(){...} ... }
ShapesomeShape=newStar();
Thisislegal,becauseaStarisaShape
();
Thisisasyntaxerror,becausesomeShapemightnothaveadraw()method
Remember:Aclassknowsitssuperclass,butnotitssubclasses
*
Asolution
abstractclassShape{ abstractvoiddraw(); }
classStarextendsShape{ voiddraw(){...} ... }
classCrescentextendsShape{ voiddraw(){...} ... }
ShapesomeShape=newStar();
Thisislegal,becauseaStarisaShape
However,ShapesomeShape=newShape();isnolongerlegal
();
Thisislegal,becauseeveryactualinstancemusthaveadraw()method
*
Interfaces
Aninterfacedeclares(describes)methodsbutdoesnotsupplybodiesforthem
interfaceKeyListener{ publicvoidkeyPressed(KeyEvente); publicvoidkeyReleased(KeyEvente); publicvoidkeyTyped(KeyEvente); }
Allthemethodsareimplicitlypublicandabstract
Youcanaddthesequalifiersifyoulike,butwhybother?
Youcannotinstantiateaninterface
Aninterfaceislikeaveryabstractclass—noneofitsmethodsaredefined
Aninterfacemayalsocontainconstants(finalvariables)
*
Designinginterfaces
Mostofthetime,youwilluseSun-suppliedJavainterfaces
Sometimesyouwillwanttodesignyourown
Youwouldwriteaninterfaceifyouwantclassesofvarioustypestoallhaveacertainsetofcapabilities
Forexample,ifyouwanttobeabletocreateanimateddisplaysofobjectsinaclass,youmightdefineaninterfaceas:
publicinterfaceAnimatable{ install(Panelp); display(); }
NowyoucanwritecodethatwilldisplayanyAnimatableclassinaPanelofyourchoice,simplybycallingthesemethods
*
ImplementinganinterfaceI
Youextendaclass,butyouimplementaninterface
Aclasscanonlyextend(subclass)oneotherclass,butitcanimplementasmanyinterfacesasyoulike
Example:
classMyListener implementsKeyListener,ActionListener{…}
*
ImplementinganinterfaceII
Whenyousayaclassimplementsaninterface,youarepromisingtodefineallthemethodsthatweredeclaredintheinterface
Example:
classMyKeyListenerimplementsKeyListener{ publicvoidkeyPressed(KeyEvente){...}; publicvoidkeyReleased(KeyEvente){...}; publicvoidkeyTyped(KeyEvente){...}; }
The“...”indicatesactualcodethatyoumustsupply
NowyoucancreateanewMyKeyListener
*
PartiallyimplementinganInterface
Itispossibletodefinesomebutnotallofthemethodsdefinedinaninterface:
abstractclassMyKeyListenerimplementsKeyListener{ publicvoidkeyTyped(KeyEvente){...}; }
Sincethisclassdoesnotsupplyallthemethodsithaspromised,itisanabstractclass
Youmustlabelitassuchwiththekeywordabstract
Youcanevenextendaninterface(toaddmethods):
interfaceFunkyKeyListenerextendsKeyListener{...}
*
Whatareinterfacesfor?
Reason1:Aclasscanonlyextendoneotherclass,butitcanimplementmultipleinterfaces
Thisletstheclassfillmultiple“roles”
InwritingApplets,itiscommontohaveoneclassimplementseveraldifferentlisteners
Example: classMyAppletextendsApplet implementsActionListener,KeyListener{ ... }
Reason2:Youcanwritemethodsthatworkformorethanonekindofclass