[Java programming ideology-learning notes (1)] Access Control-package, learning notes for programming ideas

Source: Internet
Author: User

[Java programming ideology-learning notes (1)] Access Control-package, learning notes for programming ideas

Java programming ideology-learning notes (1)
Access Control (or hiding specific implementations) is related to the "initial implementation is inappropriate.
1. Package: Library Unit
To import a package, we need to provide a space mechanism for managing the name.
Each java file can have only one public class. Other private classes are mainly supported by public classes.
1.1 code Organization
Different from compiled languages, java runnable programs are a set of. class files that can be packaged and compressed into java document files (JAR, using the Java jar document builder.
Use the package and import keywords to avoid name conflicts.
Note: All naming rules for java packages Use lowercase letters, including intermediate characters.
1.2 create a unique package
The unique feature of a package name is that the class creator has an Internet domain name in reverse order, and the package name is broken down into a directory on your machine.
The java interpreter operates as follows:
1) Find the environment variable CLASSPATH
2) Change "." of the package name to "/" to generate a path name from the CLASSPATH root.
3) The Interpreter finds the ". class" file corresponding to the package name based on the path.
CLASSPATH contains one or more directories used to find the root directory of. class. Therefore, the path searched by the java interpreter contains two parts: The first part is a directory under CLASSPATH, and the second part is the path transformed from the package name.
Note:
1) when a jar file is used, CLASSPATH contains the actual name of the jar file.
2) When using "*" to import a class library, two classes with the same name may conflict. The solution is to write the full package before the class to be used, or use a single class import form.
1.3 custom tool Library
Create your own tool library to reduce and eliminate repeated program code.
Code example:
Print. java

package com.study.until;import java.io.PrintStream;/** *  * @author wom Print method that 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 static PrintStream print(String format, Object... args) {        return System.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 are you doing?");        printnb(" where are you! ");    }}

Note:
1) In JDK1.5, the String class adds a static method String. format ().
2) Object... Args is a variable parameter. That is, you can write multiple parameters during the call, which is equivalent
Public static PrintStream print (String format, Object obj ){
Return System. out. printf (format, obj );
}
Use Time:
Print (format, Object (obj1, obj2, obj3 ,......))
3) write the import statement when the static method is referenced.
1.4 Use import to change behavior
Conditional code can be implemented by modifying the imported package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.