java 根据类名示例化类_Java类类getDeclaredClasses()方法与示例

java 根据类名示例化类

类的类getDeclaredClasses()方法 (Class class getDeclaredClasses() method)

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

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

  • getDeclaredClasses() method is used to return an array of Class objects that reflects private, protected, public, and default defined by the class but it does not include child classes or interfaces.

    getDeclaredClasses()方法用于返回一组Class对象,这些数组反映该类定义的私有,受保护,公共和默认值,但不包括子类或接口。

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

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

  • getDeclaredClasses() method may throw a SecurityException at the time of returning an array of Class objects.

    返回一个Class对象数组时, getDeclaredClasses()方法可能会引发SecurityException

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

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

Syntax:

句法:

    public Class[] getDeclaredClasses ();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is Class, it returns an array of Class object denoting the defined member of this Class.

此方法的返回类型为Class ,它返回一个Class对象数组,该数组表示该Class的已定义成员。

Note:

注意:

  • When the class does not declare classes or interfaces as member, 0 is returned.

    当类未将类或接口声明为成员时,将返回0。

  • When the class reflects primitive or void type or an array type, 0 is returned.

    当类反映原始或void类型或数组类型时,将返回0。

Example:

例:

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

public class GetDeclaredClassesOfClass {
    public static void main(String[] args) throws Exception {
        // It returns the Class object with the given class name
        Class cl = Class.forName("java.lang.String");

        // It returns an array of Classes 
        // that denotes the private, protected, public and default 
        // classes of the class String
        Class[] cl_array = cl.getDeclaredClasses();

        // Traversing String class
        for (int i = 1; i < cl_array.length; ++i) {
            System.out.print(" String Declared Classes: ");
            System.out.println(cl_array[i].getName());
        }
    }
}

Output

输出量

Declared Classes:java.lang.String$CaseInsensitiveComparator


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

java 根据类名示例化类

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值