资源描述:
《JavaObjectModel-notes.pdf》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、JavaObjectModelTheJavaObjectModelTopicsinthissectioninclude:•Javaclasses,includingtheuseofaccessrights,inheritance,methoddefinitions,constructors,andinstanceversusclassmembers•Javapackages.•Javainterfaces,andwhyaretheyaresoimportant•Javadynamicloading,andhowapplicationclassesareloadedondemandIntrod
2、uctionAJavaprogramconsistsofasetofclassdefinitions,optionallygroupedintopackages.Eachclassencapsulatesstateandbehaviorappropriatetowhatevertheclassmodelsintherealworldandmaydictateaccessprivilegesofitsmembers.Inthischapter,youwilllearnhowJavasupportstheprimaryfeaturesofanobject-orientedprogrammings
3、ystem:encapsulation,datahiding,polymorphism,andinheritance.Weassumeaknowledgeoftheobject-orientedparadigm.ClassesMinimally,aclassdefinesacollectionofstatevariables,aswellasthefunctionalityforworkingwiththesevariables.ClassesarelikeCstructorPascalrecorddefinitionsthatallowfunctiondefinitionswithinth
4、em.Thesyntaxfordefiningaclassisstraightforward:classClassName{variablesmethods}Forexample,anEmployeeclassmightresemble:classEmployee{Stringname;...}Typically,Javaclassesaredefinedoneper.javafilesoEmployeewouldbeinEmployee.javaandcompiledtoEmployee.class.Whenaclassreferencesanotherclass,theJavacompi
5、lerfindstheassociatedclassdefinitiontoperformtypecheckingandsoon.Forexample,EmployeereferencesString,whichthe©1996-2003jGuru.com.AllRightsReserved.JavaObjectModel-1JavaObjectModelcompilerfindsinfileString.classstoredinthestandardJavaclasslibrary.VariableDefinitionsVariabledefinitionsareoftheform:mo
6、difiersTypeNamename;wheremodifiersincludeaccessspecifiers(discussedinasectionbelow)andstaticifthevariableisaclassvariable.YoumayalsoinitializeJavavariablestoanyconstantornonconstantobject(exceptforwardreferencestomembers,becausetheywillnotyethavebeeninitialized)://JavacodeclassT{inti=0;//instanceva
7、riableinitializationstaticintversion=3;//classvariableinitStringid="x432";//initializeobjectvariable}TheJavalanguagehasastaticblockinitializerforinitializingsuchthingsasstaticarraysorfordetermininginitializ