Java类类getDeclaredConstructors()方法及示例

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

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

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

  • getDeclaredConstructors() method is used to return an array of Constructor object that indicates the types of constructor defined by the class denoted by this Class object (Constructor may be public, private, protected or default).

    getDeclaredConstructors()方法用于返回一个Constructor对象数组,该数组指示此Class对象所表示的类定义的构造函数的类型(Constructor可以是public,private,protected或default)。

  • getDeclaredConstructors() 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.

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

  • getDeclaredConstructors() method may throw an exception at the time of returning a Constructor[].

    getDeclaredConstructors()方法在返回Constructor []时可能会引发异常。

    SecurityException: In this exception, it may raise when the security manager exists.

    SecurityException :在此异常中,当安全管理器存在时可能会引发此异常。

Syntax:

句法:

    public Constructor[] getDeclaredConstructors ();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is Constructor[], it returns an array of Constructor object denoting all declared constructor (private, public, protected, default) of this Class.

此方法的返回类型为Constructor [] ,它返回一个Constructor对象数组,该数组表示该Class的所有已声明的构造函数(私有,公共,保护,默认)。

Example:

例:

// Java program to demonstrate the example 
// of Constructor [] getDeclaredConstructors() method of Class

import java.lang.reflect.*;

public class GetDeclaredConstructorsOfClass {
    public static void main(String[] args) throws Exception {
        GetDeclaredConstructorsOfClass dc = new GetDeclaredConstructorsOfClass();

        Class cl = dc.getClass();

        Constructor[] cons = cl.getDeclaredConstructors();

        for (int i = 0; i < cons.length; ++i)
            System.out.println("Declared Constructors :" + cons[i].toString());

    }
    private GetDeclaredConstructorsOfClass(Integer i, Short s, Long l) {
        this.i = i;
        this.s = s;
        this.l = l;
    }

    public GetDeclaredConstructorsOfClass() {

        System.out.println("We are in public Constructor");
    }

    short sh = 10;
    Integer i = new Integer(100);
    Short s = new Short(sh);
    Long l = new Long(30l);
}

Output

输出量

We are in public Constructor
Declared Constructors :private GetDeclaredConstructorsOfClass(java.lang.Integer,java.lang.Short,java.lang.Long)
Declared Constructors :public GetDeclaredConstructorsOfClass()


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值