Detailed description of the contents of package package in Java programming and the specification of package objects _java

Source: Internet
Author: User
Tags neural net

Contents of the Package
the contents of the package should be carefully designed so that it contains only functionally related classes and interfaces. The class in the package is free to access the private members of other classes in the package, and some classes may even have sufficient permissions to access the internal details of other classes, and we need to protect the class members in order to avoid the misuse of class members by such classes. Any member that is not declared private can be accessed by all other types in the same package, so the degree of coupling between any unrelated classes may be higher than we expected.

The package also provides a logical grouping function for programmers looking for useful interfaces and classes. A package made up of unrelated classes makes it difficult for programmers to tell which interfaces and classes are useful, and a logical grouping of classes can help programmers reuse code, because programmers can easily find what they need through logical groupings. If the package contains only the relevant, tightly coupled set of types, it means that we can give the type a more intuitive name, so as to avoid the name conflict.

Packages can be nested. For example, Java.lang is a nested package in which packet Lang is nested in a larger package Java, while the package J Ava contains some other packages. Nesting allows related packages to form a hierarchical naming system.

For example, in order to create a set of packages for adaptive systems such as neural networks and genetic algorithms, we can name the package with a dot-delimited moniker, creating nested packages:

Package adaptive. Neural Net;

The source file containing the statement above is located in the Adaptive.neuralnet package, and the Adaptive.neuralnet package itself is a child of the adaptive package. The adaptive package may contain classes that are related to a common adaptive algorithm, such as a generalization problem statement class or a benchmark class. Packages that are deeper in the hierarchy, such as adaptive. Neu-ralnet or Adaptive.genetic, contain classes that are related to a particular type of adaptive algorithm.

The nesting of packages is only one tool for organizing related packages, and it does not provide any special access rights between packages.

The class code in the Adaptive.genetic package cannot access members with package access in adaptive or adaptive.neuralnet packages, and the package scope applies only to specific packages. Nested packages can group related packages and help programmers find the classes they want more conveniently at the logical level, but beyond that, it does not bring any other benefits.

Notes for Packages

Packages can also have annotations. The problem, however, is that because packages are an organizational structure with no source entities, they are not actually defined, so they cannot be annotated like classes or methods, so the package annotations can only be implemented by annotating the package's declaration statements in the source file. However, only one package declaration in each package can have annotations that work on it.

So how do you annotate a package? In fact, the Java language does not force programmers to use a single annotated package statement rule in some way. The recommended approach is to create a file in the package directory named Package I Nfo.java, where only the package statement and the package annotation are stored, without any additional content. For example, a package Info.java file for a attr package looks like this:

@PackageSpec (Name two "Attr Project", version= "1.0"

@DevelopmentSite ("attr.project.org")

@DevelopmentModel (" Open one Source ")

package attr;

where Packagespec,developmentsite and devel Opmentmodel are used to decorate the annotation types, of course, they have a run-time save policy. Package a Info.java file should be compiled with the other source files in the package.

We recommend that all package-related information be placed in package one info. Java file. If you do this, you can place document comments at the beginning of the file so that they are annotated as package documents.

Package Objects and Specifications
A package usually implements a specification and usually comes from an organization. The package object differs from other reflection types, cannot be used to create or manipulate packages, but only as a repository of information that provides information about the specifications implemented by the package (the title of the specification, vendor and version number), and information about the package's implementation itself (the package title, vendor, and version number). Although a package usually comes from a single organization, the specifications it implements, such as the statistical analysis library, may be defined by other organizations. A program that uses a package might need to know the version of the specification that the package implements, so that you can use features that are defined only in one version. Similarly, these programs may also need to know which implementation version is provided to it, primarily to address possible defects in different versions. Some of the main methods of the package class allow access to this information:

    • public stri ng getName (): Returns the name of the package.
    • . public string Getspecificationtitle p: Returns the caption of the specification implemented by the package, and returns null if the title is unknown.
    • . public string Getspecificationversion (): Returns a string describing the version information of the specification implemented by the package, or null if the version information is unknown.
    • . public string Getspecificationvendor Q: Returns the name of the vendor that owns and maintains the specifications implemented by the package and returns null if the vendor is unknown.
    • . public string Getimplerentationtitle (): Returns the title of the implementation provided by the package, and returns null, public string getimplementationversion () if the title is unknown: Returns a string that describes the version information of the implementation provided by the package, or null if the version information is unknown.
    • public String Getimplementationvendor (): Returns the name of the Organization (vendor) that provided the implementation, or NULL if the organization is unknown.

For example, extracting this information from the Java.lang package in our system will result in the following results: '

Specification Title:java Platform API Specification Specification version:1.4 specification Vendor:sun Micro

Systems,inc.

Implementation Title:java Runtime Environment Implementation VERSION:1.5.0_02 implementation Vendor:sun MICR

Osystems,inc.

The canonical version number consists of a non-negative number separated by a period delimiter, such as ' 2.0 ' or ' 11.0.12 '. This pattern allows us to invoke the Iscompatiblewith method to compare the version number of the schema with the version number of the package. The method returns true if the package's version number is greater than or equal to the version number of the descendant. This comparison compares a period-delimited number each time, and if any of these numbers is less than the corresponding position in the version number passed in, the two versions are incompatible. If one version number is longer than the other, the missing part of the short version number is considered zero. For example, if the specification version number of the package is "1.4", and we use the Iscompatiblewith method to compare it to "1.2", "1.3.1", or ". 1.81", then returns true, but if compared to ' 1.4.2 '. or '. 5 ', Then false is returned. The reason for this conclusion is that the comparison mechanism assumes that the canonical version is backward compatible.

The implemented version number does not have a specified format because the different organizations that provide the implementation have different definitions of the implementation version. The only comparison that can be made between the implementation versions is whether the test version is the same, and there is no backward-compatible assumption.

The package can be sealed, which means no more classes are added to the package. An unsealed package can contain classes from several different locations in a class search path, and the contents of a sealed package must come from the same location-either a particular archive or a location specified by a URL. There are two ways to determine whether a package is sealed:

. Public Boolean issealed P: If the package is sealed, return Trueo

. public boolean issealed (URL URL): Returns True if the package is sealed for a given URL, that is, the class in the package can be loaded from this given URL. If the class in the package cannot be loaded from the given URL, or if the package is not sealed, returns false, and the specification and implementation information for the package is usually provided as part of the manifest file that is stored with the package-for example, as part of a manifest file in a Java archive (jar), like the 25.9.2 section. Archive file Java.util.jar "as described in. When the class is loaded in the package, the information is read by the person. The class loader (ClassLoader) can dynamically define a package object for the class it is to load:

We can get the package object of this class by invoking the Getpackage method of the class object for the given classes. We can also invoke the static side package.getpackage with the given package name to get the package object, or call the static side package.getpackages, which returns the package array of all the packages currently known by the ClassLoader. Both of these methods are related to the classloader of the code that calls them, because the code will call the Get-package or Getpackages method of its classloader. These ClassLoader methods search for a particular class loader and all its parent class loaders, and if no settings are made for the current ClassLoader, the System class loader is used at this time. Note that if the package is unknown, the class loader method returns NULL because no type is loaded in the package at this time.

Related Article

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.