java package
包类的getPackages()方法 (Package Class getPackages() method)
getPackages() method is available in java.lang package.
getPackages()方法在java.lang包中可用。
getPackages() method is used to return all the packages recently for the callers ClassLoader instance when the caller ClassLoader instance is the bootstrap ClassLoader instance which may be denoted by null in some deployments so the only packages similar loaded by bootstrap loader instance will be returned.
当调用方ClassLoader实例是bootstrap ClassLoader实例(在某些部署中可能用null表示)时 ,使用getPackages()方法返回调用方ClassLoader实例最近的所有软件包,因此将返回由bootstrap loader实例加载的唯一类似软件包。
getPackages() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
getPackages()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
getPackages() method does not throw an exception at the time of return packages.
getPackages()方法在返回包时不会引发异常。
Syntax:
句法:
public static Package[] getPackages();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is Package[], it returns san array of packages for the caller's ClassLoader instance otherwise it returns 0 when no package exists.
此方法的返回类型为Package [] ,它为调用者的ClassLoader实例返回完整的软件包数组,否则,当不存在任何软件包时,它将返回0。
Example:
例:
// Java program to demonstrate the example
// of Package[] getPackages() of Package method
public class GetPackages {
public static void main(String[] args) {
// Get Packages by using getPackages() method
Package[] pkg = Package.getPackages();
// Display name of the packages
System.out.print("Package Name: ");
for (int i = 0; i < pkg.length; ++i)
System.out.print(pkg[i].getName());
}
}
Output
输出量
Package Name: java.util.zipjava.lang.reflectjava.util.concurrentjdk.internal.org.objectweb.asmsun.security.actionjdk.internal.reflectsun.net.www.protocol.jrtjava.nio.filejava.nio.file.attributesun.security.utiljava.netjava.securityjdk.jfr.internaljava.util.concurrent.atomicjava.util.concurrent.locksjava.nio.file.spijdk.internal.miscjava.lang.refsun.launcherjdk.internal.loadersun.net.wwwjava.utilsun.niosun.invoke.utiljava.lang.invokejdk.internal.refsun.net.www.protocol.jarsun.util.localesun.nio.fssun.reflect.annotationjava.util.streamjava.lang.modulesun.net.utiljdk.internal.utilsun.nio.csjava.util.functionjava.niojava.util.jarjava.iojava.security.certjava.langsun.net.www.protocol.filejava.nio.charset.spijdk.internal.modulejava.lang.annotationjava.nio.charset
翻译自: https://www.includehelp.com/java/package-getpackages-method-with-example.aspx
java package