Java Programming Ideas – learning notes (i)
Access control (or hiding a specific implementation) is related to the "initial implementation is inappropriate".
1. Package: library Unit
The reason we want to import a package is to provide a spatial mechanism for managing the name.
Each Java file can have only one public class. Other private classes provide support for the primary public class.
1.1 Code organization
Unlike a compiled language, a Java runnable program is a set of. class files that can be packaged and compressed into Java document files (jars, jar document generators using Java).
Use the package and import keywords so that there is no name conflict issue.
Note: The naming conventions for Java packages all use lowercase letters, including intermediate words.
1.2 Create a unique package life
The uniqueness of the package name is the reverse order of the creator of the class is the domain name of the Internet, and the second is that the package name is decomposed into a directory on your machine.
The Java interpreter operates as follows:
1) Find the environment variable Classpath
2) by adding the package name to the "." Change to "/" to generate a path name from the Classpath root.
3) The interpreter finds the ". Class" file that corresponds to the package name according to the path.
Classpath contains one or more directories that are used to locate the root directory of the. class. So the Java interpreter looks for a path that contains two parts, the first part is a directory under Classpath, and the second part is a path transformed by the package name.
Attention:
1) When using a jar file, the classpath contains the actual name of the jar file.
2) When importing a class library through "*", there may be a conflict when using two libraries that contain the same name class. The workaround is to write all of its packages before the class being used, or to use a single class import form.
1.3 Custom Tools Library
Create your own library of tools to reduce and eliminate duplicate program code.
code example:
Print.java
PackageCom.study.until;ImportJava.io.PrintStream;/** * * @author wom Print method so can be used without * / Public class Print { //Print with a newline Public Static void Print(Object obj) {System.out.println (obj); }//Print only a newline Public Static void Print() {System.out.println (); }//Print without a newline Public Static void PRINTNB(Object obj) {System.out.print (obj); }//printf () from Java SE5 Public StaticPrintStreamPrint(String format, Object ... args) {returnSystem.out.printf (format, args); }}
printtest.javapackage Com.study.test; import static com.study.until.print.*; public class printtest { public static void main (string[] args) {Print (11111 ); Print ( "what!" ); Print (); PRINTNB ( "What is you doing?" ); PRINTNB ( "where is you!" ); }}
Attention:
1) JDK1.5, the String class has a new static method, String.Format ().
2) Object ... args refers to variable parameters. That is, you can write multiple arguments when called, which is equivalent to
public static PrintStream print (String format, Object obj) {
return System.out.printf (format, obj);
}
When used:
Print (Format,object (obj1,obj2,obj3, ...))
3) The import statement when the static method is referenced.
1.4 Changing behavior with import
You can implement the conditional code by modifying the method of the imported package.
"Java Programming Idea-learning note (i)" Access control-Package