返回列表 发帖

How to Write Doc Comments

How to Write Doc Comments for the JavadocTM Tool Javadoc Home Page This document describes the style guide, tag and image conventions we use in documentation comments for Java programs written at Java Software, Sun Microsystems. It does not rehash related material covered elsewhere: For reference material on JavadocTM tags, see the Javadoc reference pages. For the required semantic content of documentation comments, see Requirements for Writing Java API Specifications. Contents Introduction Principles Terminology Source Files Writing Doc Comments Format of a Doc Comment Doc Comment Checking Tool Descriptions A Style Guide Tag Conventions (@tag) Documenting Default Constructors http://java.sun.com/j2se/javadoc/writingdoccomments/index.html#throwstag Package-Level Comments Documenting Anonymous Inner Classes Including Images Examples of Doc Comments Troubleshooting Curly Quotes (Microsoft Word) -------------------------------------------------------------------------------- Introduction Principles At Java Software, we have several guidelines that might make our documentation comments different than those of third party developers. Our documentation comments define the official Java Platform API Specification. To this end, our target audience is those who write Java compatibility tests, or conform or re-implement the Java platform, in addition to developers. We spend time and effort focused on specifying boundary conditions, argument ranges and corner cases rather than defining common programming terms, writing conceptual overviews, and including examples for developers. Thus, there are commonly two different ways to write doc comments -- as API specifications, or as programming guide documentation. These two targets are described in the following sections. A staff with generous resources can afford to blend both into the same documentation (properly "chunked"); however, our priorities dictate that we give prime focus to writing API specifications in doc comments. This is why developers often need to turn to other documents, such as the J2SE Documentation, Java Tutorial or the Java Class Libraries (in hardback only) for programming guides. Writing API Specifications Ideally, the Java API Specification comprises all assertions required to do a clean-room implementation of the Java Platform for "write once, run anywhere" -- such that any Java applet or application will run the same on any implementation. This may include assertions in the doc comments plus those in any architectural and functional specifications (usually written in FrameMaker) or in any other document. This definition is a lofty goal and there is some practical limitation to how fully we can specify the API. The following are guiding principles we try to follow: The Java Platform API Specification is defined by the documentation comments in the source code and any documents marked as specifications reachable from those comments. Notice that the specification does not need to be entirely contained in doc comments. In particular, specifications that are lengthy are sometimes best formatted in a separate file and linked to from a doc comment. The Java Platform API Specification is a contract between callers and implementations. The Specification describes all aspects of the behavior of each method on which a caller can rely. It does not describe implementation details, such as whether the method is native or synchronized. The specification should describe (textually) the thread-safety guarantees provided by a given object. In the absence of explicit indication to the contrary, all objects are assumed to be "thread-safe" (i.e., it is permissible for multiple threads to access them concurrently). It is recognized that current specifications don';t always live up to this ideal. Unless otherwise noted, the Java API Specification assertions need to be implementation-independent. Exceptions must be set apart and prominently marked as such. We have guidelines for how to prominently document implementation differences. The Java API Specification should contain assertions sufficient to enable Software Quality Assurance to write complete Java Compatibility Kit (JCK) tests. This means that the doc comments must satisfy the needs of the conformance testing by SQA. The comments should not document bugs or how an implementation that is currently out of spec happens to work. Writing Programming Guide Documentation What separates API specifications from a programming guide are examples, definitions of common programming terms, certain conceptual overviews (such as metaphors), and descriptions of implementation bugs and workarounds. There is no dispute that these contribute to a developer';s understanding and help a developer write reliable applications more quickly. However, because these do not contain API "assertions", they are not necessary in an API specification. You can include any or all of this information in documentation comments (and can include custom tags, handled by a custom doclet, to facilitate it). At Java Software, we consciously do not include this level of documentation in doc comments, and instead include either links to this information (links to the Java Tutorial and list of changes) or include this information in the same documentation download bundle as the API spec -- the JDK documentation bundle includes the API specs as well as demos, examples, and programming guides. It';s useful to go into further detail about how to document bugs and workarounds. There is sometimes a discrepancy between how code should work and how it actually works. This can take two different forms: API spec bugs and code bugs. It';s useful to decide up front whether you want to document these in the doc comments. At Java Software we have decided to document both of these outside of doc comments, though we do make exceptions. API spec bugs are bugs that are present in the method declaration or in the doc comment that affects the syntax or semantics. An example of such a spec bug is a method that is specified to throw a NullPointerException when null is passed in, but null is actually a useful parameter that should be accepted (and was even implemented that way). If a decision is made to correct the API specification, it would be useful to state that either in the API specification itself, or in a list of changes to the spec, or both. Documenting an API difference like this in a doc comment, along with its workaround, alerts a developer to the change where they are most likely to see it. Note that an API specification with this correction would still maintain its implementation-independence. Code bugs are bugs in the implementation rather than in the API specification. Code bugs and their workarounds are often likewise distributed separately in a bug report. However, if the Javadoc tool is being used to generate documentation for a particular implementation, it would be quite useful to include this information in the doc comments, suitably separated as a note or by a custom tag (say @bug). Who "Owns" the Doc Comments - Writers or Programmers Here at Java Software, the doc comments are not owned exclusively by writers or programmers, but their ownership is shared between them. It is a basic premise that writers and programmers honor each other';s capabilities and both contribute to the best doc comments possible. Often it is a matter of negotiation to determine who writes which parts of the documentation, based on knowledge, time, resources, interest, API complexity, and on the state of the implementation itself. Ideally, the person designing the API would write the API specification in skeleton source files, with only declarations and doc comments, filling in the implementation only to satisfy the written API contract. The purpose of an API writer is to relieve the designer from some of this work. In this case, the API designer would write the initial doc comments using sparse language, and then the writer would review the comments, refine the content, and add tags. If the doc comments are an API specification for re-implementors, and not simply a guide for developers, they should be written either by the programmer who designed and implemented the API, or by a API writer who is or has become a subject matter expert. If the implementation is written to spec but the doc comments are unfinished, a writer can complete the doc comments by inspecting the source code or writing programs that test the API. A writer might inspect or test for exceptions thrown, parameter boundary conditions, and for acceptance of null arguments. However, a much more difficult situation arises if the implementation is not written to spec. Then a writer can proceed to write an API specification only if they either know the intent of the designer (either through design meetings or through a separately-written design specification) or have ready access to the designer with their questions. Thus, it may be more difficult for a writer to write the documentation for interfaces and abstract classes that have no implementors. With that in mind, these guidelines are intended to describe the finished documentation comments. They are intended as suggestions rather than requirements to be slavishly followed if they seem overly burdensome, or if creative alternatives can be found. When a complex system such as Java (which contains about 60 packages) is being developed, often a group of engineers contributing to a particular set of packages, such as javax.swing may develop guidelines that are different from other groups. This may be due to the differing requirements of those packages, or because of resource constraints. Terminology API documentation (API docs) or API specifications (API specs) On-line or hardcopy descriptions of the API, intended primarily for programmers writing in Java. These can be generated using the Javadoc tool or created some other way. An API specification is a particular kind of API document, as described above. An example of an API specification is the on-line Java 2 Platform, Standard Edition API Specifications. An example of an API document is the Java Class Libraries book by Chan/Lee. Documentation comments (doc comments) The special comments in the Java source code that are delimited by the /** ... */ delimiters. These comments are processed by the Javadoc tool to generate the API docs. javadoc The JDK tool that generates API documentation from documentation comments. Source Files The Javadoc tool can generate output originating from four different types of "source" files: Source code files for Java classes (.java) - these contain class, interface, field, constructor and method comments. Package comment files - these contain package comments Overview comment files - these contain comments about the set of packages Miscellaneous unprocessed files - these include images, sample source code, class files, applets, HTML files, and whatever else you might want to reference from the previous files. For more details, see: Source Files. -------------------------------------------------------------------------------- Writing Doc Comments Format of a Doc Comment A doc comment is written in HTML and must precede a class, field, constructor or method declaration. It is made up of two parts -- a description followed by block tags. In this example, the block tags are @param, @return, and @see. Example /** * Returns an Image object that can then be painted on the screen. * The url argument must specify an absolute http://java.sun.com/j2se/javadoc/writingdoccomments/index.html#{@link}. The name * argument is a specifier that is relative to the url argument. *

* This method always returns immediately, whether or not the * image exists. When this applet attempts to draw the image on * the screen, the data will be loaded. The graphics primitives * that draw the image will incrementally paint on the screen. * * @param url an absolute URL giving the base location of the image * name the location of the image, relative to the url argument * @return the image at the specified URL * @see Image */ public Image getImage(URL url, String name) { try { return getImage(new URL(url, name)); } catch (MalformedURLException e) { return null; } } Notes: The resulting HTML from running Javadoc is shown below Each line above is indented to align with the code below the comment. The first line contains the begin-comment delimiter (/**). Starting with Javadoc 1.4, the leading asterisks are optional. Write the first sentence as a short summary of the method, as Javadoc automatically places it in the method summary table (and index). Notice the inline tag {@link URL}, which converts to an HTML hyperlink pointing to the documentation for the URL class. This inline tag can be used anywhere that a comment can be written, such as in the text following block tags. If you have more than one paragraph in the doc comment, separate the paragraphs with a

paragraph tag, as shown. Insert a blank comment line between the description and the list of tags, as shown. The first line that begins with an "@" character ends the description. There is only one description block per doc comment; you cannot continue the description following block tags. The last line contains the end-comment delimiter (*/) Note that unlike the begin-comment delimiter, the end-comment contains only a single asterisk. For more examples, see Simple Examples. So lines won';t wrap, limit any doc-comment lines to 80 characters. Here is what the previous example would look like after running the Javadoc tool: getImage public Image getImage(URL url, String name) Returns an Image object that can then be painted on the screen. The url argument must specify an absolute URL. The name argument is a specifier that is relative to the url argument. This method always returns immediately, whether or not the image exists. When this applet attempts to draw the image on the screen, the data will be loaded. The graphics primitives that draw the image will incrementally paint on the screen. Parameters: url - an absolute URL giving the base location of the image name - the location of the image, relative to the url argument Returns: the image at the specified URL See Also: Image Also see Troubleshooting Curly Quotes (Microsoft Word) at the end of this document. Doc Comment Checking Tool At Sun, we have developed a tool for checking doc comments, called the Sun Doc Check Doclet, or DocCheck. You run it on source code and it generates a report describing what style and tag errors the comments have, and recommends changes. We have tried to make its rules conform to the rules in this document. DocCheck is a Javadoc doclet, or "plug-in", and so requires that the Javadoc tool be installed (as part of the Java 2 Standard Edition SDK). Descriptions First Sentence The first sentence of each doc comment should be a summary sentence, containing a concise but complete description of the API item. This means the first sentence of each member, class, interface or package description. The Javadoc tool copies this first sentence to the appropriate member, class/interface or package summary. This makes it important to write crisp and informative initial sentences that can stand on their own. This sentence ends at the first period that is followed by a blank, tab, or line terminator, or at the first tag (as defined below). For example, this first sentence ends at "Prof.": /** * This is a simulation of Prof. Knuth';s MIX computer. */ However, you can work around this by typing an HTML meta-character such as "&" or "<" immediately after the period, such as: /** * This is a simulation of Prof. Knuth';s MIX computer. */ or /** * This is a simulation of Prof. Knuth';s MIX computer. */ In particular, write summary sentences that distinguish overloaded methods from each other. For example: /** * Class constructor. */ foo() { ... /** * Class constructor specifying number of objects to create. */ foo(int n) { ... Implementation-Independence Write the description to be implementation-independent, but specifying such dependencies where necessary. This helps engineers write code to be "write once, run anywhere." As much as possible, write doc comments as an implementation-independent API specification. Define clearly what is required and what is allowed to vary across platforms/implementations. Ideally, make it complete enough for conforming implementors. Realistically, include enough description so that someone reading the source code can write a substantial suite of conformance tests. Basically, the spec should be complete, including boundary conditions, parameter ranges and corner cases. Where appropriate, mention what the specification leaves unspecified or allows to vary among implementations. If you must document implementation-specific behavior, please document it in a separate paragraph with a lead-in phrase that makes it clear it is implementation-specific. If the implementation varies according to platform, then specify "On " at the start of the paragraph. In other cases that might vary with implementations on a platform you might use the lead-in phrase "Implementation-Specific:". Here is an example of an implementation-dependent part of the specification for java.lang.Runtime: On Windows systems, the path search behavior of the loadLibrary method is identical to that of the Windows API';s LoadLibrary procedure. The use of "On Windows" at the beginning of the sentence makes it clear up front that this is an implementation note. Automatic re-use of method comments You can avoid re-typing doc comments by being aware of how the Javadoc tool duplicates (inherits) comments for methods that override or implement other methods. This occurs in three cases: When a method in a class overrides a method in a superclass When a method in an interface overrides a method in a superinterface When a method in a class implements a method in an interface In the first two cases, if a method m() overrides another method, The Javadoc tool will generate a subheading "Overrides" in the documentation for m(), with a link to the method it is overriding. In the third case, if a method m() in a given class implements a method in an interface, the Javadoc tool will generate a subheading "Specified by" in the documentation for m(), with a link to the method it is implementing. In all three of these cases, if the method m() contains no doc comments or tags, the Javadoc tool will also copy the text of the method it is overriding or implementing to the generated documentation for m(). So if the documentation of the overridden or implemented method is sufficient, you do not need to add documentation for m(). If you add any documentation comment or tag to m(), the "Overrides" or "Specified by" subheading and link will still appear, but no text will be copied. A Style Guide The following are useful tips and conventions for writing descriptions in doc comments. Use style for keywords and names. Keywords and names are offset by ... when mentioned in a description. This includes: Java keywords package names class names method names interface names field names argument names code examples Use in-line links economically You are encouraged to add links for API names (listed immediately above) using the {@link} tag. It is not necessary to add links for all API names in a doc comment. Because links call attention to themselves (by their color and underline in HTML, and by their length in source code doc comments), it can make the comments more difficult to read if used profusely. We therefore recommend adding a link to an API name if: The user might actually want to click on it for more information (in your judgment), and Only for the first occurrence of each API name in the doc comment (don';t bother repeating a link) Our audience is advanced (not novice) programmers, so it is generally not necessary to link to API in the java.lang package (such as String), or other API you feel would be well-known. Omit parentheses for the general form of methods and constructors When referring to a method or constructor that has multiple forms, and you mean to refer to a specific form, use parentheses and argument types. For example, ArrayList has two add methods: add(Object) and add(int, Object). The add(int, Object) method adds an item at a specified position in this arraylist. However, if referring to both forms of the method, omit the parentheses altogether. It is misleading to include empty parentheses, because that would imply a particular form of the method. The intent here is to distinguish the general method from any of its particular forms. Include the word "method" to distinguish it as a method and not a field. The add method enables you to insert items. (preferred) The add() method enables you to insert items. (avoid when you mean "all forms" of the add method) Okay to use phrases instead of complete sentences, in the interests of brevity. This holds especially in the initial summary and in @param tag descriptions. Use 3rd person (descriptive) not 2nd person (prescriptive). The description is in 3rd person declarative rather than 2nd person imperative. Gets the label. (preferred) Get the label. (avoid) Method descriptions begin with a verb phrase. A method implements an operation, so it usually starts with a verb phrase: Gets the label of this button. (preferred) This method gets the label of this button. (avoid) Class/interface/field descriptions can omit the subject and simply state the object. These API often describe things rather than actions or behaviors: A button label. (preferred) This field is a button label. (avoid) Use "this" instead of "the" when referring to an object created from the current class. For example, the description of the getToolkit method should read as follows: Gets the toolkit for this component. (preferred) Gets the toolkit for the component. (avoid) Add description beyond the API name. The best API names are "self-documenting", meaning they tell you basically what the API does. If the doc comment merely repeats the API name in sentence form, it is not providing more information. For example, if method description uses only the words that appear in the method name, then it is adding nothing at all to what you could infer. The ideal comment goes beyond those words and should always reward you with some bit of information that was not immediately obvious from the API name. Avoid - The description below says nothing beyond what you know from reading the method name. The words "set", "tool", "tip", and "text" are simply repeated in a sentence. /** * Sets the tool tip text. * * @param text the text of the tool tip */ public void setToolTipText(String text) { Preferred - This description more completely defines what a tool tip is, in the larger context of registering and being displayed in response to the cursor. /** * Registers the text to display in a tool tip. The text * displays when the cursor lingers over the component. * * @param text the string to display. If the text is null, * the tool tip is turned off for this component. */ public void setToolTipText(String text) { Be clear when using the term "field". Be aware that the word "field" has two meanings: static field, which is another term for "class variable" text field, as in the TextField class. Note that this kind of field might be restricted to holding dates, numbers or any text. Alternate names might be "date field" or "number field", as appropriate. Avoid Latin -- use "also known as" instead of "aka", use "that is" or "to be specific" instead of "i.e.", use "for example" instead of "e.g.", and use "in other words" or "namely" instead of "viz." Tag Conventions Most of the following tags are specified in the Java Language Specification, First Edition. Also see the Javadoc reference page. Order of Tags Include tags in the following order: * @author (classes and interfaces only, required) * @version (classes and interfaces only, required) (see footnote 1) * @param (methods and constructors only) * @return (methods only) * @exception (@throws is a synonym added in Javadoc 1.2) * @see * @since * @serial (or @serialField or @serialData) * @deprecated (see How and When To Deprecate APIs) Ordering Multiple Tags We employ the following conventions when a tag appears more than once in a documentation comment. If desired, groups of tags, such as multiple @see tags, can be separated from the other tags by a blank line with a single asterisk. Multiple @author tags should be listed in chronological order, with the creator of the class listed at the top. Multiple @param tags should be listed in argument-declaration order. This makes it easier to visually match the list to the declaration. Multiple @throws tags (also known as @exception) should be listed alphabetically by the exception names. Multiple @see tags should be ordered as follows, which is roughly the same order as their arguments are searched for by javadoc, basically from nearest to farthest access, from least-qualified to fully-qualified, The following list shows this progression. Notice the methods and constructors are in "telescoping" order, which means the "no arg" form first, then the "1 arg" form, then the "2 arg" form, and so forth. Where a second sorting key is needed, they could be listed either alphabetically or grouped logically. @see #field @see #Constructor(Type, Type...) @see #Constructor(Type id, Type id...) @see #method(Type, Type,...) @see #method(Type id, Type, id...) @see Class @see Class#field @see Class#Constructor(Type, Type...) @see Class#Constructor(Type id, Type id) @see Class#method(Type, Type,...) @see Class#method(Type id, Type id,...) @see package.Class @see package.Class#field @see package.Class#Constructor(Type, Type...) @see package.Class#Constructor(Type id, Type id) @see package.Class#method(Type, Type,...) @see package.Class#method(Type id, Type, id) @see package Required Tags An @param tag is required for every parameter, even when the description is obvious. The @return tag is required for every method that returns something other than void, even if it is redundant with the method description. (Whenever possible, find something non-redundant (ideally, more specific) to use for the tag comment.) These principles expedite automated searches and automated processing. Frequently, too, the effort to avoid redundancy pays off in extra clarity. Tag Comments As a reminder, the fundamental use of these tags is described on the Javadoc Reference page. Java Software generally uses the following additional guidelines to create comments for each tag: @author (reference page) You can provide one @author tag, multiple @author tags, or no @author tags. In these days of the community process when development of new APIs is an open, joint effort, the JSR can be consider the author for new packages at the package level. For example, the new package java.nio has "@author JSR-51 Expert Group" at the package level. Then individual programmers can be assigned to @author at the class level. As this tag can only be applied at the overview, package and class level, the tag applies only to those who make significant contributions to the design or implementation, and so would not ordinarily include technical writers. The @author tag is not critical, because it is not included when generating the API specification, and so it is seen only by those viewing the source code. (Version history can also be used for determining contributors for internal purposes.) If someone felt strongly they need to add @author at the member level, they could do so by running javadoc using the new 1.4 -tag option: -tag author:a:"Author:" If the author is unknown, use "unascribed" as the argument to @author. Also see http://java.sun.com/j2se/javadoc/writingdoccomments/index.html#multiple@author. @version (reference page) The Java Software convention for the argument to the @version tag is the SCCS string "%I%, %G%", which converts to something like "1.39, 02/28/97" (mm/dd/yy) when the file is checked out of SCCS. @param (reference page) The @param tag is followed by the name (not data type) of the parameter, followed by a description of the parameter. By convention, the first noun in the description is the data type of the parameter. (Articles like "a", "an", and "the" can precede the noun.) An exception is made for the primitive int, where the data type is usually omitted. Additional spaces can be inserted between the name and description so that the descriptions line up in a block. Dashes or other punctuation should not be inserted before the description, as the Javadoc tool inserts one dash. Parameter names are lowercase by convention. The data type starts with a lowercase letter to indicate an object rather than a class. The description begins with a lowercase letter if it is a phrase (contains no verb), or an uppercase letter if it is a sentence. End the phrase with a period only if another phrase or sentence follows it. Example: * @param ch the character to be tested * @param observer the image observer to be notified Do not bracket the name of the parameter after the @param tag with ... since Javadoc 1.2 and later automatically do this. (Beginning with 1.4, the name cannot contain any HTML, as Javadoc compares the @param name to the name that appears in the signature and emits a warning if there is any difference.) When writing the comments themselves, in general, start with a phrase and follow it with sentences if they are needed. When writing a phrase, do not capitalize and do not end with a period: @param x the x-coordinate, measured in pixels When writing a phrase followed by a sentence, do not capitalize the phrase, but end it with a period to distinguish it from the start of the next sentence: @param x the x-coordinate. Measured in pixels. If you prefer starting with a sentence, capitalize it and end it with a period: @param x Specifies the x-coordinate, measured in pixels. When writing multiple sentences, follow normal sentence rules: @param x Specifies the x-coordinate. Measured in pixels. Also see http://java.sun.com/j2se/javadoc/writingdoccomments/index.html#multiple@param. @return (reference page) Omit @return for methods that return void and for constructors; include it for all other methods, even if its content is entirely redundant with the method description. Having an explicit @return tag makes it easier for someone to find the return value quickly. Whenever possible, supply return values for special cases (such as specifying the value returned when an out-of-bounds argument is supplied). @deprecated (reference page) The @deprecated description in the first sentence should at least tell the user when the API was deprecated and what to use as a replacement. Only the first sentence will appear in the summary section and index. Subsequent sentences can also explain why it has been deprecated. When generating the description for a deprecated API, the Javadoc tool moves the @deprecated text ahead of the description, placing it in italics and preceding it with a bold warning: "Deprecated". An @see tag (for Javadoc 1.1) or {@link} tag (for Javadoc 1.2 or later) should be included that points to the replacement method: For Javadoc 1.2 and later, the standard format is to use @deprecated tag and the in-line {@link} tag. This creates the link in-line, where you want it. For example: /** * @deprecated As of JDK 1.1, replaced by {@link #setBounds(int,int,int,int)} */ For Javadoc 1.1, the standard format is to create a pair of @deprecated and @see tags. For example: /** * @deprecated As of JDK 1.1, replaced by setBounds * @see #setBounds(int,int,int,int) */ If the member has no replacement, the argument to @deprecated should be "No replacement". Do not add @deprecated tags without first checking with the appropriate engineer. Substantive modifications should likewise be checked first. @since (reference page) Specify the product version when the Java name was added to the API specification (if different from the implementation). For example, if a package, class, interface or member was added to the Java 2 Platform, Standard Edition, API Specification at version 1.2, use: /** * @since 1.2 */ The Javadoc standard doclet displays a "Since" subheading with the string argument as its text. This subheading appears in the generated text only in the place corresponding to where the @since tag appears in the source doc comments (The Javadoc tool does not proliferate it down the hierarchy). (The convention once was "@since JDK1.2" but because this is a specification of the Java Platform, not particular to the Sun JDK or SDK, we have dropped "JDK".) When a package is introduced, specify an @since tag in its package description and each of its classes. (Adding @since tags to each class is technically not needed, but is our convention, as enables greater visibility in the source code.) In the absence of overriding tags, the value of the @since tag applies to each of the package';s classes and members. When a class (or interface) is introduced, specify one @since tag in its class description and no @since tags in the members. Add an @since tag only to members added in a later version than the class. This minimizes the number of @since tags. If a member changes from protected to public in a later release, the @since tag would not change, even though it is now usable by any caller, not just subclassers. @throws (@exception was the original tag) (reference page) A @throws tag should be included for any checked exceptions (declared in the throws clause), as illustrated below, and also for any unchecked exceptions that the caller might reasonably want to catch, with the exception of NullPointerException. Errors should not be documented as they are unpredictable. For more details, please see http://java.sun.com/j2se/javadoc/writingdoccomments/index.html#throwstag. /** * @throws IOException If an input or output exception occurred */ public void f() throws IOException { // body } See the Exceptions chapter of the Java Language Specification, Second Edition for more on exceptions. Also see http://java.sun.com/j2se/javadoc/writingdoccomments/index.html#multiple@throws. @see (reference page) Also see http://java.sun.com/j2se/javadoc/writingdoccomments/index.html#multiple@see. @serial @serialField @serialData (All added in Javadoc 1.2) (reference page) For information about how to use these tags, along with an example, see "Documenting Serializable Fields and Data for a Class," Section 1.6 of the Java Object Serialization Specification. Also see Sun';s criteria for including classes in the serialized form specification. {@link} (Added in Javadoc 1.2) (reference page) For conventions, see Use In-Line Links Economically. Documenting Default Constructors Section 8.8.7 of the Java Language Specification, Second Edition describes a default constructor: If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided. It invokes the superclass constructor with no arguments. The constructor has the same access as its class. The Javadoc tool generates documentation for default constructors. When it documents such a constructor, Javadoc leaves its description blank, because a default constructor can have no doc comment. The question then arises: How do you add a doc comment for a default constructor? The simple answer is that it is not possible -- and, conveniently, our programming convention is to avoid default constructors. (We considered but rejected the idea that the Javadoc tool should generate a default comment for default constructors.) Good programming practice dictates that code should never make use of default constructors in public APIs: All constructors should be explicit. That is, all default constructors in public and protected classes should be turned into explicit constructor declarations with the appropriate access modifier. This explicit declaration also gives you a place to write documentation comments. The reason this is good programming practice is that an explicit declaration helps prevents a class from inadvertently being made instantiable, as the design engineer has to actually make a decision about the constructor';s access. We have had several cases where we did not want a public class to be instantiable, but the programmer overlooked the fact that its default constructor was public. If a class is inadvertently allowed to be instantiable in a released version of a product, upward compatibility dictates that the unintentional constructor be retained in future versions. Under these unfortunate circumstances, the constructor should be made explicit and deprecated (using @deprecated). Note that when creating an explicit constructor, it must match precisely the declaration of the automatically generated constructor; even if the constructor should logically be protected, it must be made public to match the declaration of the automatically generated constructor, for compatibility. An appropriate doc comment should then be provided. Often, the comment should be something as simple as: /** * Sole constructor. (For invocation by subclass constructors, typically * implicit.) */ protected AbstractMap() { } Documenting Exceptions with @throws Tag NOTE - The tags @throws and @exception are synonyms. Documenting Exceptions in API Specs The API specification for methods is a contract between a caller and an implementor. Javadoc-generated API documentation contains two ways of specifying this contract for exceptions -- the "throws" clause in the declaration, and the @throws Javadoc tag. These guidelines describe how to document exceptions with the @throws tag. Throws Tag The purpose of the @throws tag is to indicate which exceptions the programmer must catch (for checked exceptions) or might want to catch (for unchecked exceptions). Guidelines - Which Exceptions to Document Document the following exceptions with the @throws tag: All checked exceptions. (These must be declared in the throws clause.) Those unchecked exceptions that the caller might reasonably want to catch. (It is considered poor programming practice to include unchecked exceptions in the throws clause.) Documenting these in the @throws tag is up to the judgment of the API designer, as described below. Documenting Unchecked Exceptions It is generally desirable to document the unchecked exceptions that a method can throw: this allows (but does not require) the caller to handle these exceptions. For example, it allows the caller to "translate" an implementation-dependent unchecked exception to some other exception that is more appropriate to the caller';s exported abstraction. Since there is no way to guarantee that a call has documented all of the unchecked exceptions that it may throw, the programmer must not depend on the presumption that a method cannot throw any unchecked exceptions other than those that it is documented to throw. In other words, you should always assume that a method can throw unchecked exceptions that are undocumented. Note that it is always inappropriate to document that a method throws an unchecked exception that is tied to the current implementation of that method. In other words, document exceptions that are independent of the underlying implementation. For example, a method that takes an index and uses an array internally should not be documented to throw an ArrayIndexOutOfBoundsException, as another implementation could use a data structure other than an array internally. It is, however, generally appropriate to document that such a method throws an IndexOutOfBoundsException. Keep in mind that if you do not document an unchecked exception, other implementations are free to not throw that exception. Documenting exceptions properly is an important part of write-once, run-anywhere. -------------------------------------------------------------------------------- Background on Checked and Unchecked Exceptions The idea behind checking an exception is that the compiler checks at compile-time that the exception is properly being caught in a try-catch block. You can identify checked and unchecked exceptions as follows. Unchecked exceptions are the classes RuntimeException, Error and their subclasses. All other exception subclasses are checked exceptions. Note that whether an exception is checked or unchecked is not defined by whether it is included in a throws clause. Background on the Throws Clause Checked exceptions must be included in a throws clause of the method. This is necessary for the compiler to know which exceptions to check. For example (in java.lang.Class): public static Class forName(String className) throws ClassNotFoundException By convention, unchecked exceptions should not be included in a throws clause. (Including them is considered to be poor programming practice. The compiler treats them as comments, and does no checking on them.) The following is poor code -- since the exception is a RuntimeException, it should be documented in the @throws tag instead. java.lang.Byte source code: public static Byte valueOf(String s, int radix) throws NumberFormatException Note that the Java Language Specification also shows unchecked exceptions in throws clauses (as follows). Using the throws clause for unchecked exceptions in the spec is merely a device meant to indicate this exception is part of the contract between the caller and implementor. The following is an example of this (where "final" and "synchronization" are removed to make the comparison simpler). java.util.Vector source code: public Object elementAt(int index) java.util.Vector spec in the Java Language Specification, 1st Ed. (p. 656): public Object elementAt(int index) throws IndexOutOfBoundsException Package-Level Comments With Javadoc 1.2, package-level doc comments are available. Each package can have its own package-level doc comment source file that The Javadoc tool will merge into the documentation that it produces. This file is named package.html (and is same name for all packages). This file is kept in the source directory along with all the *.java files. (Do not put the packages.html file in the new doc-files source directory, because those files are only copied to the destination and are not processed.) Here';s an example of a package-level source file for java.text and the file that the Javadoc tool generates: package.html --------------> package-summary.html (source file) javadoc (destination file) The Javadoc tool processes package.html by doing three things: Copies its contents (everything between and ) below the summary tables in the destination file package-summary.html. Processes any @see, @since or {@link} Javadoc tags that are present. Copies the first sentence to the right-hand column of the Overview Summary. Template for package.html source file At Sun Microsystems, we use the following template when creating a new package doc comment file. This contains a copyright statement. Obviously, if you are from a different company, you would supply your own copyright statement. An engineer would copy this whole file, rename it to package.html, and delete the lines set off with hash marks: #####. One such file should go into each package directory of the source tree. Empty Template for Package-Level Doc Comment File Contents of package.html source file The package doc comment should provide (directly or via links) everything necessary to allow programmers to use the package. It is a very important piece of documentation: for many facilities (those that reside in a single package but not in a single class) it is the first place where programmers will go for documentation. It should contain a short, readable description of the facilities provided by the package (in the introduction, below) followed by pointers to detailed documentation, or the detailed documentation itself, whichever is appropriate. Which is appropriate will depend on the package: a pointer is appropriate if it';s part of a larger system (such as, one of the 37 packages in Corba), or if a Framemaker document already exists for the package; the detailed documentation should be contained in the package doc comment file itself if the package is self-contained and doesn';t require extensive documentation (such as java.math). To sum up, the primary purpose of the package doc comment is to describe the purpose of the package, the conceptual framework necessary to understand and to use it, and the relationships among the classes that comprise it. For large, complex packages (and those that are part of large, complex APIs) a pointer to an external architecture document is warranted. The following are the sections and headings you should use when writing a package-level comment file. There should be no heading before the first sentence, because the Javadoc tool picks up the first text as the summary statement. Make the first sentence a summary of the package. For example: "Provides classes and interfaces for handling text, dates, numbers and messages in a manner independent of natural languages." Describe what the package contains and state its purpose. Package Specification Include a description of or links to any package-wide specifications for this package that are not included in the rest of the javadoc-generated documentation. For example, the java.awt package might describe how the general behavior in that package is allowed to vary from one operating system to another (Windows, Solaris, Mac). Include links to any specifications written outside of doc comments (such as in FrameMaker or whatever) if they contain assertions not present in the javadoc-generated files. An assertion is a statement a conforming implementor would have to know in order to implement the Java platform. On that basis, at Sun, references in this section are critical to the Java Compatibility Kit (JCK). The Java Compatibility Kit includes a test to verify each assertion, to determine what passes as Java CompatibleTM. The statement "Returns an int" is an assertion. An example is not an assertion. Some "specifications" that engineers have written contain no assertions not already stated in the API specs (javadoc) -- they just elaborate on the API specs. In this respect, such a document should not be referred to in this section, but rather should be referred to in the next section. Include specific references. If only a section of a referenced document should be considered part of the API spec, then you should link or refer to only that section and refer to the rest of the document in the next section. The idea is to clearly delineate what is part of the API spec and what is not, so the JCK team can write tests with the proper breadth. This might even encourage some writers to break documents apart so specs are separate. Related Documentation Include references to any documents that do not contain specification assertions, such as overviews, tutorials, examples, demos, and guides. Class and Interface Summary [Omit this section until we implement @category tag] Describe logical groupings of classes and interfaces @see other packages, classes and interfaces Documenting Anonymous Inner Classes Anonymous inner classes are defined in Java Language Specification, Second Edition, at Anonymous Class Declaration. The Javadoc tool does not directly document anonymous classes -- that is, their declarations and doc comments are ignored. If you want to document an anonymous class, the proper way to do so is in a doc comment of its outer class, or another closely associated class. For example, if you had an anonymous TreeSelectionListener inner class in a method makeTree that returns a JTree object that users of this class might want to override, you could document in the method comment that the returned JTree has a TreeSelectionListener attached to it: /** * The method used for creating the tree. Any structural modifications * to the display of the Jtree should be done by overriding this method. * This method adds an anonymous TreeSelectionListener to the returned JTree. * Upon receiving TreeSelectionEvents, this listener calls refresh with * the selected node as a parameter. */ public JTree makeTree(AreaInfo ai){ } Including Images This section covers images used in the doc comments, not images directly used by the source code. NOTE: The bullet and heading images required with Javadoc version 1.0 and 1.1 are located in the images directory of the JDK download bundle: jdk1.1/docs/api/images/. Those images are no longer needed starting with 1.2. Prior to Javadoc 1.2, the Javadoc tool would not copy images to the destination directory -- you had to do it in a separate operation, either manually or with a script. Javadoc 1.2 looks for and copies to the destination directory a directory named "doc-files" in the source tree (one for each package) and its contents. (It does a shallow copy for 1.2 and 1.3, and a deep copy for 1.4 and later.) Thus, you can put into this directory any images (GIF, JPEG, etc) or other files not otherwise processed by the Javadoc tool. The following are the Java Software proposals for conventions for including images in doc comments. The master images would be located in the source tree; when the Javadoc tool is run with the standard doclet, it would copy those files to the destination HTML directory. Images in Source Tree Naming of doc images in source tree - Name GIF images -1.gif, incrementing the integer for subsequent images in the same class. Example: Button-1.gif Location of doc images in source tree - Put doc images in a directory called "doc-files". This directory should reside in the same package directory where the source files reside. (The name "doc-files" distinguishes it as documentation separate from images used by the source code itself, such as bitmaps displayed in the GUI.) Example: A screen shot of a button, Button-1.gif, might be included in the class comment for the Button class. The Button source file and the image would be located at: java/awt/Button.java (source file) java/awt/doc-files/Button-1.gif (image file) Images in HTML Destination Naming of doc images in HTML destination - Images would have the same name as they have in the source tree. Example: Button-1.gif Location of doc images in HTML destination - With hierarchical file output, such as Javadoc 1.2, directories would be located in the package directory named "doc-files". For example: api/java/awt/doc-files/Button-1.gif With flat file output, such as Javadoc 1.1, directories would be located in the package directory and named "images-". For example: api/images-java.awt/ api/images-java.awt.swing/ Examples of Doc Comments /** * Graphics is the abstract base class for all graphics contexts * which allow an application to draw onto components realized on * various devices or onto off-screen images. * A Graphics object encapsulates the state information needed * for the various rendering operations that Java supports. This * state information includes: *

    *
  • The Component to draw on *
  • A translation origin for rendering and clipping coordinates *
  • The current clip *
  • The current color *
  • The current font *
  • The current logical pixel operation function (XOR or Paint) *
  • The current XOR alternation color * (see setXORMode) *
*

* Coordinates are infinitely thin and lie between the pixels of the * output device. * Operations which draw the outline of a figure operate by traversing * along the infinitely thin path with a pixel-sized pen that hangs * down and to the right of the anchor point on the path. * Operations which fill a figure operate by filling the interior * of the infinitely thin path. * Operations which render horizontal text render the ascending * portion of the characters entirely above the baseline coordinate. *

* Some important points to consider are that drawing a figure that * covers a given rectangle will occupy one extra row of pixels on * the right and bottom edges compared to filling a figure that is * bounded by that same rectangle. * Also, drawing a horizontal line along the same y coordinate as * the baseline of a line of text will draw the line entirely below * the text except for any descenders. * Both of these properties are due to the pen hanging down and to * the right from the path that it traverses. *

* All coordinates which appear as arguments to the methods of this * Graphics object are considered relative to the translation origin * of this Graphics object prior to the invocation of the method. * All rendering operations modify only pixels which lie within the * area bounded by both the current clip of the graphics context * and the extents of the Component used to create the Graphics object. * * @author Sami Shaio * @author Arthur van Hoff * @version %I%, %G% * @since 1.0 */ public abstract class Graphics { /** * Draws as much of the specified image as is currently available * with its northwest corner at the specified coordinate (x, y). * This method will return immediately in all cases, even if the * entire image has not yet been scaled, dithered and converted * for the current output device. *

* If the current output representation is not yet complete then * the method will return false and the indicated {@link ImageObserver} * object will be notified as the conversion process progresses. * * @param img the image to be drawn * @param x the x-coordinate of the northwest corner of the * destination rectangle in pixels * @param y the y-coordinate of the northwest corner of the * destination rectangle in pixels * @param observer the image observer to be notified as more of the * image is converted. May be null * @return true if the image is completely * loaded and was painted successfully; * false otherwise. * @see Image * @see ImageObserver * @since 1.0 */ public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer); /** * Dispose of the system resources used by this graphics context. * The Graphics context cannot be used after being disposed of. * While the finalization process of the garbage collector will * also dispose of the same system resources, due to the number * of Graphics objects that can be created in short time frames * it is preferable to manually free the associated resources * using this method rather than to rely on a finalization * process which may not happen for a long period of time. *

* Graphics objects which are provided as arguments to the paint * and update methods of Components are automatically disposed * by the system when those methods return. Programmers should, * for efficiency, call the dispose method when finished using * a Graphics object only if it was created directly from a * Component or another Graphics object. * * @see #create(int, int, int, int) * @see #finalize() * @see Component#getGraphics() * @see Component#paint(Graphics) * @see Component#update(Graphics) * @since 1.0 */ public abstract void dispose(); /** * Disposes of this graphics context once it is no longer referenced. * @see #dispose() * @since 1.0 */ public void finalize() { dispose(); } } Troubleshooting Curly Quotes (Microsoft Word) Problem - A problem occurs if you are working in an editor that defaults to curly (rather than straight) single and double quotes, such as Microsoft Word on a PC -- the quotes disappear when displayed in some browers (such as Unix Netscape). So a phrase like "the display';s characteristics" becomes "the displays characteristics." The illegal characters are the following: 146 - right single quote 147 - left double quote 148 - right double quote What should be used instead is: 39 - straight single quote 34 - straight quote Preventive Solution - The reason the "illegal" quotes occurred was that a default Word option is "Change ';Straight Quotes'; to ';Smart Quotes';". If you turn this off, you get the appropriate straight quotes when you type. Fixing the Curly Quotes - Microsoft Word has several save options -- use "Save As Text Only" to change the quotes back to straight quotes. Be sure to use the correct option: "Save As Text Only With Line Breaks" - inserts a space at the end of each line, and keeps curly quotes. "Save As Text Only" - does not insert a space at the end of each lines, and changes curly quotes to straight quotes. -------------------------------------------------------------------------------- Footnotes [1] At Java Software, we use @version for the SCCS version. See "man sccs-get" for details. The consensus seems to be: %I% gets incremented each time you edit and delget a file %G% is the date mm/dd/yy When you create a file, %I% is set to 1.1. When you edit and delget it, it increments to 1.2. Some developers omit the date %G% (and have been doing so) if they find it too confusing -- for example, 3/4/96, which %G% would produce for March 4th, would be interpreted by those outside the United States to mean the 3rd of April. Some developers include the time %U% only if they want finer resolution (due to multiple check-ins in a day). The clearest numeric date format would be to have the date formatted with the year first, something like yyyy-mm-dd, as proposed in ISO 8601 and elsewhere (such as http://www.cl.cam.ac.uk/~mgk25/iso-time.html), but that enhancement would need to come from SCCS. [Author';s comment: Include more comments from step 5 of: ~dkramer/javadocdir/moving-frame-into-src.html#revisproc "Manual edit the HTML files produced by WebWorks"] -------------------------------------------------------------------------------- Copyright 2000 Sun Microsystems, Inc All Rights Reserved

How to Write Doc Comments

用金山快译翻译的
大家将就着看吧

TOP

How to Write Doc Comments

下面引用由bigblock2005/05/09 00:38am 发表的内容:
版权 2000 升阳公司,公司
版权所有
台湾的翻译?
呵呵,我看《Java深度历险》王森就是那么翻译的。

TOP

How to Write Doc Comments

该如何写文件意见 对于 JavadocTM 工具 Javadoc 首页 这一份文件描述我们为在文件中的爪哇使用意见在爪哇软件,升阳公司被写的计画风格引导者,标签和图像大会。 资讯科技不重新雕刻被复盖其他地方的相关材料: 作为叁考在 JavadocTM 上的材料标签,见 Javadoc 叁考页。 因为文件意见的必需与语意有关内容,为写爪哇美国石油协会规格见到需求。 内容 介绍 原则 用辞 来源文件 写作文件意见 一个文件意见的格式 文件意见检查工具 描述 一个风格引导者 标签大会 (@标签) 证明疏怠职责建设者 http:// java.sun.com/j2se/javadoc/writingdoccom/index.html# throwstag 包裹- 水平的意见 证明作者不详的内部班级 包括图像 文件意见的例子 修理卷曲的引证 (微软字) -------------------------------------------------------------------------------- 介绍 原则 在爪哇软件,我们举行可能超过第三者开发者的使我们的文件批评不同的一些指导方针。 我们的文件意见定义官方的爪哇月台美国石油协会规格。 对这一个结束,我们的目标听众是那些写爪哇兼容性测试, 或使一致或者再实现爪哇月台的人,除了开发者之外。我们花费时间,而且努力把重心集中在叙述边界情况,争论范围而且迫至一隅情形不愿定义通常的规画期限,写概念上的概观, 而且为开发者包括例子。 因此,那里普遍二个不同的方法写文件意见 -- 如美国石油协会规格, 或如规画指导文件。 这二个目标在下列的区段中被描述。 由于有雅量的资源一个职员能负担混合两者进相同的文件之内;(适当地大块) 然而,我们的优先听写我们给主要的焦点到写文件意见的美国石油协会规格。 这是开发者为什么时常需要转向其他的文件, 像是规画引导者的 J2SE 文件,爪哇个别指导或爪哇班级图书馆 ( 在硬皮的唯一) 。 写作美国石油协会规格 理想地,爪哇美国石油协会规格包含必需做爪哇月台的干净- 房间落实的所有断言为 " 写曾经,无论何处跑"-- 以致于任何的爪哇支程序或申请将会跑在任何的落实上的一样。 这在任何的建筑和功能的规格 ( 通常在 FrameMaker 写) 或任何其他的文件中加上那些可能在文件意见中包括断言。 这一种定义是一个高的目标并且有一些实际的限制到我们能叙述美国石油协会地多完全。 下列各项正在指导我们试着跟随的原则: 爪哇月台美国石油协会规格被文件定义原始码的意见,而且任何的文件从那些意见作记号如可到达的规格。 注意规格不需要完全地被包含在文件意见。尤其,冗长的规格有时最好的在一个分开的文件中格式而且联编到从一个文件意见。 爪哇月台美国石油协会规格是在来电者和落实之间的一份契约。 规格描述所有一位来电者能信赖的每个方法的行为方面。 资讯科技不描述落实细节, 像是方法是否是本国的或同步化。 规格应该描述 (本文地) 由一个给定的物体提供的线- 安全的保证。 缺乏对相反又所有的物体明白的指示被假定是 "线-保险箱".(也就是, 多样的线存取他们是可允许的协同地) 资讯科技是公开的现在规格没有总是活的在到这理想上面。 除非以别的方式着名, 爪哇美国石油协会规格断言需要是落实-中立派。 例外一定分别地而且显着地被设定显着的同样地。 我们对于该如何显着地证明落实不同有指导方针。 爪哇美国石油协会规格应该包含充份使软件质量保证能够写完全的爪哇兼容性装备 (JCK) 测试的断言。 这意谓文件意见一定使 SQA 的顺应需要测试满意。 意见不应该证明疯狂的或现在超出投机的落实如何碰巧工作。 写作规画引导者文件 来自一个规画引导者的分开美国石油协会规格是例子,疯狂的落实通常规画期限,特定的概念上的概观 (像是隐喻) 和描述的定义和工作区。 没有争论这些更快成为开发者的理解因素而且帮助一个开发者写可靠的申请。 然而,因为这些不包含美国石油协会 " 断言 ",他们在一件美国石油协会规格中不是必需的。你能包括文件意见的任何或所有的这数据。 ( 而且能包括订制的标签, 被订制的 doclet 处理, 促进它)在爪哇软件,我们有意识地不包括文件意见的这水平的文件, 而且改为也包括对这数据 (对爪哇个别指导和变化的目录联编) 的联编或包括相同的文件这数据下载如美国石油协会投机的捆 -- JDK 文件捆包括美国石油协会详细说明书和民众,例子和规画引导者。 资讯科技是有用讨论较远的细节有关该如何证明的事疯狂的和工作区。 有时有一个相差在密码应该如何工作之间而且它如何实际上工作。 这能轮流二种不同的表格: 疯狂的疯狂并且编码的美国石油协会投机。 资讯科技是有用在前面上面决定是否你想要证明文件意见的这些。 在我们已经决定在文件意见的外面证明这两个的爪哇软件,虽然我们确实发动例外。 疯狂的美国石油协会投机是疯狂的那在方法公告或影响语法或语意学的文件意见中在场。 当零被获准进入的时候,一个一个如此投机虫的例子是一个被指定丢 NullPointerException 的方法,但是零实际上是一个应该被接受的有用叁数。 ( 而且那样甚至被实现) 如果决定被作出改正美国石油协会规格, 说会是有用的或在美国石油协会规格本身中, 或在一连串的变化中到投机, 或两者的。 证明一种像一个文件意见的这美国石油协会不同,连同它的工作区一起,对他们是最有可能见到它的变化提醒一个开发者。 注意由于这一个订正的一件美国石油协会规格会仍然维持它的落实-独立。 疯狂的密码在落实方面是疯狂的不愿在美国石油协会规格中。 疯狂的密码和他们的工作区时常同样地被在一项虫报告中分开的分配。 然而,如果 Javadoc 工具是用来为特别的落实产生文件,包括文件意见的这数据会是相当有用, 合适地分开如一个笔记或被一个订制的标签.( 发言权 @虫) 世界卫生组织 " 拥有 " 文件意见 -作家或程序师 在爪哇软件,文件意见不是被作家或程序师独家地拥有的,但是他们的所有权在他们之间被分享。资讯科技是基本的前提作家和程序师尊敬彼此能力,而且两者成为最好的文件因素批评可能的。 时常它是一个谈判的物质决定谁写文件的哪一部份,被基于的知识,时间,资源,兴趣,美国石油协会复杂, 和在落实本身的状态上。 理想地,设计美国石油协会的人会写概略的来源文件的美国石油协会规格,藉由唯一的公告和文件意见,在落实方面不料竟会填充使书面的美国石油协会契约满意。 美国石油协会作家的目的是减轻来自一些工作的设计者。 在这情况,美国石油协会设计者会写开始的文件使用稀疏的语言意见,然后作家会检讨意见,精炼内容, 而且增加标签。 如果文件意见是再 implementors 的一件美国石油协会规格, 而且不只是针对开发者的导引,他们应该也被设计,而且实现了美国石油协会的程序师写, 或藉着是或已经成为一个服从的物质专家的一位美国石油协会作家。 如果落实被写到投机但是文件意见是未完成的,一位作家能藉由检查原始码或写测试美国石油协会的计画完成文件意见。一位作家可能为被丢的例外检查或测试,叁数边界为条件, 和为对无效力的争论接受度。然而,如果落实然后不被写到规格一位作家,一种较加困难的情形发生能着手进行写一件美国石油协会规格只有当如果他们也对设计者以他们的问题知道设计者 ( 也经过设计会议或完成的一分开的-书面的设计规格) 的意图或让现金存取。 因此,它可能让一位作家是更困难的为接口和摘要没有 implementors 的班级写文件。 藉由思想的那,这些指导方针预计描述完成的文件意见。 他们被想要当提议并非需求奴隶地被跟随如果他们似乎过度累赘, 或有创造力的替代选择能被发现。 当一个复杂的系统 , 像是爪哇 ( 包含大约 60个包裹) 正在被发展的时候,时常成为一特别组的包裹, 像是 javax.swing 的因素一群工程师可能发展不同于其他的团体指导方针。 这可能是由于那些包裹的不一致需求, 或因为资源拘束。 用辞 美国石油协会文件 (美国石油协会文件) 或美国石油协会规格 (美国石油协会详细说明书) 在线的或美国石油协会的 hardcopy 描述,主要地在爪哇为程序师想要写作。 这些能被产生使用 Javadoc 工具或者产生了一些其他的方法。 一件美国石油协会规格是一份特别类型的美国石油协会文件, 如同在上面描述。 美国石油协会规格的一个例子是在线的爪哇 2 月台, 标准版本美国石油协会规格。 美国石油协会文件的一个例子是 Chan/李的爪哇班级图书馆书。 文件意见 ( 文件意见) 被那 /* 界定的爪哇原始码的特别意见*.。。 */去限器。 这些意见被 Javadoc 处理工具产生美国石油协会文件。 javadoc 产生来自文件意见的美国石油协会文件的 JDK 工具。 来源文件 Javadoc 工具能产生输出起于 " 来源 " 文件的四不同类型: 为爪哇的原始码文件分类 (.新型的计算机语言)- 这些包含班级,接口,领域,建设者和方法意见。 包裹意见文件 - 这些包含包裹意见 概观意见文件 - 这些包含关于包裹的组意见 各种的不加工的文件 -这些包括图像,样品原始码,班级文件,支程序,HTML 申请, 和任何的否则你可能对来自早先的文件叁考想要。 对于较多的细节,见到: 来源文件。 -------------------------------------------------------------------------------- 写作文件意见 一个文件意见的格式 一个文件意见在 HTML 被写而且一定在班级,领域 , 建设者或方法公告之前。 资讯科技由二个部份组成 -- 被区段标签跟随的描述。 在这一个例子中,区段标签是 @param,@回返, 和 @见到。 例子 /** *归还一个然后能在荧屏上被画的图像物体。 *url 争论一定叙述绝对的 http:// java.sun.com/j2se/javadoc/writingdoccom/index.html#{@联编}. 名字 * 争论是一叙述那相对于 url 争论。 *

*这一个方法总是立刻归还, 是否那 *图像存在。 当这一个支程序尝试画那条线图像的时候在 *荧屏,数据将会被装载。 图形原始人 * 那平局图像将会逐渐增加地在荧屏上油漆。 * *@ param url 一个绝对的网址给图像的恶劣位置 * 命名图像的位置,相对于 url 争论 *@回返 在指定的网址图像 *@见到 图像 */ 公众的图像 getImage{(网址 url, 线名字) 尝试 { 归还 getImage(新的网址 (url,名字)); } 捕捉 (MalformedURLException e){ 无效力的回返; } } 注意: 来自流动的 Javadoc 的产生 HTML 在下面被显示 每条线上方被切割成锯齿状在意见下面使密码一致。 第一条行包含开始- 意见的去限器。 (/**) 以 Javadoc 1.4 开始, 领导的星号是可选择的。 写如方法的一个短摘要的第一个句子, 如同 Javadoc 自动地把它放在方法摘要桌子一样.( 和索引) 注意线内标签 {@联编网址}, 转换到一个 HTML 超链接指向文件因为网址分类。 这线内标签能被用任何地方一个意见能被写, 如此的当做在本文中在区段标签之后。 如果你有文件意见的超过一个段落,用一个

段落标签区隔段落,当做显示。 插入在描述和标签的目录之间的一个空白的意见线,当做显示。 第一条由行开始的一 "@" 个性结束描述。 只有每文件意见一个描述区段; 你在区段标签之后不能够继续描述。 最后一条行不像开始- 意见的去限器包含结束- 意见的去限器 (*/) 笔记那,结束-意见只包含一个单一星号。 对于较多的例子,见简单的例子。 因此线将不包装,任何的文件界限- 意见对 80个个性的线。 早先的例子所会在跑 Javadoc 工具之后看起来像的在这里: getImage 公众的图像 getImage(网址 url, 线名字) 归还一个然后能在荧屏上被画的图像物体。 url 争论一定叙述一个绝对的网址。 名字争论是一叙述那相对于 url 争论。 这一个方法总是立刻归还,是否图像存在。 当这一个支程序尝试画那条线在荧屏上的图像时候,数据将会被装载。 图形原始人画那条线图像将会逐渐增加地在荧屏上油漆。 叁数: url- 一个绝对的网址给图像的恶劣位置 命名 -图像的位置, 相对于 url 争论 回返: 在指定的网址图像 也见到: 图像 也在这一份文件结束的时候见到修理卷曲的引证 (微软字) 。 文件意见检查工具 在太阳,我们已经为检查文件意见发展一个工具,呼叫了太阳文件检查 Doclet, 或 DocCheck。 你在原始码上跑它,而且它产生报告描述风格和标签错误意见有, 而且推荐变化。 我们已经尝试使它的规则使一致到这一份文件的规则。 DocCheck 是 Javadoc doclet, 或 " 插件 ", 而且如此需要 Javadoc 工具被安装。 ( 如爪哇 2 的部份标准的版本 SDK) 描述 第一个句子 每个文件意见的第一个句子应该是一个摘要的句子,包含美国石油协会项目的简洁但是完全描述。 这意谓每个成员,班级,接口或包裹描述的第一个句子。Javadoc 工具对适当的成员复印这个第一的句子,分类/ 接口或包装摘要。 这使写脆的和情报的开始能靠他们自己站的句子是重要的。 这一个句子在被一个空格,定位键或线终结者跟随的第一个时期结束, 或在第一个标签.(当做在下面定义) 举例来说,这一个第一个句子在 " 教授 " 结束。: /** *这是一个教授的模拟。 Knuth';s 的混合计算机。 */ 然而,你藉由打字 HTML meta 能在附近工作这- 个性如此的当做 "&" 或 "<" 在时期之后立刻, 如此的当做: /** *这是一个教授的模拟。 Knuth';s 的混合计算机。 */ 或 /** *这是一个教授的模拟。Knuth';s 的混合计算机。 */ 尤其, 写区别的摘要句子超载了来自彼此的方法。 举例来说: /** * 班级建设者。 */ foo{() 。。。 /** *分类叙述物体的数字产生的建设者。 */ foo(int n){ 。。。 落实-独立 写描述给是落实-中立派, 但是叙述如此的属国哪里必需的。 这帮助工程师写密码给是 " 写曾经,无论何处跑". 如很多如可能的,写如一件落实- 独立的美国石油协会规格的文件意见。 清楚地定义是必需的东西和什么被允许改变横过月台/落实。 理想地,为使一致 implementors 使它成为完全的充足。 实际地, 包括充足的描述以便读原始码的某人能写一组顺应测试的实质上随员。 基本上,投机应该是完全的,包括边界情况,叁数范围和角落情形。 哪里适当的, 提到什么被不指定或者允许在落实之中改变的规格树叶。 如果你一定证明落实- 特性的行为, 请在和一个使它清楚它的天线引入线片语的一个分开的段落中证明它是落实-特性。 如果落实依照月台改变,然后在段落的开始 " 在 <月台> 上 " 叙述。 在可能因在一个月台上的落实而改变的其他情形中你可能使用天线引入线片语 "落实-特性:". java.lang.Runtime 的规格落实- 依赖部份的一个例子在这里: 在窗口系统上,窗口美国石油协会的 LoadLibrary 程序 loadLibrary 方法的路径搜寻行为和那相同。 " 在窗口上 " 的使用在句子的开始使它成为清楚的向上前面这是一个落实笔记。 自动机械再方法意见使用 你能藉由知道再避免打字文件意见工具复制 (继承) 的 Javadoc 如何为弃绝或实现其他的方法方法批评。 这在三情况发生: 当在一个班级中的一个方法弃绝一个总纲的一个方法的时候 当在一个接口中的一个方法弃绝 superinterface 的一个方法的时候 当在一个班级中的一个方法实现一个接口的一个方法的时候 在最初二个情形中,如果方法 m() 弃绝另外的一个方法,Javadoc 工具为 m() 将会在文件中产生一个副标题 " 弃绝 ",藉由对它正在弃绝的方法一个联编。 在第三个情形中,如果在一个给定的班级中的方法 m() 实现一个接口的一个方法, Javadoc 工具将会产生一个副标题 " 指定被" 在文件中为 m(),藉由对它正在实现的方法一个联编。 在这些情形中的所有三个中,如果方法 m() 没包含文件意见或标签,Javadoc 工具也将会复印它正在为 m 弃绝或实现到被产生的文件方法的本文。 () 所以如果文件那蹂躏或实现方法是充份的,你不需要为 m 增加文件。 () 如果你把任何的文件意见或标签加入 m()," 弃绝 " 或 "指定被" 副标题和联编将会仍然出现,但是没有本文将会被复印。 一个风格引导者 下列各项是写文件意见的描述有用的顶端和大会。 使用 <密码>为牛鼻子字和名字设计。 牛鼻子字和名字被弥补被 <密码>.。。 当在描述方面提到。 这包括: 爪哇牛鼻子字 包裹名字 班级名字 方法名字 接口名字 领域名字 争论名字 密码例子 使用在-线节俭地联编 你被鼓励增加和美国石油协会的连结名字 ( 列出的立刻在)上面使用那 {@联编}标签。 资讯科技不必需的增加一个文件意见的和所有的美国石油协会名字的连结。 因为联编注意他们自己 ( 藉着他们的颜色而且在 HTML 中下面划线, 和藉着原始码文件的他们长度意见),如果很多地用,它能使意见变成更困难读。 我们因此推荐把一个联编加入一个美国石油协会名字如果: 使用者可能实际上想要对于较多的数据 (在你的裁判中) 在它上按, 和 只对于文件的每个美国石油协会名字的第一发生批评 (不烦扰重复一个联编) 我们的听众被前进 ( 不新手) 程序师,因此,联编到 java.lang 包裹 (像是线) 的美国石油协会是通常不是必需, 或你感觉的其他美国石油协会会是众所周知的。 为一般形式的方法和建设者省略刮弧 当提及一个方法或有多样的表格建设者的时候,而且你意谓提及一种特定的形式,使用刮弧和争论类型。 举例来说, ArrayList 有二增加方法: 增加 (物体) 了又增加 (物体) 。 (int,物体) 那增加 (int,物体) 方法把一个项目在一个指定的位置加入这 arraylist。 然而, 如果谈及到两者形式的方法,全部省略刮弧。 因为那会暗示,所以资讯科技正在误导包括空的刮弧一特别形式的方法。 专心的这里是区别来自它的特别表格之中的任何一个的一般方法。 包括”方法”那个字区别它如一个方法和不是一个领域。 那增加方法准许你插入项目。 (偏爱) 那增加 () 方法使你能够插入项目。 ( 避免当你低劣 " 所有的表格 " 那增加方法) 好使用片语代替完全的句子,在短暂的兴趣中。 这尤其在开始的摘要中支撑和在 @param 标签描述。 使用第三个人 (描述的) 不第二个人。 (规约的) 描述是在第三个人中能明白的不愿第二个人命令。 拿标签。 (偏爱) 拿标签。 (避免) 方法描述由一个动词片语开始。 一个方法实现操作,因此,它通常从一个动词片语开始: 拿这一个钮扣的标签。 (偏爱) 这一个方法拿这一个钮扣的标签。 (避免) 班级/ 接口/ 领域描述能省略主题并且只是说物体。 这些美国石油协会时常描述事物并非行动或行为: 钮扣标签。 (偏爱) 这一个领域是钮扣标签。 (避免) 使用 " 这 " 代替 " 那 " 当提及一个物体从现在的班级产生的时候。 举例来说, getToolkit 方法的描述应该依下列各项读: 为这一个成份拿工具箱。 (偏爱) 为成份拿工具箱。 (避免) 增加描述超过美国石油协会名字。 最好的美国石油协会名字是 " 自我证明的 ",意谓他们基本上告诉你美国石油协会做的。 如果文件意见只以句子形式重复美国石油协会名字,它没有在提供较多的数据。 举例来说,如果方法描述只使用在方法名字中出现的字,然后它正在把无全然加入你可以推论出的。 理想的意见超越那些字而且应该总是用一些一点点的从美国石油协会名字不是立刻明显的数据奖赏你。 避免 - 描述在发言权无下面超过你从读方法名字知道的。 ”组”那个字," 工具 " , " 顶端 " 和 " 本文 " 只是在一个句子中被重复。 /** *设定工具顶端本文。 * *@ param 本文工具的本文装顶端 */ 民众空虚 setToolTipText{(串起本文) 优先的 -这描述更完全定义工具顶端是什么, 在登记而且被显示的较大上下文中回应光标。 /** *登记本文在工具顶端中显示。 本文 * 展览当光标在成份之上逗留的时候。 * *@ param 本文线显示。 如果本文是无效力的, * 工具顶端被为这一个成份关掉。 */ 民众空虚 setToolTipText{(串起本文) 当使用期限 " 领域 " 的时候,要清楚。 要知道那一个”领域”那个字有二意义: 静态的领域, 是对于 "班级变数" 另外的一个期限 本文领域, 当做在 TextField 班级中。 注意这种领域可能被限制到握住日期,数字或任何的本文。 交互的名字可能是 " 约会领域 " 或 " 数领域 ", 当做充用。 避免拉丁文 -- 使用 " 也知道当做"代替 " aka", 使用 " 那是 " 或 " 是特定的"代替 " 也就是 ",使用 " 举例来说 " 代替 " 举例来说 ", 而且使用 " 换句话说 " 或 " 即 " 代替 " viz". 标签大会 大部份的下列标签被以爪哇语言规格,第一个版本指定。 也见到 Javadoc 叁考页。 标签的次序 包括下列的次序标签: *@作家 ( 班级和接口只有,需要) *@版本 ( 班级和接口只有,必需的)(见到脚注 1) *@param (只有方法和建设者) *@回返 (只有方法) *@例外 (@投是一个在 Javadoc 1.2 中被增加的同义字) *@见到 *@自从~后 *@序列 ( 或 @ serialField 或 @serialData) *@ 声明不赞成 ( 见到该如何以及何时声明不赞成美国石油协会) 命令多样的标签 当一个标签出现一个文件意见的超过一次的时候,我们雇用下列的大会。如果需要, 群体的标签, 像是复 @见到标签, 能与另一个分离藉着和一个单一星号的一条空白的线附以签条。 多样的 @ 作家标签应该在按年代序的次序中被列出,与班级的创作者在顶端列出。 多样的 @param 标签应该在争论- 公告的次序中被列出。 这使视觉上相配目录给公告是比较容易的。 多样的 @ 丢标签 ( 也已知的当做 @例外) 应该按字母顺序地被例外名字列出。 多样的 @ 见到标签应该依下列各项被命令, 概略地是如同他们的争论一般的次序被 javadoc 寻找, 基本上从最近的对最远的通路,从最少- 取得资格到完全-合格的,下列的目录表示这前进。注意方法和建设者是在压缩次序方面,这首先意谓没有 arg 形式,然后 "1 arg" 形成,然后 "2 arg" 形成, 和如此往前。 在分类钥匙的秒被需要的地方, 他们可能被列出不是按字母顺序地就是合乎逻辑地聚集。 @ 见到 #领域 @ 见到 #建设者(类型,类型。。.) @ 见到 #建设者(类型遗传素质, 类型遗传素质。。.) @ 见到 #方法(类型,类型,.。.) @ 见到 #方法(类型遗传素质,类型,遗传素质。。.) @见到班级 @见到班级#领域 @见到班级#建设者(类型,类型。。.) @见到班级#建设者 (类型遗传素质, 类型遗传素质) @见到班级#方法(类型,类型,.。.) @见到班级#方法(类型遗传素质, 类型遗传素质,.。.) @见到包裹班级。 @见到 package.Class#领域 @见到 package.Class#建设者(类型,类型。。.) @见到 package.Class#建设者 (类型遗传素质, 类型遗传素质) @见到 package.Class#方法(类型,类型,.。.) @见到 package.Class#方法 (类型遗传素质,类型,遗传素质) @见到包裹 必需的标签 一 @param 标签为每个叁数被需要, 即使当描述是明显的。那 @ 回返标签为除了空虚之外归还某事的每个方法被需要,即使它对方法描述感到多余。 ( 每当可能的, 找非东西多余的 (理想地,较多的特性) 为标签使用批评.) 这些原则加快自动化搜寻而且自动化处理。 时常,也, 努力在额外的清楚中走开避免多余薪资。 标签意见 如一个提醒的人,这些标签的基本使用在 Javadoc 叁考页上被描述。 爪哇软件通常使用那跟随另外的指导方针为每个标签产生意见: @作家 ( 叁考页) 你能提供一 @ 作家标签, 多样的 @ 作家标签, 或不 @ 作家标签。 当新美国石油协会的发展是一个开着又联合的努力时候,在社区的这些数天中处理, JSR 能是在包裹水平为新的包裹考虑作家。 举例来说,新的包裹 java.nio 有 "@ 作家 JSR-51 专家团体"在包裹水平。 然后个别的程序师能被指定给 @在班级水平的作家。 就如这一个标签才能在概观,包裹被应用而且分类水平的,标签只适用于那些制造对设计或落实的重要贡献的人, 而且不通常包括技术上的作家。 那 @ 作家因为当产生美国石油协会规格的时候,它不被包括,所以标签不是紧要关头的,而且因此它只被正在看原始码的那些见到。 (版本历史也能为内在的目的作为决定贡献者)。 如果某人强烈地感觉他们需要增加 @ 作家在成员水平,他们可以藉由跑 javadoc 这么做使用新的 1.4个标签的选项: -标签作家:一:"着作:" 如果作家是未知的, 使用不归于如争论到 @作家。 也见到 http:// java.sun.com/j2se/javadoc/writingdoccom/index.html#multiple@author。 @版本 ( 叁考页) 为争论的爪哇软件大会到那 @ 版本标签是 SCCS 线 "%我%,%G%", 转换到某事相似的 "1.39,02/28/97"(毫米/dd/yy)当文件被检查离 SCCS 的时候。 @param ( 叁考页) 那 @param 标签被叁数的名字 ( 不数据类型) 跟随,被叁数的描述跟随。 藉着大会,描述的第一个名词是叁数的数据类型。 ( 文章喜欢 " 一 "," 一 ", 和 " 那 " 能在名词之前.) 例外被对原始的 int 有益,在数据类型通常被省略的地方。 另外的空间能在名字和描述之间被插入,以便描述排成一行在一个区段中上面。冲撞或其他的标点在描述之前不应该被插入,如 Javadoc 工具插入冲撞。 叁数名字是大会的小写字母。 数据类型从一个小写字母开始指出一个物体并非一个班级。 如果它是一个句子,如果它是一个片语 (没包含动词), 或一封大写字母的信,描述由一个小写字母开始。 只有当如果另外的一个片语或句子跟随它,用一个时期结束片语。 例子: *@param ch 个性被测试 *@ param 观察者图像观察者被通知 不支架叁数的名字在那之后 @ param 标签由于 <编码>.。。 因为 Javadoc 1.2 而且稍后自动地做这。 (由 1.4 开始,名字不能够包含任何的 HTML, 如同 Javadoc 比较那 @ param 名字和一样在签字中出现的名字而且发出一个警告如果有任何的不同.) 当写意见他们自己,大体上,从一个片语开始而且用句子跟随它的时候如果他们被需要。 当写一个片语的时候,不以大写字母写而且不以一个时期结束: @ param x x- 同等的人物, 在图素中测量 当写一个片语藉着一个句子跟随,不以大写字母写片语, 但是用一个时期结束它区别它和下一个句子的开始时候: @ param x x-同等的人物。 在图素中测量。 如果你用一个句子偏爱出发, 以大写字母写它而且用一个时期结束它: @param x 叙述 x-同等的人物,在图素中测量。 当写多样的句子时候,遵从常态句子规则: @param x 叙述 x-同等的人物。 在图素中测量。 也见到 http:// java.sun.com/j2se/javadoc/writingdoccom/index.html#multiple@param。 @回返 ( 叁考页) 省略 @ 回返对于归还空虚的方法和对于建设者; 为所有的其他方法包括它,即使它的内容对方法描述感到完全地多余。 有一明白的 @ 回返标签使它变成更容易让某人很快地找回返价值。 每当可能的, 对于特别的情形补给回返价值.( 如此的当做叙述被退还的价值当一个外面-- 范围的争论被供应的时候) @声明不赞成 ( 叁考页) 那 @ 声明不赞成第一个句子的描述应该至少告诉使用者当美国石油协会被声明不赞成的时候和该使用如一个替换什么。 只有第一个句子将会在摘要的区段和索引中出现。 后来的句子也能解释它为什么已经被声明不赞成。当为一个被声明不赞成的美国石油协会产生描述的时候, Javadoc 工具移动那 @在描述之前声明不赞成本文, 把它放在斜体字和前述的它由于一个大胆的警告: " 声明不赞成 " 。 一 @ 见到标签 (对于 Javadoc 1.1) 或 {@联编}标签 ( 对于 Javadoc 1.2 或稍后) 应该被包括对替换方法的那点: 因为 Javadoc 1.2 而且稍后,标准的格式是使用 @ 声明不赞成了标签和那在- 线 {@联编}标签。 这产生联编在-线,在你想要它的地方。 举例来说: /** *@ 声明不赞成当做 JDK 1.1, 代替被 {@ 联编 #setBounds}(int , int , int,int) */ 因为 Javadoc 1.1, 标准的格式是产生一双 @ 声明不赞成和 @见到标签。 举例来说: /** *@ 声明不赞成当做 JDK 1.1, 被 setBounds 代替 *@ 见到 #setBounds(int , int , int,int) */ 如果成员没有替换, 争论到 @ 声明不赞成应该是没有替换. 不要增加 @不需要首先和适当的工程师检查就声明不赞成了标签。 实词修正应该同样地被首先检查。 @自从~后 ( 叁考页) 当爪哇名字被增加到美国石油协会规格的时候,叙述产品版本。 ( 如果不同的落实)举例来说,如果包裹,班级 , 接口或成员被增加到爪哇 2 月台,标准的版本,在 1.2 版的美国石油协会规格,使用: /** *@ 自从 1.2 以后 */ Javadoc 标准 doclet 显示一 " 自从~以后 " 如它的本文和线争论的副标题。 这一个副标题只有在地方中的被产生的本文中出现符合到哪里那 @因为标签在来源文件意见中出现。 (Javadoc 工具不把它增殖下来阶级组织) ( 大会一次是 "@ 因为 JDK1.2"但是因为这是爪哇月台的一件规格, 不是特别到太阳 JDK 或 SDK ,我们已经降低 " JDK" )。 当一个包裹被介绍的时候, 叙述一 @ 自从~以后标签在它的包裹描述和它的每一个班级中。( 增加 @ 因为附以签条到每个班级是技术上不需要, 但是我们的大会, 如原始码的准许较棒的能见.) 缺乏弃绝标签, 价值那 @因为标签适用于每一个包裹的班级和成员。 当一个班级 ( 或接口) 被介绍的时候, 叙述一 @ 自从~以后它的班级描述的标签和不 @自从~以后成员的标签。 增加一 @ 因为只对成员附以签条超过班级在一个较迟的版本中增加。 这将数字减到最少 @自从~以后标签。 如果一个成员从保护对较迟的释放民众改变, 那 @因为标签不改变,即使现在它在~手边任何来电者是可使用的, 不只是次分选羊毛等级的工人。 @ 投 (@例外是最初的标签) ( 叁考页) 一 @ 丢标签应该为任何的被检查的例外 (在投子句中宣布) 被包括在内,当做在下面举例, 以及为任何的未加抑制的例外来电者可能适度地想要捕捉,除了 NullPointerException。 当他们是不可预知的时候,错误不应该被证明。 对于较多的细节,请见到 http:// java.sun.com/j2se/javadoc/writingdoccom/index.html#throwstag。 /** *@ 丢 IOException 如果输入或被发生的输出例外 */ 民众空虚 f() 丢 IOException{ //身体 } 见到爪哇语言规格的例外章,对于在例外上的更多秒版。 也见到 http:// java.sun.com/j2se/javadoc/writingdoccom/index.html#multiple@throws。 @见到 ( 叁考页) 也见到 http:// java.sun.com/j2se/javadoc/writingdoccom/index.html#multiple@see。 @ 序列 @ serialField @ serialData (全部在 Javadoc 1.2 中增加) ( 叁考页) 因为关于该如何使用这些标签的资讯,连同一个例子一起,见为一个班级证明 Serializable 领域和数据,爪哇物体连载规格的第 1.6 节。 也见到太阳对包括被写成连载长篇而刊登的形式规格的班级标准。 {@联编}(在 Javadoc 1.2 中增加) ( 叁考页) 对于大会, 见到使用在- 线节俭地的联编。 证明疏怠职责建设者 爪哇语言规格的第 8.8.7 节,秒版描述一个假设值建设者: 如果一个班级没包含建设者公告,然后一个没轮流叁数的假设值建设者自动地被提供。 资讯科技用没有争论唤起总纲建设者。 建设者有如同它的班级一般的通路。 Javadoc 工具为假设值建设者产生文件。 因为一个假设值建设者能没有文件意见,所以当它证明一个如此建设者的时候,Javadoc 留下它的描述空格。 问题然后发生: 你如何为一个假设值建设者增加一个文件意见? 简单的答案是它不是可能的 -- 并且,方便地,我们的规画大会是避免假设值建设者。 (我们考虑但是拒绝主意, Javadoc 工具应该为假设值建设者产生一个假设值意见)。 好规画练习命令那一个密码应该从不利用公众的美国石油协会的假设值建设者: 所有的建设者应该是明白的。那是, 全部当众疏怠职责建设者而且保护班级应该与适当的通路修饰语一起变成明白的建设者公告。 这明白的公告也给你一个地方写文件意见。 理由这是好的规画练习是明白的公告帮助从不注意地避免一个班级被做 instantiable, 如设计工程师必须实际上有关建设者的通路作决定。 我们已经有我们没有想要一个公众的班级是 instantiable 的一些情形,但是程序师俯看了事实它的假设值建设者是公众的。 如果一个班级不注意地被允许是产品的一个被释放的版本 instantiable,向上的兼容性听写无心的建设者在将来的版本中被保有。 在这些不幸的环境之下,建设者应该被做明白并且声明不赞成。 ( 使用 @声明不赞成) 注意,当创造一个明白的建设者的时候,它一定精确地相配自动地产生的建设者的公告; 即使建设者应该合乎逻辑地被保护,它一定被公开相配自动地产生的建设者的公告,为兼容性。 一适当的然后文件意见应该被提供。 时常,意见应该同样地是某事简单的当做: /** * 脚掌建设者。 ( 因为亚纲建设者的祈愿,典型地 *暗示的.) */ 保护了 AbstractMap{() } 用 @证明例外丢标签 注意 - 标签 @ 丢和 @例外是同义字。 证明美国石油协会详细说明书的例外 为方法的美国石油协会规格是在一位来电者和 implementor 之间的一份契约。 Javadoc 产生的美国石油协会文件包含为例外叙述这一份契约的二个方法 -- 公告的 " 投 " 子句, 和那 @丢 Javadoc 标签。 这些指导方针描述该如何证明例外那 @丢标签。 投标签 目的那 @投标签是指出哪一例外程序师一定捕捉 (为检查例外) 或可能想要捕捉。 (对于未加抑制的例外) 指导方针 -证明哪一例外 用那证明下列的例外 @丢标签: 全部检查了例外。 (这些一定在投子句中被宣布)。 来电者可能适度地想要捕捉的那些未加抑制的例外。 (资讯科技是考虑过的贫穷规画练习包括投子句的未加抑制的例外)。 证明这些在那 @ 投标签是决定于美国石油协会设计者的裁判, 如同在下面描述。 证明未加抑制的例外 资讯科技通常令人想要的证明未加抑制的例外一个方法能丢: 这允许 ( 但是不需要) 来电者处理这些例外。 举例来说,它允许来电者 " 翻译 " 落实-对被输出抽象化的来电者的对更适当的一些其他的例外依赖的未加抑制的例外。 因为没有方法保证一个呼叫已经证明所有的未加抑制的例外,它可能丢,程序师不能仰赖推测一个方法不能够丢那些它被证明丢之外的任何未加抑制的例外。 换句话说,你应该总是承担一个方法能丢无证明的未加抑制的例外。 注意它总是不适当证明一个方法丢对那一个方法的现在落实被系的未加抑制的例外。 换句话说,证明与在下面的落实无关的例外。 举例来说,一个轮流一个索引并且使用排列的方法在内部不应该被证明丢 ArrayIndexOutOfBoundsException, 如另外的落实可以除了排列之外使用数据结构在内部。 资讯科技然而,通常适当的证明一个如此方法丢 IndexOutOfBoundsException。 如果你不证明未加抑制的例外,记住,其他的落实是免费不丢那例外。 适当地证明例外是一个重要部份写-曾经,跑-无论何处。 -------------------------------------------------------------------------------- 背景在检查之上和未加抑制的例外 主意在后检查例外是编辑者检查在编译-计时例外在一个尝试- 捕捉的区段中正在适当地被捕捉。 你能识别检查和依下列各项未加抑制的例外。 未加抑制的例外是班级 RuntimeException ,错误和他们的亚纲。 所有的其他例外亚纲被检查例外。 注意,例外是否被检查或未加抑制的那不是被它是否被包含在一个投子句之中定义的。 在投子句上的背景 检查例外一定被包含在方法的一个投子句之中。 这是必需的让编辑者知道检查哪一例外。 举例来说:(在 java.lang.Class 中) 公众的静电班级 forName( 线 className) 丢 ClassNotFoundException 藉着大会,未加抑制的例外不应该被包含在一个投子句之中。 ( 包括他们被考虑是贫穷的规画练习。 编辑者视他们为意见, 而且做在他们上的没有检查)。 下列各项是贫穷的密码 -- 既然例外是 RuntimeException ,它应该被证明在那 @改为丢标签。 java.lang。位元组原始码: 民众静电位元组 valueOf( 线 s,int 基) 丢 NumberformatException 注意爪哇语言规格也表示投子句的未加抑制的例外。 ( 依下列各项) 只在投机中使用投子句作为未加抑制的例外是一个必须指出的装置这例外是在来电者和 implementor 之间的契约部份。 下列各项是这的一例子。 (" 结局 " 和 " 同步 " 被移动使比较变成更简单哪里) java.util。矢量原始码: 公众的物体 elementAt(int 索引) java.util。以爪哇语言规格,第一个 Edmund 无线电诱导投机。 (p。 656): 公众的物体 elementAt(int 索引) 丢 IndexOutOfBoundsException 包裹- 水平的意见 藉由 Javadoc 1.2 ,包裹- 水平的文件意见是可得的。 每个包裹能有它自己的包裹级的文件意见来源文件 Javadoc 工具将会进入它生产的文件之内合并。 这一个文件叫做 package.html 。 ( 而且是所有的包裹相同的名字) 这一个文件向前在来源目录中被保持由于所有的那 *.新型的计算机语言申请。 (不要把 packages.html 文件放入新的文件- 文件的来源目录,因为那些文件只被复印到目的地并且不被处理.) 这里是包裹级的来源一个例子 java.text 和 Javadoc 工具产生的文件文件: package.html-------------->包裹-summary.html ( 来源文件) javadoc (目的地文件) Javadoc 工具藉由做三件事物处理 package.html: 在目的地文件包裹的摘要桌子下面复印它的内容 ( 每件事物在 <身体>之间和 )-summary.html。 任何的程序 @见到,@ 因为或 {@联编} Javadoc 在场的标签。 复印第一个句子到那用右手纵队的概观摘要。 为 package.html 来源文件的型板 当创造意见申请的一个新的包裹文件的时候,在升阳公司,我们使用下列的型板。 这包含一份版权陈述。 明显地,如果你来自一家不同的公司,你会供应你自己的版权陈述。 一个工程师会复印这全部文件,重新命名它到 package.html, 而且划除与年功袖章一起出发的线: #####. 一个如此的文件应该进入来源树的每个包裹目录。 为包裹级的文件意见文件倒空型板 package.html 来源文件的内容 包裹文件意见应该提供 ( 直接地或经由联编) 允许程序师使用包裹所需的每件事物。 资讯科技是一个非常重要文件: 对于许多设备 ( 归一个单一包裹所有的那些人但是不在一个单一班级中) 它是程序师将会去拿文件的第一个地方。资讯科技应该包含设备的短又易读描述是由被对详细的文件 , 或详细的文件本身的指针跟随的包裹 (在介绍方面,在下面) 提供, 无论那一个是适当的。 哪一是适当的将会仰赖包裹: 如果资讯科技是一个较大的系统 ( 如此的当做,37 之一 Corba 的包裹) 的部份, 或一份 Framemaker 文件已经为包裹存在,一个指针是适当的; 如果包裹是自我包含的并且不需要广泛的文件,详细的文件应该被包含在包裹文件意见文件本身。 (像是 java.math) 为了总结,包裹文件意见的主要目的是描述包裹的目的,必需了解并且使用它的概念上的结构和在包含它的班级之中的关系。 为大的, 合成物包装 ( 和是大又复杂美国石油协会的部份那些人) 对一份外部的建筑学文件的一个指针被保证。 当写一个包裹级的意见申请的时候,下列各项是你应该使用的区段和标题。 因为 Javadoc 工具拾起第一个本文如摘要的陈述,所以在第一个句子之前应该有没有朝向。 使第一个句子成为包裹的一个摘要。 举例来说: " 以一个自然语言的样子中立派提供班级和接口给操作的本文,日期,数字和信息 " 。 描述包裹包含的而且说它的目的。 包裹规格 包括描述或对于这一个不被包含在 javadoc 产生的文件其它部分之中的包裹任何的包裹- 宽的规格联编。举例来说, java.awt 包裹可能描述那一个包裹的一般行为如何被允许从一个操作系统人变化到另外一 (窗口, Solaris, Mac) 。 如果他们包含在 javadoc 产生的文件中不出现的断言,包括对在文件的外面被写意见 ( 如此的当做在 FrameMaker 中或任何的) 任何的规格联编。 断言是使一致 implementor 会必须为了要实现爪哇月台 , 知道的一份陈述。 在那一种基础上,在太阳,这一个区段的叁考对爪哇兼容性装备 (JCK) 是紧要关头的。 爪哇兼容性装备包括一个测试查证每断言, 决定被看作是爪哇 CompatibleTM 的东西。 陈述 " 归还 int" 是断言。 一个例子不是断言。 一些 " 规格 " 那设计已经写没包含断言不已经在美国石油协会详细说明书 (javadoc) 中陈述 -- 他们仅仅详述美国石油协会详细说明书。 在这尊敬,一份如此文件应该不在这一个区段中被提到, 但是宁可应该在下一个区段中被提到。 包括特定的叁考。 如果只有叁考的文件一个区段应该被考虑美国石油协会投机的部份,然后你应该在下一个区段中联编或提及只有那一个区段而且提及文件的其它部分。 主意是清楚地描绘是美国石油协会投机的部份东西和什么是不,因此 JCK 队能用适当的宽度写测试。 这可能甚至鼓励一些作家分别地打破文件,如此详细说明书是分开的。 相关的文件 包括关于不包含规格断言, 像是概观,个别指导,例子,民众和引导者的任何文件的参考。 班级和接口摘要 [ 省略这一个区段,直到我们实现 @种类标签] 描述班级的合乎逻辑配列和接口 @ 见到其他的包裹,班级和接口 证明作者不详的内部班级 在作者不详的班级公告作者不详的内部班级以爪哇语言规格,秒版被定义。 Javadoc 工具不直接地证明作者不详的班级 -- 那是,他们的公告和文件意见被忽略。 如果你想要证明一个作者不详的班级,这么做的适当方法是在它的外部班级的一个文件意见 , 或另外的接近联合的班级中。 举例来说,如果你有了归还一个 JTree 物体的方法 makeTree 的作者不详的 TreeSelectionListener 内部的班级,这一个班级的使用者可能想要弃绝,你可以在方法中证明意见被送回的 JTree 附上 TreeSelectionListener 到它: /** *作为创造树的方法。 任何的结构修正 *到 Jtree 的展览应该被藉由弃绝这一个方法做。 *这一个方法把作者不详的 TreeSelectionListener 加入被送回的 JTree 。 * 一收到呼叫生气蓬勃的 TreeSelectionEvents ,这一个收听者 *如一个叁数的挑选节。 */ 公众的 JTree makeTree(AreaInfo ai){ } 包括图像 这一个区段包括被用于文件意见的图像而不是直接地被原始码用的图像。 注意: 对 Javadoc 1.0 和 1.1 版感到必需的子弹和标题图像位于 JDK 的图像目录下载捆: jdk1.1/文件/api/图像/. 那些图像不再是需要的开始由于 1.2. 在~之前 Javadoc 1.2 , Javadoc 工具不复印对目的地目录的图像 -- 你必须用手也在分开的操作方面做它或由于一个手写体。 Javadoc 1.2 容貌为和目的地目录的副本一个被命名的目录 "文件-申请"在来源树 ( 一对于每个包裹) 和它的内容中。 ( 资讯科技做浅的副本为 1.2 和 1.3, 而且做深的副本为 1.4 和稍后.) 因此,你能进入这一个目录之内放任何的图像 (图形交换格式,联合图象专家组格式,及其他) 或其他的文件不被 Javadoc 工具以别的方式处理。 下列各项是包括文件意见的图像大会的爪哇软件提议。 主人的图像会位于来源树; 当 Javadoc 工具与标准的 doclet 一起跑的时候,它会复印对目的地 HTML 目录的那些文件。 来源树的图像 来源树的文件图像的取名 - 名字图形交换格式图像 <班级>-1. gif,在相同的班级中为后来的图像增量完整的事物。 例子: 钮扣 -1. gif 来源树的文件图像的位置 - 把文件图像放入一个被叫做的目录 "文件-文件". 这一个目录应该归来源文件住的相同包裹目录所有。( 名字 "文件-申请"当文件从被原始码本身, 像是在图形用户接口中被显示的位图用的图像分开,区别它.) 例子: 钮扣,钮扣 -1. gif 的一个萤幕画面, 可能为钮扣被包含在班级之中意见分类。 钮扣来源文件和图像会位于: 新型的计算机语言/awt/ Button.java( 来源文件) 新型的计算机语言/awt/文件- 文件/ 钮扣 -1. gif( 图像文件) 在 HTML 目的地中图像 在 HTML 目的地中文件图像的取名 - 图像会有那相同的名字当他们有在来源树中。 例子: 钮扣 -1. gif 在 HTML 目的地中文件图像的位置 - 藉由阶层的文件输出, 像是 Javadoc 1.2 ,目录会位于被命名的包裹目录 "文件-文件". 举例来说: api/新型的计算机语言/awt/文件-文件/钮扣 -1. gif 藉由平坦的文件输出, 像是 Javadoc 1.1 ,目录会位于包裹目录而且命名 "图像-<包裹>". 举例来说: api/图像-java.awt/ api/图像-java.awt.swing/ 文件意见的例子 /** *图形对于所有的图形上下文是抽象的基础班级 * 允许一个在被了解的成份之上拉的申请在 * 各种不同的装置或在离开- 荧屏的图像之上。 * 一个图形物体装入胶囊州数据需要的 *对于爪哇支援的各种不同的翻译操作。 这 *州数据包括: *

    *
  • 成份临近 *
  • 一翻译起源对于翻译和剪断坐标 *
  • 现在的修剪 *
  • 现在的颜色 *
  • 现在的字型 *
  • 现在的合乎逻辑的图素操作功能 (XOR 或油漆) *
  • 现在的 XOR 交互颜色 * ( 见到 setXORMode) *
*

* 坐标无限瘦并且在图素之间躺着那 *输出装置。 *画条线一个身材的大纲操作藉由穿越操作 * 沿着和一枝图素规模钢笔的无限瘦的路径悬挂 *??落和对锚的权利在路径上的点。 *填充一个身材的操作藉由填充内部操作 *无限瘦的路径。 * 提出水平的本文操作提出那上 * 个性的部分完全地在基线同等的人物上面。 *

* 考虑的一些重要的点是那画一条线身材那 * 包括一个给定的长方形将会占领额外图素的排在 * 权利和底部边缘与填充相较一个身材是 *藉着那个相同的长方形跳跃。 * 同时, 画一条线水平线沿着相同的 y 同等的人物当做 *一条本文的线基线在下面将会完全地画那条线线 *除了任何的 descenders 以外的本文。 * 两者都这些财产是由于钢笔悬挂下来和到 *来自它横越的路径权利。 *

*出现如对这的方法来说的争论所有的坐标 *图形物体相对于翻译起源是考虑过的 *这图形在~之前方法的祈愿反对。 * 所有的翻译操作只修正在里面躺着的图素那 *被两者图形上下文的现在修剪跳跃的区域 * 而且成份的范围过去一直产生图形物体。 * *@作家 Sami Shaio *@作家 亚瑟货车 Hoff *@版本 %我%,%G% *@自从~后 1.0 */ 公众的摘要分类图形 { /** * 平局当许多指定的图像之时当做现在可得 * 藉由它的西北角落在指定的同等人物.(x,y) *这一个方法将会立刻在所有的情况归还, 即使那 *整个的图像还没有被依比例决定, 发抖而且转换 *对于现在的输出装置。 *

* 如果现在的输出表现是还未完全的然后 * 方法将会归还错误的和那指出 {@ 联编 ImageObserver} *物体将会被通知为转变程序进步。 * *@param img 图像是憔悴的 *@param x x- 西北的同等人物角落那 * 图素的目的地长方形 *@param y y- 西北的同等人物角落那 * 图素的目的地长方形 *@ param 观察者图像观察者被通知为更多那 * 图像被转换。 五月是 <密码>无效力的 *@回返 <密码>真实的 如果图像完全地 * 装载而且被成功地画; * <密码>错误的以别的方式。 *@见到 图像 *@见到 ImageObserver *@自从~后 1.0 */ 民众摘要 boolean drawImage(图像 img , int x , int y, ImageObserver 观察者); /** *解决被这图形上下文用的系统资源。 *图形上下文不能够被在被解决之后用。 *垃圾收集搬运夫的 finalization 程序决意 *也解决相同的系统资源,由于数字 *图形能在短时间体格中被产生的物体 * 它是较好的到用手自由地联合的资源 * 使用这一个方法不愿仰赖 finalization * 不可能发生长的时段程序。 *

* 图形被提供如对于油漆的争论物体 *而且成份的更新方法自动地被处理 * 藉着系统当那些方法归还的时候。 程序师应该, *对于效率, 呼叫那处理方法当使用完 * 一个图形物体只有当如果它直接地被产生从一 *成份或另外的图形反对。 * *@见到 #产生 (int , int , int,int) *@见到 #使结束 () *@见到 成份#getGraphics() *@见到 成份#油漆 (图形) *@见到 成份#更新 (图形) *@自从~后 1.0 */ 公众的摘要空虚处理;() /** *一经它是不再叁考,解决这图形上下文。 *@见到 #处理 () *@自从~后 1.0 */ 民众空虚使结束 {() 处理;() } } 修理卷曲的引证 (微软字) 问题 - 如果你正在一个编者中工作,一个问题发生卷曲的 ( 不愿直线) 独身者和两倍的引证假设值, 像是一个个人计算机的微软字 -- 当在一些 browers 中显示,引证消失。 (像是 Unix 网景) 如此一个像 " 展览的特性 " 的片语变成 " 展览特性 " 。 违法的个性是下列各项: 146- 正确地选出引证 147- 左边加倍引证 148- 正确的加倍引证 应该改为被用的是: 39- 笔直地选出引证 34- 直的引证 预防的解决 - 理由被发生的 " 违法的 " 引证是,假设值字选项是 " 变化 ';直的引证 '; 到 '; 聪明的引证". 如果你把这关掉 ,当你打字的时候,你拿适当的直引证。 固定卷曲的引证 - 微软的字有一些救援选项 -- 使用 " 解救当本文唯一的" 把引证改变回直的引证。 确定使用正确的选项: " 只解救如和线休息的本文 "- 在每条线结束的时候插入空间, 而且保存卷曲的引证。 " 如本文的救援唯一的"- 不在每条线结束的时候插入空间, 而且将卷曲的引证换成直的引证。 -------------------------------------------------------------------------------- 脚注 [在爪哇软件的 1], 我们使用 @对于 SCCS 版本的版本。 见到 " 男人 sccs-拿"对于细节。 共识似乎是: %我% 每时间拿增量你编辑和 delget 一个文件 %G% 是日期毫米/ dd/ yy 当你产生一个文件的时候,%我%被设定成 1.1. 当你编辑的时候和 delget 它, 它增量到 1.2. 如果他们找它太困惑的,一些开发者省略日期 %G%( 而且已经这么做)-- 举例来说,3/4/96,%G% 会生产三月 4 日哪一, 会被解释被那些在美国以外意谓四月第三日。 只有当如果他们想要较好的决议,一些开发者包括时间 %U% 。 (在一天中由于多样的报到处) 最清楚的数值日期格式会是有首先与那年一起格式的日期,某事喜欢 yyyy-毫米-dd,如国际标准组织 8601 和其他地方 (像是 http:// www.cl.cam.ac.uk/~ mgk25/iso-time.html) 所计划,但是提高会需要来自 SCCS 。 [作家的意见: 包括来自第 5 步骤的较多意见: ~dkramer/javadocdir/移动-体格-进入-之内src.html#revisproc " 手册编辑被 WebWorks 生产的 HTML 文件 "] -------------------------------------------------------------------------------- 版权 2000 升阳公司,公司 版权所有 用金山快译翻译的 大家将就着看吧

TOP

How to Write Doc Comments

翻译一下

TOP

How to Write Doc Comments

哈哈
用javadoc很简单就能写好文档

TOP

返回列表 回复 发帖