Thinking in Java——笔记(6)
Access Control
- A piece of work isn’t good until it’s been rewritten, often many times.
- Thus a primary consideration in object-oriented design is to “separate the things that change from the things that stay the same.”
- Java provides access specifiers to allow the library creator to say what is available to the client programmer and what is not.
- As a library designer, you’ll want to keep everything as “private” as possible, and expose only the methods that you want the client programmer to use.
- How the components are bundled together into a cohesive library unit.
- The access specifiers are affected by whether a class is in the same package or in a separate package.
package: the library unit
- The reason for all this importing is to provide a mechanism to manage namespaces.
- This potential clashing of names is why it’s important to have complete control over the namespaces in Java, and to create a unique identifier combination for each class.
- Inside the compilation unit there can be a public class that must have the same name as the file.
- There can be only one public class in each compilation unit.
- Additional classes in that compilation unit comprise “support” classes for the main public class.
Code organization
- A working program is a bunch of .class files, which can be packaged and compressed into a Java ARchive (JAR) file. The Java interpreter is responsible for finding, loading, and interpreting these files.
- Each source file usually has a public class and any number of non-public classes, so there’s one public component for each source file.
Creating unique package names
- A logical thing to do is to place all the .class files for a particular package into a single directory.
- By convention, the first part of the package name is the reversed Internet domain name of the creator of the class.
- The second part of this trick is resolving the package name into a directory on your machine.
- CLASSPATH contains one or more directories that are used as roots in a search for .class files.
- The package statement must be the first non-comment code in the file.
- You must put the actual name of the JAR file in the classpath, not just the path where it’s located.
Collisions
- You can use the single-class import form or fully specify the names to prevent clashes.
A custom tool library
- You can create your own libraries of tools to reduce or eliminate duplicate code.
Using imports to change behavior
- The debugging features are enabled during development and disabled in the shipping product.
- You can accomplish this by changing the package that’s imported in order to change the code used in your program from the debug version to the production version.
Package caveat
- Unless you adhere to the package-name to directory-path rule, you’ll get a lot of mysterious runtime messages about not being able to find a particular class, even if that class is sitting there in the same directory.
Java access specifiers
Package access
- The default access has no keyword, but it is commonly referred to as package access.
- It means that all the other classes in the current package have access to that member, but to all the classes outside of this package, the member appears to be private.
- All the classes within a single compilation unit are automatically available to each other via package access.
- Package access allows you to group related classes together in a package so that they can easily interact with each other.
- In many languages the way you organize your definitions in files can be arbitrary, but in Java you’re compelled to organize them in a sensible fashion.
- The only way to grant access to a member is to:
1.Make the member public
2.Give the member package access and put the other classes in the same package.
3.Inherited class can access a protected member as well as a public member.
4.Provide “accessor/mutator” methods.
public: interface access
- It means that the member declaration that immediately follows public is available to everyone.
- Don’t make the mistake of thinking that Java will always look at the current directory as one of the starting points for searching.
The default package
- The reason that they are available in Cake.java is because they are in the same directory and have no explicit package name.
- Java treats files like this as implicitly part of the “default package” for that directory, and thus they provide package access to all the other files in that directory.
private: you can’t touch that!
- It means that no one can access that member except the class that contains that member, inside methods of that class.
- private allows you to freely change that member without concern that it will affect another class in the same package.
- It turns out that the consistent use of private is very important, especially where multithreading is concerned.
- Making a method private guarantees that you retain this option.
- A reference to an object is private inside a class doesn’t mean that some other object can’t have a public reference to the same object.
protected: inheritance access
- If you create a new package and inherit from a class in another package, the only members you have access to are the public members of the original package.
- Sometimes the creator of the base class would like to take a particular member and grant access to derived classes but not the world in general.
Interface and implementation
- Access control is often referred to as implementation hiding.
- Access control puts boundaries within a data type for two important reasons: The first is to establish what the client programmers can and can’t use, the second is to separate the interface from the implementation.
- Displaying the interface to the consumer of a class is really the job of the class browser
Class access
- This controls whether the client programmer can even create an object of the class.
- There can be only one public class per compilation unit.It can have as many supporting package-access classes as you want.
- The name of the public class must exactly match the name of the file containing the compilation unit, including capitalization.
- It is possible, though not typical, to have a compilation unit with no public class at all.
- When you create a package-access class, it still makes sense to make the fields of the class private, but it’s generally reasonable to give the methods the same access as the class.
- Note that a class cannot be private or protected(an inner class can be private or protected, but that’s a special case).
- If you don’t want anyone else to have access to that class, you can make all the constructors private.
- If you don’t put an access specifier for class access, it defaults to package access. This means that an object of that class can be created by any other class in the package, but not outside the package.
- If a static member of that class is public, the client programmer can still access that static member even though they cannot create an object of that class.
Thinking in Java——笔记(6)的更多相关文章
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
- servlet(6) - servlet总结 - 小易Java笔记
垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...
- Java笔记 —— 继承
Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...
- Java笔记 —— 方法重载和方法重写
Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...
- Java笔记 —— 初始化
Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...
- Java笔记 —— this 关键字
Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...
- Java 笔记 —— java 和 javac
Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...
随机推荐
- R AnalyticFlow---R的流程图
0.简介 R AnalyticFlow是一款利用R环境作为统计计算的数据分析软件,创作者是日本人,版权属于日本Ef-prime公司.R AnalyticFlow除了拥有直观的用户界面和流程图显示,它还 ...
- 【原】iOS多线程之NSThread、NSOperationQueue、NSObject和GCD的区别
区别: Thread: 是这几种方式里面相对轻量级的,但也是使用起来最负责的,你需要自己管理thread的生命周期,线程之间的同步.线程共享同一应用程序的部分内存空间, 它们拥有对数据相同的访问权限. ...
- soapui中文操作手册(四)----MOCK服务
Web Service Mocking是武器库一个非常有用的工具.这是解决“如果没有Web服务如何创建针对性的Web服务测试”问题的办法.Web Service Mocking将在这里派上用场.它允许 ...
- 修改文档框架:word-多级列表与标题样式相结合
转自:http://blog.sina.com.cn/s/blog_6721f25c0100nuf0.html 设置标题的时候希望出现多标题并且自动编号的标题,如下1. XXXXXXXXXXX ...
- CSS3弹性盒模型flexbox布局基础版
原文链接:http://caibaojian.com/using-flexbox.html 最近看了社区上的一些关于flexbox的很多文章,感觉都没有我这篇文章实在,最重要的兼容性问题好多人都没有提 ...
- 转载:CSS计数器的趣味时光之css计算数据
CSS计数器是“啊太好了,竟不知道CSS可以做这啊”这类非常有趣的众多特性之一.简言之,用CSS使你持续某增加某个量,而无需JavaScript. 简单计数器 我们从这个简单的分页示例开始: 你见到的 ...
- [题解+总结]动态规划大合集II
1.前言 大合集总共14道题,出自江哥之手(这就没什么好戏了),做得让人花枝乱颤.虽说大部分是NOIP难度,也有简单的几道题目,但是还是做的很辛苦,有几道题几乎没思路,下面一道道边看边分析一下. 2. ...
- Introduction of SQLite
SQLite is a lightweight, server-less database, it's great for embedding into client application. It ...
- 360safe安全卫士防网站攻击源码
近段时间,公司网站老被攻击,于是研究起防止攻击方法,当然无外乎就是SQL注入之类的问题,无意间发现了一个360安全卫士提供的源码,觉得挺好的,咋们暂且不说防攻击效果,至少思路是很好的,奉献给大家,大家 ...
- TCP和UDP的聊天
TCP聊天 TCP(Transmission Control Protocol,传输控制协议)是基于连接的协议. 1.一个TCP连接必须要经过三次"对话"才能建立起来,其中的过程非 ...