《JAVA编程思想》学习备忘(第209页:Access Control)-1

Access control(or implementation hiding) is about"not getting it right the first time."
存取控制: 
All good writers-including those who write software-know that a piece of work isn't good until it's been rewritten,often many times.If you leave a piece of code in a drawer for a while and come back to it,you may see a much better way to do it.
以上为“重构”的最初动机。
There is a tension,however,in this desire to change and improve your code.There are often consumers(client programmers)who rely on some aspect of your code staying the same.So you want to change it;they want it to stay the same.Thus a primary consideration in object-oriented design is to "separate the things that change from the things that stay the same."
对“库”来说,这特别重要。Consumers of that library must rely on the part they use,and know that they won't need to rewrite code if a new version of the library comes out.On the flip side,the library creator must have the freedom to make modifications and improvements with the certainty that the client code won't be affected by those changes.
This can be achieved through convention.For example,the library programmer must agree not to remove existing methods when modifying a class in the library,since that would break the client programmer's code.
...Thus the library creator is in a strait jacket and can't change anything.
To solve this problem,Java provides "access specifiers" to allow the library creator to say what is available to the client programmer and what is not.
从高到低的存取等级控制为:public,protected,package access(无关键字),private.
So to begin this chapter,you'll learn how library components are placed into packages.Then you'll be able to understand the complete meaning of the access specifiers.
 
package:the library unit
关于“包”的应用知识:
A package contains a group of classes,organized together under a single namespace.
 
For example,there's a utility library that's part of the standard Java distribution,organized under the namespace java.util.One of the classes in java.util is called ArrayList.One way to use an ArrayList is to specify the full name java.util.ArrayList.
关于两种导入包的方法:
例一:
//:access/FullQualification.java
public class FullQualification{
    public static void main(String[] args){
        java.util.ArrayList list = new java.util.ArrayList();
    }
}
例二
//:access/SingleImprt.java
import java.util.ArrayList;
 
public class SingleImport{
    public static void main(String[] args){
        ArrayList list = new java.util.ArrayList();
    }
}
使用“*”可导入包内的所有类:
import java.util.*;
......
if you're planning to create libraries that are friendly to other Java programs on the same machine,you must think about preventing class name clashes.
 
Code organization
When you compile a .java file,you get an output file for each class in the .java file.Each output file has the name of a class in the .java file,but with an extension of .class.Thus you can end up with quite a few .class files from a small number of .java files.If you've programmed with a compiled language,you might be used to the compiler spitting out an intermediate form(usually an "obj"file)that is then packaged together with others of its kind using a linker(to create an executable file)or a librarian(to create a library).That's not how Java works.A working program is a bunch of .class files,which can be packaged and compressed into a Java ARchive(JAR)file(using Java's jar archiver).The Java interpreter is responsible for finding,loading,and interpreting these files.
 
A library is a group of these class 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(每个原文件只有一个public类).If you want to say that all these components(each in its own separate .java and .class files)belong together,that's where the package keyword comes in.
 
If you use a package statement,it must appear as the first non-comment in the file.When you say:
package access;
you're stating that this compilation unit is part of a library named access.Put another way,you're saying that the public class name within this compilation unit is under the umbrella of the name access,and anyone who wants to use that name must either fully specify the name or use the import(Note that the convention for Java package names is to use all lowercase letters,even for intermediate words.)
 
For example,suppose the name of the file is MyClass.java.This means there can be one and only one public class in that file,and the name of that class must be MyClass(including the capitalization):
//:access/MyClass.java
package access.mypackage;
public class MyClass{
// ...
}
 
Now,if someone wants to use MyClass or, for that matter,any of the other public classes in access,they must use the import keyword to make the name or names in access available.The alternative is to give the fully qualified name:
如不使用import关键字,替代的方法是:
//:access/QualifiedMyClass.java
public class QualifiedMyClass{
    public static void main(String[] args){
        access.mypackage.MyClass m = new access.mypackage.MyClass();
    }
}
 
The import keyword can make this much cleaner:
//:access/ImportedMyClass.java
import access.mypackage.*;
public class ImportedMyClass{
    public static void main(String[] args){
        MyClass m  = new MyClass();
    }
}
It's worth keeping in mind that what the package and import keywords allow you to do,as a library designer,is to divide up the single global namespace so you won't have clashing names,no matter how many people get on the Internet and start writing classes in Java.
 
Creating unique package names
the first part of the package name is the reversed internet domain name of the creator of the class(使用域名做包名,以确保唯一性).
......
The Java interpreter proceeds as follows.First,it finds the environment variable CLASSPATH.CLASSPATH contains one or more directories that are used as roots in a search for .class files.Starting at that root,the intepreter will take the package name and replace each dot with a slash to generate a path name off of the CLASSPATH root(so package foo.bar.baz becomes foo/bar/baz or possibly something else,depending on your operating system).
......
the entire package name is lowercase.
......
应用JAR文件:
There's a variation when using JAR files,however.You must put the actual name of the JAR file in the classpath,not just the path where it's located.So for a JAR named grape.jar your classpath would include:
CLASSPATH=.;D:/JAVA/LIB;C:/flavors/grape.jar
(本小节其它内容暂略)
 
A custom tool library
With this knowledge,you can now create your own libraries of tools to reduce or eliminate(消除) duplicate code.Consider,for example,the alias we've been using for System.out.println(),to reduce typing.This can be part of a class called Print so that you end up with a readable static import:
//:net/mindview/util/Print.java
//Print methods that can be used without
//qualifiers,using Java SE5 static imports:
package net.mindview.util;
import java.io.*;
public class Print{
    //Print with a newline:
    public static void print(Object obj){
        System.out.println(obj);
    }
    //Print a newline by itself:
    public static void print(){
        System.out.println();
    }
    //Print with no line break:
    public static void printnb(Object obj){
        System.out.print(obj);
    }
    //The new Java SE5 printf()(from C):
    public static PrintStream printf(String format,Object... args){
        return System.out.printf(format, args);
    }
}
You can use the printing shorthand to print anything,either with a newline(print()) or without a newline(printnb()).
You can guess that the location of this file must be in a directory that starts at one of the CLASSPATH locations,then continues into net/mindview.After compiling,the static print() and printnb() methods can be used anywhere on your system with an import static statement:
//:access/PrintTest.java
//Uses the static printing methods in Print.java.
import static net.mindview.util.Print.*;
public class PrintTest{
    public static void main(String[] args){
        print("Available from new on!");
        print(100);
        print(100L);
        print(3.14159);
    }
}
输出结果:
Available from new on!
100
100
3.14159
 
A second component of this library can be the range() methods,introduced in the Controlling Execution chapter,that allow the use of the foreach syntax for simple integer sequences:(参见上边提到的章节学习笔记)
 
(代续)
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值