Touch the package and document in Java

zhaozj2021-02-16  96

What is the package in Java (package) is actually a directory, which is to better manage the Java class (Class) and interfaces (Interface). The java language package can be used by another Java development package. If we want to reference the classes in a package, use the Import keyword to indicate it. For example: import java.util.date = new date 

Tip: Import Java.util. indicates that all public classes and interfaces in java.util are introduced to the current package. The  match here can be transferred into multiple class names. Commonly used Java standard package JDK provides us with a lot of standard Java classes and interfaces, which are necessary to write Java programs, know that the classes and interfaces included in each package, and familiar with these classes and interfaces are each Java The basic skills of programmers should master. Java usually used in Java.applet, java.awt, java.i, java.lang, java.net, and java.util, etc. The Java.applet contains some classes and interfaces of the design mini application (Applet); Java.aw is a window tool luggage (AWT Abstract Window Toolkit), which is some GUI interface-related classes; Java.IO package support input Output, such as file input streaming: fileInputstream, etc.; java.lang contains classes related to thread, exception, system, integer, etc., is a package that is loaded in the Java program; java.net supports TCP / IP network protocol, and The class containing the socket class and the URL is the use of the network programming; java.util contains the utility classes of some programs, such as DateDictionary, and more. In addition to some of the standard packs mentioned above, there are still many other packages, such as database programming, we may need to use Java.SQL package, write network programs and use Java.RMI packages (RMI  Remote Method Invocation, etc. . In addition, Javax.  is extension of some standard packs, common packages are: javax.swing, javax.sound. The package you have to create a package Java is you can create yourself. We can put a lot of features similar to the same features in the same package to facilitate management and use. Basic ways to create a package 1. Define the public class; 2. First sentence plus "package"; 3. Place the Class generated by the Java file is placed in the directory named directory name. 4. In other programs " IMPORT package name You can access all public classes in this package. Creation and use of packets

// filename  B.JavaPackage com.chen.test // Define a package public class b // Define a class public void add int jma   // two numbers and outputs System. Out.printlni J  

The above code is to create a simple package: com.chen.test, we name this file B.java. Run Javac -D C / B.java compile this package (this command generates a COM / CHEN / TEST directory under the C drive, and saves the compile results in B.Class). Now we entered the C / COM / CHEN / TEST directory to see compilation and generated Class files are also included.

OK, now this package has been created, then you need you to do it in your environment variable ClassPath in your environment variable.

How to call a package

Now let's use a small program to call the package created above:

// filename a.javaimport com.chen.test. // Introduced the packet of the original created PUBLIC CLASSTRING = New Bb Test = New B Test.add 6 8   // Call custom package with ADD 

Save the top Java code as c / a.java, then compile: Javac C / A.java.

Tip: For package files B.java, you can only put it in the C / COM / CHEN / TEST directory. It should be noted that A. Java and B.java two files cannot be placed in the same directory so that it will be wrong.

Java documentation and javadoc

In addition to the bag, Java document is also a very important concept in Java programming. The Java document is the help document for the Java program. In general, let's write a help documentation and use tool javadoc to generate a corresponding document. Javadoc is a document generating tool provided by JDK. It uses Java Compile Javac to grammarize the declaration and document comments in the program code source file, and generate a set of HTML documents by default to describe classes, internal classes, interfaces, Construction functions, methods and domains. Javadoc comments start with "", end with " /", can contain normal text, HTML tag, and Javadoc tags. Javadoc only handles the comments before the class / interface definition, method, domain, and constructors before the source file, ignore the comments of other places.

Master Javadoc

Now let's use a routine to illustrate the meaning of the Javadoc mark:

/  My Javadoc test procedure - javadoctest  @ Author Warton2003 Xi'an
test < - here can embed HTML  this sentence is html Note  ->  @ Version 0.1 2004/01/01  / public class javadoctest /  in main   Use string @ See # mainjava.lang.string   / static string sdisplay /  Show javadoc test @ param args command line parameters @ Return no return value  / public static void main String args  sdisplay = "javadoc test" system.out.println sdisplay  

In this Java program, a large number of Javadoc tags, text, and HTML tags are used. Javadoc marks start with "/ ", indicating that these are comments for Java code. The mark started by "@" is JavaDoc tag.

The Javadoc marks have two: Standalone tags and inline tags. Independent markers are those common tags, such as @ param, @ return, and @Author, etc.; another type of tag is the inline mark  This tag is used in the form of  @ tag  it allows developers to create hyperlinks The comment section is linked to other Javadoc pages or elsewhere on this page.

Tip: Using Javadoc tags correctly in the Java source program is a very necessary annotation habit, will help Javadoc automatically generate a complete format API document with source code files.

OK, then let's take a look at how Javadoc generates a beautiful document! Save the above code to JavadOCtest.java and compile. Operation: javadoc -private -d doc -auth

Or -Version JavadoCtest.java

At this time, the Javadoc program generates a doc directory based on Javadoctest.java, there are many HTML files, which are the API help document generated by Javadoc.

Javadoc command line syntax

The light will not use the functions provided by Javadoc, and it must be used with its command line. Finally, introduce you to the simple javadoc command.

Javadoc's command line syntax is as follows:

Javadoc Options PackageNames SourceFiles @files 

Tip: For general applications, we only need to use the command lines such as Javadoc Yourjavafile.java to help documents. If you need to customize the help document, you will use the parameters. In general, you can use the Javadoc Help command to view Javadoc's help documentation.

In short, Javadoc provides a full-specific API document function. In software project management and development, Javadoc can not only reduce the amount of document work at the time of development, but also improve efficiency, but also very beneficial to future modifications and maintenance.

Some detailed descriptions of Javadoc tags:

@Author Specifies the "Author" item in the document, you can specify multiple authors. In the left code, an HTML tag is added behind the AUTHOR to link the URL.
is HTML tag, < ----> is HTML annotation.

@Version Specify version information.

@see tags are more complicated. @see "String" Adds a text item for "String" and does not generate any links. @see Label uses the HTML tag to generate a link. @see package.class # member label.

@Param tag used to describe parameters.

The @returN tag is used to describe the return value.

@LINK is the inline mark, the usual format of the inline marker is: @linkpackage.class#member label

Tip: Label is the text in the middle of the comment, and package.class # metore points to a specific class or method. Here are some examples:

Link to the properties of the same class, use: @ link # key

Link to the same class, use:  @ link # getcomponentatatt  GetComponentat

Link to other classes, use @ linkjava.util.map Map

转载请注明原文地址:https://www.9cbs.com/read-17044.html

New Post(0)