《Java程序设计英文版课件:ch6 Access Control》由会员分享,可在线阅读,更多相关《Java程序设计英文版课件:ch6 Access Control(25页珍藏版)》请在金锄头文库上搜索。
1、1Chapter 6Access Control26.1Why Java support Access ControlMachine LanguageAssembly language36.1Why Java support Access Control“separatingthethingsthatchangefromthethingsthatstaythesame.”46.1Why Java support Access ControlAccess Control“separatingthethingsthatchangefromthethingsthatstaythesame.”56.1
2、Why Java support Access ControlPublicProtectedPrivatePackageimportAccess Control66.2 Package: the library unitImport Statement76.2 Package: the library unit.javaOnly one public classclassclass.class.class.classcompilation unitlpublic class: There can be one and there can be only one in each compilat
3、ion unit.lpublic class name: the same name as the file (including capitalization, but excluding the .java filename extension). ladditional classes: support main public class.86.2 Package: the library unita.javalibraryb.javac.javalIfyouuseapackagestatement,itmustappearasthefirstnoncommentinthefile.lN
4、ote that the convention for Java package names is to use all lowercase letters, even for intermediate words. 96.2 Package: the library MindV W106.2 Package: the library unitPackage net.mindview.simple netmindviewsimpleVector.class C:DOCJavaTnetmindviewsimpleVector.classList.class C:DOCJavaTnetmind
5、viewsimpleList.classJava interpreterCLASSPATH.class filedirectory116.2 Package: the library unit126.2 Package: the library unitCollisions:136.2 Package: the library unitA custom tool library:146.2 Package: the library unitPackage caveat:lIts worth remembering that anytime you create a package, you i
6、mplicitly specify a directory structure when you give the package a name. lThe package must live in the directory indicated by its name, which must be a directory that is searchable starting from the CLASSPATH. 156.3 Java access specifierslJavaaccessspecifiers:public,protected,andprivatelPlacedinfro
7、ntofeachdefinitionforeachmemberinyourclass,whetheritsafieldoramethod.lEachaccessspecifiercontrolstheaccessforonlythatparticulardefinition.166.3 Java access specifiersvPublic:Whenyouusethepublickeyword,itmeansthatthememberdeclarationthatimmediatelyfollowspublicisavailabletoeveryone.176.3 Java access
8、specifiersvPrivate: you cant touch that!The private keyword means that no one can access that member except that particular class, inside methods of that class.Any method that youre certain is only a “helper” method for that class can be made private.186.3 Java access specifiersvProtected:The protec
9、ted keyword deals with a concept called inheritance,which takes an existing class which we refer to as the base classand adds new members to that class without touching the existing class. 196.3 Java access specifiersv“Friendly”:Thedefaultaccesshasnokeyword,butitiscommonlyreferredtoas“friendly.”Itme
10、ansthatalltheotherclassesinthecurrentpackagehaveaccesstothefriendlymember,buttoalltheclassesoutsideofthispackagethememberappearstobeprivate.206.3 Java access specifiersvDefault Package:216.3 Java access specifiersvAccess specifiers:Inside class Inside packageChildOtherprivate protected public friend
11、ly 226.4 Interface and implementationvAccess control is often referred to as implementation hiding. Wrapping data and methods within classes in combination with implementation hiding is often called encapsulation. The result is a data type with characteristics and behaviors.236.5 Class AccessClass A
12、ccess: “friendly” or public.A class cannot be private (that would make it accessible to no one but the class), or protected.There can be only one public class per compilation unit (file).The name of the public class must exactly match the name of the file containing the compilation unit, including c
13、apitalization.If there is no public class, file name can be arbitrarily.246.6 ExamplevExercise 5: (2) Create a class with public, private, protected, and package-access fields and method members. Create an object of this class and see what kind of compiler messages you get when you try to access all the class members. Be aware that classes in the same directory are part of the “default” package.25 SummaryvWhy Java support Access Control?vPackage: the library unitvJava access specifiersvInterface and implementationvClass AccessvExample