Java programming ideas --- Chapter VI Access Control (on)

Chapter VI  Access Control (on)

  The initial implementation of access control and is not appropriate related.

  Class library developers must have permission to modify the code and improvements, and ensuring that the client code will not be affected by these changes. This goal can be achieved by agreement, but if the program developer wants to achieve an old and to add new implementations, any changes are likely to undermine a member of the client programmer's code, so library developers will assisted hands and feet, can not make changes to anything.

  To solve this problem, the Java provides access qualifier, for specifying which are available to the client programmer developer library, which is not available. Access control levels, from largest to smallest privileges were: public , protected , package access (no keyword), Private .

  However, the concept of member libraries as well as for those who have the right to access to the library component control problems are still imperfect, in which there is still the question of how the components bundled into a cohesive class library class library unit. For this, the Java keyword package to control, and access to the qualifiers due to a class exists in the same package, still exists in a separate package affected.

 

6.1 package: the library unit

 

  The package includes a set of classes that are organized together under a single name space.

  For example, in Java there is a tool library of standard release, which is organized in java.util under the name space. java.util has called ArrayList class using ArrayList One way is to use its full name java.util.ArrayList specified.

public class FullQualification {

public static void main(String[] args) {
  java.util.ArrayList list = new java.util.ArrayList();
  }
}

 

  This immediately let the program become very lengthy, so we use the import keyword:

import java.util.ArrayList;

public class SingleImport {
  public static void main(String[] args) {
    ArrayList list = new ArrayList();
  }
}

 

  Now you can not limit the use of ArrayList , and in order to import all the classes in which only need to use the " * ":

import java.util. *;

 

  The reason why we want to import is to provide a mechanism to manage name, the names of all class members are isolated from each other, A class method f () and B class have the same parameter list of the method f () will not each other conflict. But the class name conflict how to do? In Java full control of the name space and create a unique identifier for each class combination has become a very important thing.

  When writing a Java the source code file, this file is typically referred to as coding units (translation unit), each compilation unit must have a suffix .java , and can have a compilation unit in the public class, class name must be the same as the name of the file (including capitalization). Each compilation unit can only have one public class, or the compiler will not accept.

 

6.1.1 Code Organization

 

  When compiling a .java file, in .java each class will have a file in the output file, and the name of the output file with the .java file name of each class is the same, but with an extension .class , Therefore, a small amount at compile .java after the file will get a lot of .class files. Java when a group can run the program can be packaged and compressed into a Java document file ( JAR ) of .class files, Java interpreter is responsible for finding these files, and installed in the interpretation.

  Library is actually a set of class files, each file has a public class, and any number of non- public classes, each file has a member, if desired these members belonging to a group, the key can be used word Package Penalty for .

  Use package statement, it must be a file code comments in addition to the first sentence of the accident, wrote in the beginning of the file:

package access;

 

  In a statement to show that you are the compilation unit is called access part of the library. Assuming that the file name is MyClass.java , it means that there is in the file and only one public class name of the class must be MyClass :

package access.mypackage;

public class MyClass{

  //...

}

 

  As a library designer to keep in mind, Package Penalty for and import keywords allow you to do a zombie single global name space separated, so that no matter how many people use the Internet and Java start writing classes, the name will not appear in the issue of conflict .

 

6.1.2 Creating unique package names

 

  Since the system never really a Buddha which will be packaged packages packed into a single file, and a package can be composed of many .class configuration file, then the situation is a bit complicated. To avoid this from happening, a logical method is to package all the specific .class files in a directory, it is to use a hierarchical file structure of the operating system to solve this problem.

  All files in a subdirectory of income can solve two other problems: how to create a unique name? How to find there may be hidden somewhere in the directory structure of the class. These tasks are by .class coded path where the files into the package name to achieve.

  Java is running interpreter as follows:

  First, find the environment variable the CLASSPATH , the CLASSPATH can be set by the operating system, includes one or more directories used to find .class root directory of the file. Start from the root directory, the name of the interpreter acquires the package and replace each dot to a backslash, from CLASSPATH generating a path name of the root, the path will be obtained with the CLASSPATH connected to respective different items, the interpreter in these directories to find the name associated with the class that you want to create a .class file.

 

  conflict:

  If the two libraries containing the same name as "*" at the same time introduced in the form:

import net.mindview.simple.*;

import java.util.*;

 

  由于两个类都含有Vector类,这样就存在潜在的冲突。如果现在要创建一个Vector类的话就会产生冲突:

Vector v = new Vector();

 

  这到底是取用那个Vector类呢?编译器不知道,于是编译器就会提出错误信息,强制你明确指明:

java.util.Vector v = new java.util.Vector();

 

  由于这样可以完全指明该Vector类的位置,所以除非还要使用import java.util中的其他东西,否则没有必要写import java.util.*语句了。

 

6.1.3 定制工具库

 

  现在我们可以创建自己的工具库,例如我们已经用到的System.out.println()的别名可以减少输出负担,这种机制可以用于名为Print的类中,这样我们在使用该类的时候可以用一个更具可读性的静态import语句来导入:

package com.example.demo;

import java.io.*;

public class Print {
    public static void print(Object obj) {
        System.out.println(obj);
    }

    public static void print() {
        System.out.println();
    }

    public static void printnb(Object obj) {
        System.out.print(obj);
    }

    public static PrintStream print(String format, Object... args) {
        return System.out.printf(format, args);
    }
}

 

  现在,你可以创建有空的工具,并将其添加到自己的类库中。

 

6.1.4 import改变行为

 

  Java没有C的条件编译功能,该功能可以使你不必更改任何程序代码,就能切换开关并产生不同的行为。然而条件编译还有其他一些有价值的用途,例如说调试。调试功能在开发过程中是开启的,而在发布的产品中是禁用的。

 

6.1.5 对使用包的忠告

 

  无论何时创建包,都已经在给定包名的时候隐含地指定了目录结构,这个包必须位于其名称所指定的目录之中,而该目录必须是在以CLASSPATH开始的目录中可以查询到的。

 

Guess you like

Origin www.cnblogs.com/parable/p/11448640.html