Java ClassLoader getParent()方法与示例

ClassLoader类的getParent()方法 (ClassLoader Class getParent() method)

  • getParent() method is available in java.lang package.

    getParent()方法在java.lang包中可用。

  • getParent() method is used to return the parent class loader for delegations.

    getParent()方法用于返回委托的父类加载器。

  • getParent() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getParent()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getParent() method is a final method, it does not override in child class.

    getParent()方法是最终方法,它不会在子类中重写。

  • getParent() method may throw an exception at the time of returning ClassLoader.

    返回ClassLoader时, getParent()方法可能会引发异常。

    SecurityException: This exception may throw when its checkPermission() method does not allow access to the parent class loader of this loader when the security manager exists.

    SecurityException :当存在安全管理器时,如果其checkPermission()方法不允许访问此加载器的父类加载器,则可能引发此异常。

Syntax:

句法:

    protected  final ClassLoader getParent();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is ClassLoader, it returns a parent class loader.

该方法的返回类型为ClassLoader ,它返回父类加载器。

Example:

例:

// Java program to demonstrate the example 
// of ClassLoader getParent() method of ClassLoader 

public class GetParentOfClassLoader {
 public static void main(String args[]) throws ClassNotFoundException {

  // It loads the class 
  Class cl = Class.forName("GetParentOfClassLoader");

  // It returns the class loader associated with 
  // the given class
  ClassLoader loader = cl.getClassLoader();

  // Display Loader Class
  System.out.println("Loader Class : ");
  System.out.println(loader.getClass());

  System.out.println();

  // Display Loader Class Parent
  System.out.println("Loader Parent Class : ");
  System.out.println(loader.getParent());
 }
}

Output

输出量

Loader Class : 
class jdk.internal.loader.ClassLoaders$AppClassLoader

Loader Parent Class : 
[email protected]


翻译自: https://www.includehelp.com/java/classloader-getparent-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值