java 根据类名示例化类_Java类类getEnclosingMethod()方法及示例

java 根据类名示例化类

类类getEnclosingMethod()方法 (Class class getEnclosingMethod() method)

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

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

  • getEnclosingMethod() method is used to return a recent enclosing method of the underlying class when this Class object denotes local or anonymous class inside the method.

    当此Class对象在方法内部表示本地或匿名类时,使用getEnclosingMethod()方法返回基础类的最新封闭方法。

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

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

  • getEnclosingMethod() method does not throw an exception at the time of returning Method object.

    getEnclosingMethod()方法在返回Method对象时不会引发异常。

Syntax:

句法:

    public Method getEnclosingMethod();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is Method, it returns recently enclosing method of the underlying class when this class is local or anonymous class.

该方法的返回类型为Method ,当此类是本地或匿名类时,它将返回基础类的最近封闭方法。

Note: When this class is not local or anonymous, null is returned.

注意:如果此类不是本地的或匿名的,则返回null。

Example:

例:

// Java program to demonstrate the example 
// of Method getEnclosingMethod () 
// method of Class 

public class GetEnclosingMethodOfClass {
    public Object a1;
    public GetEnclosingMethodOfClass() {
        class A1 {};
        a1 = new A1();
    }

    public Object A1() {
        class A1 {};
        return new A1();
    }

    public static void main(String[] args) {

        // It returns the class of A1 method is 
        // GetEnclosingMethodOfClass and Method A1 is enclose in 
        // class GetEnclosingConstructorOfClass 
        Class cl = (new GetEnclosingMethodOfClass()).A1().getClass();

        System.out.print("Enclosing Method :");
        System.out.print(cl.getEnclosingMethod());
    }
}

Output

输出量

Enclosing Method :public java.lang.Object GetEnclosingMethodOfClass.A1()


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

java 根据类名示例化类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值