Java Package类相关方法调用示例

x33g5p2x  于2021-08-19 转载在 Java  
字(6.4k)|赞(0)|评价(0)|浏览(196)

Package对象包含关于Java包的实现和规范的版本信息。这些版本信息是由加载该类的ClassLoader实例检索并提供的。通常,它被存储在与类一起分发的清单中。
组成包的类集可能会实现一个特定的规范,如果是这样的话,规范的标题、版本号和供应商字符串就会标识该规范。应用程序可以询问该包是否与某个特定的版本兼容,详情见isCompatibleWith方法。

Java包类方法

下面的类图显示了Java Package类方法的列表。

Java包类方法/应用程序实例

让我们通过例子来演示几个重要的和常用的Package类方法的用法。在下面的PackageClassMethods类中,每个方法名称都描述了Package类方法及其用法。请在https://docs.oracle.com/javase/8/docs/api/java/lang/Package.html查看每个Package类的方法描述。

package net.javaguides.lang;

//*/*
/* Class demonstrates the usage of Package class methods with examples
/* 
/* @author Ramesh Fadatare
/*
/*/
public class PackageClassMethods {

    public static void main(String[] args) {
        PackageClassMethods classMethods = new PackageClassMethods();
        classMethods.toStringMethod();
        classMethods.isSealedMethod();
        classMethods.isCompatibleWithMethod();
        classMethods.getSpecificationVersionMethod();
        classMethods.getSpecificationVendorMethod();
        classMethods.getSpecificationTitleMethod();
        classMethods.getPackagesMethod();
        classMethods.getPackageMethod();
        classMethods.getNameMethod();
        classMethods.getImplementationVersionMethod();
        classMethods.getImplementationVendorMethod();
        classMethods.getImplementationTitleMethod();
    }

    public void toStringMethod() {
        // get the java lang package
        Package pack = Package.getPackage("java.lang");

        // print the package as a string
        System.out.println("" + pack.toString());
    }

    public void isSealedMethod() {
        // get the java lang package
        Package pack = Package.getPackage("java.lang");

        // check if this package is sealed
        System.out.println("" + pack.isSealed());
    }

    public void isCompatibleWithMethod() {
        // get the java lang package
        Package pack = Package.getPackage("java.lang");

        // check if this package is compatible with version 1.4.6
        System.out.println("" + pack.isCompatibleWith("1.8"));
    }

    public void getSpecificationVersionMethod() {
        // get the java lang package
        Package pack = Package.getPackage("java.lang");

        // print the specification version for this package
        System.out.println("" + pack.getSpecificationVersion());
    }

    public void getSpecificationVendorMethod() {
        // get the java lang package
        Package pack = Package.getPackage("java.lang");

        // print the specification vendor for this package
        System.out.println("" + pack.getSpecificationVendor());
    }

    public void getSpecificationTitleMethod() {
        // get the java lang package
        Package pack = Package.getPackage("java.lang");

        // print the specification title for this package
        System.out.println("" + pack.getSpecificationTitle());
    }

    public void getPackagesMethod() {
        // get all the packages
        Package[] pack = Package.getPackages();

        // print all packages, one by one
        for (int i = 0; i < pack.length; i++) {
            System.out.println("" + pack[i]);
        }
    }

    public void getPackageMethod() {
        // create a package object for java.lang package
        Package pack = Package.getPackage("java.lang");

        // get the fully qualified name for this package
        System.out.println("" + pack.getName());
    }

    public void getNameMethod() {
        // create a package object for java.lang package
        Package pack = Package.getPackage("java.lang");

        // get the fully qualified name for this package
        System.out.println("" + pack.getName());
    }

    public void getImplementationVersionMethod() {
        // create a package object for java.lang package
        Package pack = Package.getPackage("java.lang");

        // get the implementation version
        System.out.println("" + pack.getImplementationVersion());
    }

    public void getImplementationVendorMethod() {
        // create a package object for java.lang package
        Package pack = Package.getPackage("java.lang");

        // get the implementation vendor
        System.out.println("" + pack.getImplementationVendor());
    }

    public void getImplementationTitleMethod() {
        // create a package object for java.lang package
        Package pack = Package.getPackage("java.lang");

        // get the annotation for lang package
        System.out.println("" + pack.getImplementationTitle());
    }
}

输出:

package java.lang, Java Platform API Specification, version 1.8
false
true
1.8
Oracle Corporation
Java Platform API Specification
package sun.reflect, Java Platform API Specification, version 1.8
package java.util, Java Platform API Specification, version 1.8
package sun.reflect.annotation, Java Platform API Specification, version 1.8
package java.lang.annotation, Java Platform API Specification, version 1.8
package java.nio, Java Platform API Specification, version 1.8
package sun.nio, Java Platform API Specification, version 1.8
package java.security.cert, Java Platform API Specification, version 1.8
package java.util.zip, Java Platform API Specification, version 1.8
package sun.launcher, Java Platform API Specification, version 1.8
package sun.security.action, Java Platform API Specification, version 1.8
package java.nio.file, Java Platform API Specification, version 1.8
package java.nio.charset, Java Platform API Specification, version 1.8
package sun.net.www, Java Platform API Specification, version 1.8
package java.lang.ref, Java Platform API Specification, version 1.8
package java.net, Java Platform API Specification, version 1.8
package jdk.internal.util, Java Platform API Specification, version 1.8
package sun.net.www.protocol.file, Java Platform API Specification, version 1.8
package java.lang.invoke, Java Platform API Specification, version 1.8
package sun.util.locale, Java Platform API Specification, version 1.8
package sun.reflect.generics.repository, Java Platform API Specification, version 1.8
package sun.misc, Java Platform API Specification, version 1.8
package java.lang.reflect, Java Platform API Specification, version 1.8
package sun.net.util, Java Platform API Specification, version 1.8
package net.javaguides.lang
package java.security, Java Platform API Specification, version 1.8
package sun.net.www.protocol.jar, Java Platform API Specification, version 1.8
package java.util.concurrent, Java Platform API Specification, version 1.8
package sun.io, Java Platform API Specification, version 1.8
package java.util.concurrent.atomic, Java Platform API Specification, version 1.8
package java.util.concurrent.locks, Java Platform API Specification, version 1.8
package sun.util, Java Platform API Specification, version 1.8
package java.lang, Java Platform API Specification, version 1.8
package java.io, Java Platform API Specification, version 1.8
package sun.reflect.misc, Java Platform API Specification, version 1.8
package sun.nio.ch, Java Platform API Specification, version 1.8
package java.util.jar, Java Platform API Specification, version 1.8
package sun.nio.cs, Java Platform API Specification, version 1.8
package java.util.function, Java Platform API Specification, version 1.8
package java.nio.charset.spi, Java Platform API Specification, version 1.8
package sun.security.util, Java Platform API Specification, version 1.8
package sun.usagetracker, Java Platform API Specification, version 1.8
java.lang
java.lang
1.8.0_172
Oracle Corporation
Java Runtime Environment

###引用

https://docs.oracle.com/javase/8/docs/api/java/lang/Package.html

相关文章

最新文章

更多