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 根据类名示例化类