istheinterfacebetweenindividualsofdifferentranks.
WithintheJavaprogramminglanguage,aninterfaceisatype,justasaclassisatype.Likeaclass,aninterfacedefinesmethods.Unlikeaclass,aninterfaceneverimplementsmethods;instead,classesthatimplementtheinterfaceimplementthemethodsdefinedbytheinterface.Aclasscanimplementmultipleinterfaces.
Thebicycleclassanditsclasshierarchydefinewhatabicyclecanandcannotdointermsofits"bicycleness."Butbicyclesinteractwiththeworldonotherterms.Forexample,abicycleinastorecouldbemanagedbyaninventoryprogram.Aninventoryprogramdoesn'tcarewhatclassofitemsitmanagesaslongaseachitemprovidescertaininformation,suchaspriceandtrackingnumber.Insteadofforcingclassrelationshipsonotherwiseunrelateditems,theinventoryprogramsetsupacommunicationprotocol.Thisprotocolcomesintheformofasetofmethoddefinitionscontainedwithinaninterface.Theinventoryinterfacewoulddefine,butnotimplement,methodsthatsetandgettheretailprice,assignatrackingnumber,andsoon.
Toworkintheinventoryprogram,thebicycleclassmustagreetothisprotocolbyimplementingtheinterface.Whenaclassimplementsaninterface,theclassagreestoimplementallthemethodsdefinedintheinterface.Thus,thebicycleclasswouldprovidetheimplementationsforthemethodsthatsetandgetretailprice,assignatrackingnumber,andsoon.
Youuseaninterfacetodefineaprotocolofbehaviorthatcanbeimplementedbyanyclassanywhereintheclasshierarchy.Interfacesareusefulforthefollowing:
Capturingsimilaritiesamongunrelatedclasseswithoutartificiallyforcingaclassrelationship
Declaringmethodsthatoneormoreclassesareexpectedtoimplement
Revealinganobject'sprogramminginterfacewithoutrevealingitsclass
Modelingmultipleinheritance,afeatureofsomeobject-orientedlanguagesthatallowsaclasstohavemorethanonesuperclass
??
??
??
??
上一篇:
ASP_net电子购物商城系统+论文(功能结构图、业务流程图、数据流程图、数据字典)(论文和程序)
下一篇:
医学论文写作格式要求