📜  Java.lang.Class Java中的类 |设置 1

📅  最后修改于: 2022-05-13 01:55:34.316000             🧑  作者: Mango

Java.lang.Class Java中的类 |设置 1

Java在Java.lang 包中提供了一个名为Class的类。 Class 类的实例表示正在运行的Java应用程序中的类和接口。原始Java类型(boolean、byte、char、short、int、long、float 和 double)和关键字void也表示为 Class 对象。它没有公共构造函数。类对象由Java虚拟机 (JVM) 自动构建。这是一个最终类,所以我们不能扩展它。

Class 类方法在反射 API 中被广泛使用。

创建一个类对象

创建 Class 对象的三种方法:

  1. Class.forName(“className”) :由于类 Class 不包含任何构造函数,因此类 Class 中存在静态工厂方法,即Class.forName() ,用于创建类 Class 的对象。下面是语法:
    Class c = Class.forName(String className)
    

    上面的语句为作为字符串参数(className)传递的类创建了 Class 对象。请注意,参数 className 必须是要为其创建 Class 对象的所需类的完全限定名称。 Java中任何类中返回相同类对象的方法也称为工厂方法。要为其创建 Class 对象的类名是在运行时确定的。

  2. Myclass.class :当我们在类名后面写 .class 时,它引用代表给定类的 Class 对象。它主要用于原始数据类型,并且仅在我们知道类的名称时使用。要为其创建 Class 对象的类名是在编译时确定的。下面是语法:
    Class c = int.class
    

    请注意,此方法与类名一起使用,而不是与类实例一起使用。例如

    A a = new A();   // Any class A
    Class c = A.class; // No error
    Class c = a.class; // Error 
    
  3. obj.getClass() :此方法存在于 Object 类中。它返回 this(obj) 对象的运行时类。下面是语法:
    A a = new A();   // Any class A
    Class c = a.getClass();
    

方法:

  1. String toString() :此方法将 Class 对象转换为字符串。它返回字符串表示形式,即字符串“类”或“接口”,后跟一个空格,然后是类的完全限定名称。如果 Class 对象表示原始类型,则此方法返回原始类型的名称,如果它表示void ,则返回“void”。
    Syntax : 
    public String toString()
    Parameters : 
    NA
    Returns :
    a string representation of this class object.
    Overrides :
    toString in class Object
    
    // Java program to demonstrate toString() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        { 
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
            Class c2 = int.class;
            Class c3 = void.class;
              
            System.out.print("Class represented by c1: ");
             
            // toString method on c1
            System.out.println(c1.toString());
              
            System.out.print("Class represented by c2: ");
              
            // toString method on c2
            System.out.println(c2.toString());
              
            System.out.print("Class represented by c3: ");
              
            // toString method on c3
            System.out.println(c3.toString());
        }
    }
    

    输出:

    Class represented by c1: class java.lang.String
    Class represented by c2: int
    Class represented by c3: void
    
  2. Class forName(String className) :如前所述,此方法返回与具有给定字符串名称的类或接口关联的 Class 对象。接下来讨论该方法的另一个变体。
    Syntax : 
    public static Class forName(String className) throws ClassNotFoundException
    Parameters : 
    className - the fully qualified name of the desired class.
    Returns :
    return the Class object for the class with the specified name.
    Throws :
    LinkageError : if the linkage fails
    ExceptionInInitializerError - if the initialization provoked by this method fails
    ClassNotFoundException - if the class cannot be located
    
    // Java program to demonstrate forName() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // forName method
            // it returns the Class object for the class 
            // with the specified name 
            Class c = Class.forName("java.lang.String");
              
            System.out.print("Class represented by c : " + c.toString());
        }
    }
    

    输出:

    Class represented by c : class java.lang.String
    
  3. Class forName(String className,boolean initialize, ClassLoader loader) :此方法还使用给定的类加载器返回与具有给定字符串名称的类或接口关联的 Class 对象。

    指定的类加载器用于加载类或接口。如果参数加载器为空,则通过引导类加载器加载该类。仅当初始化参数为真且之前未初始化时,才初始化该类。

    Syntax : 
    public static Class forName(String className,boolean initialize, ClassLoader loader) 
    throws ClassNotFoundException
    Parameters : 
    className - the fully qualified name of the desired class
    initialize - whether the class must be initialized
    loader - class loader from which the class must be loaded
    Returns :
    return the Class object for the class with the specified name.
    Throws :
    LinkageError : if the linkage fails
    ExceptionInInitializerError - if the initialization provoked by this method fails
    ClassNotFoundException - if the class cannot be located
    
    // Java program to demonstrate forName() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object for this class
            Class myClass = Class.forName("Test");
          
            ClassLoader loader = myClass.getClassLoader();
              
            // forName method
            // it returns the Class object for the class 
            // with the specified name using the given class loader
            Class c = Class.forName("java.lang.String",true,loader);
              
            System.out.print("Class represented by c : " + c.toString());
        }
    }
    

    输出:

    Class represented by c : class java.lang.String
    
  4. T newInstance() :此方法创建此 Class 对象表示的类的新实例。该类是由一个带有空参数列表的表达式创建的。如果尚未初始化该类,则将其初始化。
    Syntax : 
    public T newInstance() throws InstantiationException,IllegalAccessException
    TypeParameters : 
    T - The class type whose instance is to be returned
    Parameters : 
    NA
    Returns :
    a newly allocated instance of the class represented by this object.
    Throws :
    IllegalAccessException - if the class or its nullary constructor is not accessible.
    InstantiationException - if this Class represents an abstract class, an interface,
    an array class, a primitive type, or void
    or if the class has no nullary constructor; 
    or if the instantiation fails for some other reason.
    ExceptionInInitializerError - if the initialization provoked by this method fails.
    SecurityException - If a security manager, s, is present
    
    // Java program to demonstrate newInstance() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException, InstantiationException,
                             IllegalAccessException
        {
            // returns the Class object for this class
            Class myClass = Class.forName("Test");
              
            // creating new instance of this class
            // newInstance method
            Object obj = myClass.newInstance();
              
            // returns the runtime class of obj
            System.out.println("Class of obj : " + obj.getClass());
        }
    }
    

    输出:

    Class of obj : class Test
    
  5. boolean isInstance(Object obj) :此方法确定指定的 Object 是否与该 Class 表示的对象赋值兼容。它相当于Java中的 instanceof运算符。
    Syntax : 
    public boolean isInstance(Object obj)
    Parameters : 
    obj - the object to check
    Returns :
    true if obj is an instance of this class else return false
    
    // Java program to demonstrate isInstance() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c = Class.forName("java.lang.String");
      
            String s = "GeeksForGeeks";
            int i = 10;
              
              
            // checking for Class instance
            // isInstance method
            boolean b1 = c.isInstance(s);
            boolean b2 = c.isInstance(i);
              
            System.out.println("is s instance of String : " + b1);
            System.out.println("is i instance of String : " + b1);
              
        }
    }
    

    输出:

    is s instance of String : true
    is i instance of String : false
    
  6. boolean isAssignableFrom(Class cls) :此方法确定此 Class 对象表示的类或接口是否与指定的 Class 参数表示的类或接口相同,或者是超类或超接口。
    Syntax : 
    public boolean isAssignableFrom(Class cls)
    Parameters : 
    cls - the Class object to be checked
    Returns :
    true if objects of the type cls can be assigned to objects of this class
    Throws:
    NullPointerException - if the specified Class parameter is null.
    
    // Java program to demonstrate isAssignableFrom() method
    public class Test extends Thread
    {
        public static void main(String[] args)
                             throws ClassNotFoundException, InstantiationException,
                             IllegalAccessException
        {
            // returns the Class object for this class
            Class myClass = Class.forName("Test");
              
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Thread");
            Class c2 = Class.forName("java.lang.String");
              
           // isAssignableFrom method on c1
           // it checks whether Thread class is assignable from Test
           boolean b1 = c1.isAssignableFrom(myClass);
             
           // isAssignableFrom method on c2
           // it checks whether String class is assignable from Test
           boolean b2 = c2.isAssignableFrom(myClass);
             
           System.out.println("is Thread class Assignable from Test : " + b1);
           System.out.println("is String class Assignable from Test : " + b2);
             
        }
    }
    

    输出:

    is Thread class Assignable from Test : true
    is String class Assignable from Test : false
    
  7. boolean isInterface() :此方法确定指定的 Class 对象是否表示接口类型。
    Syntax : 
    public boolean isInterface()
    Parameters : 
    NA
    Returns :
    return true if and only if this class represents an interface type else return false
    
    // Java program to demonstrate isInterface() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
            Class c2 = Class.forName("java.lang.Runnable");
              
            // checking for interface type
      
            // isInterface method on c1
            boolean b1 = c1.isInterface();
      
            // is Interface method on c2
            boolean b2 = c2.isInterface();
              
            System.out.println("is java.lang.String an interface : " + b1);
            System.out.println("is java.lang.Runnable an interface : " + b2);
        }
    }
    

    输出:

    is java.lang.String an interface : false
    is java.lang.Runnable an interface : true
    
  8. boolean isPrimitive() :此方法确定指定的 Class 对象是否表示原始类型。
    Syntax : 
    public boolean isPrimitive() 
    Parameters : 
    NA
    Returns :
    return true if and only if this class represents a primitive type else return false
    
    // Java program to demonstrate isPrimitive method
    public class Test
    {
        public static void main(String[] args) 
        {
            // returns the Class object associated with an integer;
            Class c1 = int.class;
              
           // returns the Class object associated with Test class
            Class c2 = Test.class;
             
            // checking for primitive type
      
            // isPrimitive method on c1
            boolean b1 = c1.isPrimitive();
      
            // isPrimitive method on c2
            boolean b2 = c2.isPrimitive();
              
            System.out.println("is "+c1.toString()+" primitive : " + b1);
            System.out.println("is "+c2.toString()+" primitive : " + b2);
        }
    }
    

    输出:

    is int primitive : true
    is class Test primitive : false
    
  9. boolean isArray() :此方法确定指定的 Class 对象是否表示数组类。
    Syntax : 
    public boolean isArray() 
    Parameters : 
    NA
    Returns :
    return true if and only if this class represents an array type else return false
    
    // Java program to demonstrate isArray method
    public class Test
    {
        public static void main(String[] args)
        {
            int a[] = new int[2];
              
            // returns the Class object for array class
            Class c1 = a.getClass();
              
            // returns the Class object for Test class
            Class c2 = Test.class;
              
            // checking for array type
      
            // isArray method on c1
            boolean b1 = c1.isArray();
      
            // is Array method on c2
            boolean b2 = c2.isArray();
              
            System.out.println("is "+c1.toString()+" an array : " + b1);
            System.out.println("is "+c2.toString()+" an array : " + b2);
              
        }
    }
    

    输出:

    is class [I an array : true
    is class Test an array : false
    
  10. boolean isAnonymousClass() :当且仅当此类是匿名类时,此方法才返回 true。匿名类与本地类类似,只是它们没有名称。
    Syntax : 
    public boolean isAnonymousClass() 
    Parameters : 
    NA
    Returns :
    true if and only if this class is an anonymous class.
    false,otherwise.
    
  11. boolean isLocalClass() :当且仅当此类是本地类时,此方法才返回 true。本地类是在Java代码块中本地声明的类,而不是作为类的成员。
    Syntax : 
    public boolean isLocalClass()
    Parameters : 
    NA
    Returns :
    true if and only if this class is a local class.
    false,otherwise.
    
  12. boolean isMemberClass() :当且仅当此类是成员类时,此方法才返回 true。成员类是被声明为包含类的非静态成员的类。
    Syntax : 
    public boolean isMemberClass() 
    Parameters : 
    NA
    Returns :
    true if and only if this class is a Member class.
    false,otherwise.
    

    下面是解释使用 isAnonymousClass() 、isLocalClass 和 isMemberClass() 方法的Java程序。

    // Java program to demonstrate isAnonymousClass() ,isLocalClass 
    // and isMemberClass() method
      
    public class Test
    {
        // any Member class of Test
        class A{}
          
        public static void main(String[] args) 
        {
            // declaring an anonymous class
            Test t1 = new Test()
            {
                //  class definition of anonymous class
            };
              
            // returns the Class object associated with t1 object
            Class c1 = t1.getClass();
              
            // returns the Class object associated with Test class
            Class c2 = Test.class;
              
            // returns the Class object associated with A class
            Class c3 = A.class;
              
            // checking for Anonymous class
            // isAnonymousClass method
            boolean b1 = c1.isAnonymousClass();
            System.out.println("is "+c1.toString()+" an anonymous class : "+b1);
                  
            // checking for Local class
            // isLocalClass method
            boolean b2 = c2.isLocalClass();
            System.out.println("is "+c2.toString()+" a local class : " + b2);
              
            // checking for Member class
            // isMemberClass method
            boolean b3 = c3.isMemberClass();
            System.out.println("is "+c3.toString()+" a member class : " + b3);
             
        }
    }
    

    输出:

    is class Test$1 an anonymous class : true
    is class Test a local class : false
    is class Test$A a member class : true
    
  13. boolean isEnum() :当且仅当该类在源代码中声明为枚举时,此方法才返回 true。
    Syntax : 
    public boolean isEnum() 
    Parameters : 
    NA
    Returns :
    true iff this class was declared as an enum in the source code.
    false,otherwise.
    
    // Java program to demonstrate isEnum() method
      
    enum Color
    {
        RED, GREEN, BLUE;
    }
       
    public class Test
    {
        public static void main(String[] args) 
        {   
            // returns the Class object associated with Color(an enum class)
            Class c1 = Color.class;
              
            // returns the Class object associated with Test class
            Class c2 = Test.class;
              
            // checking for Enum class
            // isEnum method
            boolean b1 = c1.isEnum();
            boolean b2 = c2.isEnum();
              
            System.out.println("is "+c1.toString()+" an Enum class : " + b1);
            System.out.println("is "+c2.toString()+" an Enum class : " + b2);
        }
    }
    

    输出:

    is class Color an Enum class : true
    is class Test an Enum class : false
    
  14. boolean isAnnotation() :此方法确定此 Class 对象是否表示注释类型。请注意,如果此方法返回 true,isInterface() 方法也将返回 true,因为所有注解类型也是接口。
    Syntax : 
    public boolean isAnnotation() 
    Parameters : 
    NA
    Returns :
    return true if and only if this class represents an annotation type else return false
    
    // Java program to demonstrate isAnnotation() method
      
    // declaring an Annotation Type
    @interface A
    {
         // Annotation element definitions
    }
      
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object associated with A  annotation
            Class c1 = A.class;
      
            // returns the Class object associated with Test class
            Class c2 = Test.class;
              
            // checking for annotation type
            // isAnnotation method
            boolean b1 = c1.isAnnotation();
            boolean b2 = c2.isAnnotation();
              
            System.out.println("is "+c1.toString()+" an annotation  : " + b1);
            System.out.println("is "+c2.toString()+" an annotation : " + b2);
              
        }
    }
    

    输出:

    is interface A an annotation  : true
    is class Test an annotation : false
    
  15. String getName() :此方法以 String 形式返回此 Class 对象表示的实体(类、接口、数组类、原始类型或 void)的名称。
    Syntax : 
    public String getName()
    Parameters : 
    NA
    Returns :
    returns the name of the name of the entity 
    represented by this object.
    
    // Java program to demonstrate getName() method
    public class Test
    {
        public static void main(String[] args) 
        {
            // returns the Class object associated with Test class
            Class c = Test.class;
              
            System.out.print("Class Name associated with c : ");
      
            // returns the name of the class
            // getName method
            System.out.println(c.getName());
        }
    }
    

    输出:

    Class Name associated with c : Test
    
  16. String getSimpleName() :此方法返回源代码中给出的底层类的名称。如果底层类是匿名类,它返回一个空字符串。

    数组的简单名称是附加了“[]”的组件类型的简单名称。特别是组件类型为匿名的数组的简单名称是“[]”。

    Syntax : 
    public String getSimpleName()
    Parameters : 
    NA
    Returns :
    the simple name of the underlying class
    
    // Java program to demonstrate getSimpleName() method
    public class Test
    {
        public static void main(String[] args) 
                throws ClassNotFoundException 
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
      
            System.out.print("Class Name associated with c : ");
              
            // returns the name of the class
            // getName method
            System.out.println(c1.getName());
      
            System.out.print("Simple class Name associated with c : ");
              
            // returns the simple name of the class
            // getSimpleName method
            System.out.println(c1.getSimpleName());
        }
    }
    

    输出:

    Class Name associated with c : java.lang.String
    Simple class Name associated with c : String
    
  17. ClassLoader getClassLoader() :此方法返回该类的类加载器。如果类加载器是引导类加载器,则此方法返回 null,因为引导类加载器是用 C、C++ 等本地语言实现的。
    如果此对象表示原始类型或 void,则也返回 null。
    Syntax : 
    public ClassLoader getClassLoader()
    Parameters : 
    NA
    Returns :
    the class loader that loaded the class or interface represented by this object
    represented by this object.
    Throws :
    SecurityException - If a security manager and its checkPermission method 
    denies access to the class loader for the class.
    
    // Java program to demonstrate getClassLoader() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object for this class
            Class myClass = Class.forName("Test");
              
              
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
              
            // returns the Class object for primitive int
            Class c2 = int.class;
              
            System.out.print("Test class loader : ");
              
            // getting the class loader for Test class
            // getClassLoader method on myClass
            System.out.println(myClass.getClassLoader());
              
            System.out.print("String class loader : ");
              
            // getting the class loader for String class
            // we will get null as String class is loaded
            // by BootStrap class loader
            // getClassLoader method on c1
            System.out.println(c1.getClassLoader());     
              
            System.out.print("primitive int loader : ");
              
            // getting the class loader for primitive int
            // getClassLoader method on c2
            System.out.println(c2.getClassLoader());
        }
    }
    

    输出:

    Test class loader : sun.misc.Launcher$AppClassLoader@73d16e93
    String class loader : null
    primitive int loader : null
    
  18. TypeVariable>[ ] getTypeParameters() :此方法返回一个 TypeVariable 对象数组,这些对象表示由此 GenericDeclaration 对象表示的泛型声明所声明的类型变量,按声明顺序
    Syntax : 
    public TypeVariable>[] getTypeParameters()
    Specified by:
    getTypeParameters in interface GenericDeclaration
    Parameters : 
    NA
    Returns :
    an array of TypeVariable objects that represent the type variables declared 
    by this generic declaration represented by this object.
    Throws :
    GenericSignatureFormatError - if the generic signature of this generic declaration 
    does not conform to the format specified in JVM.
    
    // Java program to demonstrate getTypeParameters() method
      
    import java.lang.reflect.TypeVariable;
      
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c = Class.forName("java.util.Set");
              
            // getting array of TypeVariable for myClass object
            // getTypeParameters method
            TypeVariable[] tv = c.getTypeParameters();
              
            System.out.println("TypeVariables in "+c.getName()+" class : ");
              
            // iterating through all TypeVariables
            for (TypeVariable typeVariable : tv)
            {
                System.out.println(typeVariable);
            }
              
        }
    }
    

    输出:

    TypeVariables in java.util.Set class : 
    E
    
  19. getSuperclass() :此方法返回表示该 Class 所表示的实体(类、接口、原始类型或 void)的超类的 Class。
    如果此 Class 表示 Object 类、接口、原始类型或 void,则返回 null。
    如果此对象表示数组类,则返回表示 Object 类的 Class 对象。
    Syntax : 
    public Class getSuperclass() 
    Parameters : 
    NA
    Returns :
    the superclass of the class represented by this object
    
    // Java program to demonstrate getSuperclass() method
      
    // base class
    class A
    {
        // methods and fields
    }
      
    // derived class
    class B extends A
    {
          
    }
      
    // Driver class
    public class Test
    {
        public static void main(String[] args) 
                throws ClassNotFoundException 
          
        {
            // returns the Class object associated with Test class
            Class myClass = Test.class;
              
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("A");
            Class c2 = Class.forName("B");
            Class c3 = Class.forName("java.lang.Object");
              
            // getSuperclass method returns the superclass of the class represented 
            // by this class object 
              
            System.out.print("Test superclass : ");
              
            // getSuperclass method on myClass
            System.out.println(myClass.getSuperclass());
              
            System.out.print("A superclass : ");
              
            // getSuperclass method on c1
            System.out.println(c1.getSuperclass());
              
            System.out.print("B superclass : ");
              
            // getSuperclass method on c2
            System.out.println(c2.getSuperclass());
              
            System.out.print("Object superclass : ");
              
            // getSuperclass method on c3
            System.out.println(c3.getSuperclass());
        }
    }
    

    输出:

    Test superclass : class java.lang.Object
    A superclass : class java.lang.Object
    B superclass : class A
    Object superclass : null
    
  20. Type getGenericSuperclass() :此方法返回表示此 Class 所表示的实体(类、接口、原始类型或 void)的直接超类的 Type。

    如果此 Class 表示 Object 类、接口、原始类型或 void,则返回 null。如果此对象表示数组类,则返回表示 Object 类的 Class 对象。

    Syntax : 
    public Type getGenericSuperclass()
    Parameters : 
    NA
    Returns :
    the superclass of the class represented by this object
    Throws:
    GenericSignatureFormatError - if the generic class signature does not conform to the format 
    specified in JVM
    TypeNotPresentException - if the generic superclass refers to a non-existent 
    type declaration MalformedParameterizedTypeException - if the generic 
    superclass refers to a parameterized type
    that cannot be instantiated for any reason
    
    // Java program to demonstrate 
    // getGenericSuperclass() method
    public class Test
    {
        public static void main(String[] args) 
                throws ClassNotFoundException 
          
        {
            // returns the Class object associated with Test class
            Class myClass = Test.class;
              
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.util.ArrayList");
            Class c3 = Class.forName("java.lang.Object");
              
            // getGenericSuperclass method returns the generic 
            // superclass of the class represented 
            // by this class object 
              
            System.out.print("Test superclass : ");
              
            // getGenericSuperclass method on myClass
            System.out.println(myClass.getGenericSuperclass());
              
            System.out.print("ArrayList superclass : ");
              
            // getGenericSuperclass method on c1
            System.out.println(c1.getGenericSuperclass());
              
            System.out.print("Object superclass : ");
              
            // getGenericSuperclass method on c3
            System.out.println(c3.getGenericSuperclass());
        }
    }
    

    输出:

    Test superclass : class java.lang.Object
    Set superclass : java.util.AbstractList
    Object superclass : null
    
  21. Class[] getInterfaces() :该方法返回该对象所代表的类或接口实现的接口。

    如果此对象表示一个没有实现接口的类或接口,则该方法返回一个长度为 0 的数组。
    如果此对象表示原始类型或 void,则该方法返回长度为 0 的数组。

    Syntax : 
    public Class[] getInterfaces()
    Parameters : 
    NA
    Returns :
    an array of interfaces implemented by this class.
    
    // Java program to demonstrate getInterfaces() method
      
    // base interface
    interface A
    {
        // methods and constant declarations
    }
      
    // derived class
    class B implements A
    {
        // methods implementations that were declared in A
    }
      
    // Driver class
    public class Test
    {
        public static void main(String[] args) 
                throws ClassNotFoundException 
          
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("B");
            Class c2 = Class.forName("java.lang.String");
              
            // getInterface method on c1
            // it returns the interfaces implemented by B class
            Class c1Interfaces[] = c1.getInterfaces();
              
            // getInterface method on c2
            // returns the interfaces implemented by String class
            Class c2Interfaces[] = c2.getInterfaces();
              
              
            System.out.println("interfaces implemented by B class : ");
              
            // iterating through B class interfaces
            for (Class class1 : c1Interfaces) 
            {
                System.out.println(class1);
            }
              
              
            System.out.println("interfaces implemented by String class : ");
              
            // iterating through String class interfaces
            for (Class class1 : c2Interfaces) 
            {
                System.out.println(class1);
            }
        }
    }
    

    输出:

    interfaces implemented by B class : 
    interface A
    interfaces implemented by String class : 
    interface java.io.Serializable
    interface java.lang.Comparable
    interface java.lang.CharSequence
    
  22. Type[] getGenericInterfaces() :此方法返回表示由该对象表示的类或接口直接实现的接口的类型。

    如果此对象表示一个没有实现接口的类或接口,则该方法返回一个长度为 0 的数组。
    如果此对象表示原始类型或 void,则该方法返回长度为 0 的数组。

    Syntax : 
    public Type[] getGenericInterfaces()
    Parameters : 
    NA
    Returns :
    an array of interfaces implemented by this class
    Throws:
    GenericSignatureFormatError - if the generic class signature does not conform to the format 
    specified in JVM
    TypeNotPresentException - if the generic superinterfaces refers to 
    a non-existent type declaration MalformedParameterizedTypeException - if the
    generic superinterfaces refers to a parameterized type
    that cannot be instantiated for any reason
    
    // Java program to demonstrate getGenericInterfaces() method
      
    import java.lang.reflect.Type;
      
    public class Test
    {
        public static void main(String[] args) 
                throws ClassNotFoundException 
          
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.util.Set");
              
            // getGenericInterfaces() on c1
            // it returns the interfaces implemented by Set interface
            Type c1GenericInterfaces[] = c1.getGenericInterfaces();
              
            System.out.println("interfaces implemented by Set interface : ");
              
            // iterating through Set class interfaces
            for (Type type : c1GenericInterfaces)
            {
                System.out.println(type);
            }
        }
    }
    

    输出:

    interfaces implemented by Set interface : 
    java.util.Collection
    
  23. Package getPackage() :此方法返回此类的包。 JVM Architecture 中的类加载器子系统使用这种方法来查找类或接口的包。
    Syntax : 
    public Package getPackage()
    Parameters : 
    NA
    Returns :
    the package of the class, 
    or null if no package information is available
    from the archive or codebase. 
    
    // Java program to demonstrate getPackage() method
    public class Test
    {
        public static void main(String[] args) throws ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
            Class c2 = Class.forName("java.util.ArrayList");
              
            // getting package of class
      
            // getPackage method on c1
            System.out.println(c1.getPackage());
      
            // getPackage method on c2
            System.out.println(c2.getPackage());
      
        }
    }
    

    输出:

    package java.lang, Java Platform API Specification, version 1.8
    package java.util, Java Platform API Specification, version 1.8
    
  24. Field[] getFields() :此方法返回一个 Field 对象数组,反映由该 Class 对象表示的类(及其所有超类)或接口(及其所有超类)的所有可访问公共字段。
    Syntax : 
    public Field[] getFields()  throws SecurityException
    Parameters : 
    NA
    Returns :
    the array of Field objects representing the public fields
    and  array of length 0 if the class or interface has no accessible public fields
    or if this Class object represents a primitive type or void.
    Throws :
    SecurityException - If a security manager, s, is present.
    
    // Java program to demonstrate getFields() method
      
    import java.lang.reflect.Field;
      
    public class Test
    {
        public static void main(String[] args) 
                throws SecurityException,ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Integer");
              
            // getFields method on c1
            // it array of fields of Integer class
            Field F[] = c1.getFields();
              
            System.out.println("Below are the fields of Integer class : ");
              
            // iterating through all fields of  String class
            for (Field field : F) 
            {
                    System.out.println(field);
            } 
        }
    }
    

    输出 :

    Below are the fields of Integer class :
    public static final int java.lang.Integer.MIN_VALUE
    public static final int java.lang.Integer.MAX_VALUE
    public static final java.lang.Class java.lang.Integer.TYPE
    public static final int java.lang.Integer.SIZE
    public static final int java.lang.Integer.BYTES
    
  25. Class[ ] getClasses() :此方法返回一个包含 Class 对象的数组,这些对象表示所有公共类和接口,这些公共类和接口是此 Class 对象表示的类的成员。该数组包含从超类继承的公共类和接口成员以及类声明的公共类和接口成员。

    如果此 Class 对象没有公共成员类或接口,则此方法返回长度为 0 的数组。
    如果此 Class 对象表示原始类型、数组类或 void,则此方法还返回长度为 0 的数组。

    Syntax : 
    Class[ ] getClasses()
    Parameters : 
    NA
    Returns :
    the array of Class objects representing the public members of this class
    Throws :
    SecurityException - If a security manager, s, is present.
    
    // Java program to demonstrate getClasses() method
      
    public class Test
    {
        // base interface
        public interface A
        {
            // methods and constant declarations
        }
      
        // derived class
        public class B implements A
        {
            // methods implementations that were declared in A
        }
          
        public static void main(String[] args) 
                throws ClassNotFoundException 
          
        {
            // returns the Class object associated with Test class
            Class c1 = Test.class;
              
            // getClasses method on c1
            // it returns the array of Class objects containing the all 
            // public classes and interfaces  represented by Test class
            Class[] c1Classes = c1.getClasses();
              
            System.out.println("public members of Test class : ");
              
            // iterating through all public members of Test class
            for (Class class1 : c1Classes)
            {
                System.out.println(class1);
            }
              
        }
    }
    

    输出:

    public members of Test class : 
    interface Test$A
    class Test$B
    
  26. Method[] getMethods() :此方法返回一个 Method 对象数组,反映类或接口的所有可访问公共方法以及从该 Class 对象表示的超类和超接口继承的那些方法。
    Syntax : 
    public Method[] getMethods() throws SecurityException
    Parameters : 
    NA
    Returns :
    the array of Method objects representing the public methods
    and  array of length 0 if the class or interface has no accessible public method
    or if this Class object represents a primitive type or void.
    Throws :
    SecurityException - If a security manager, s, is present.
    
    // Java program to demonstrate getMethods() method
      
    import java.lang.reflect.Method;
      
    public class Test
    {
        public static void main(String[] args) 
                throws SecurityException,ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Object");
              
            // getMethods method on c1
            // it returns array of methods of Object class
            Method M[] = c1.getMethods();
              
            System.out.println("Below are the methods of Object class : ");
              
            // iterating through all methods of Object class
            for (Method method : M) 
            {
            System.out.println(method);
            }
        }
    }
    

    输出:

    Below are the methods of Object class : 
    public final void java.lang.Object.wait() throws java.lang.InterruptedException
    public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
    public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
    public boolean java.lang.Object.equals(java.lang.Object)
    public java.lang.String java.lang.Object.toString()
    public native int java.lang.Object.hashCode()
    public final native java.lang.Class java.lang.Object.getClass()
    public final native void java.lang.Object.notify()
    public final native void java.lang.Object.notifyAll()
    
  27. Constructor[] getConstructors() :此方法返回一个 Constructor 对象数组,反映此 Class 对象表示的类的所有公共构造函数。
    Syntax : 
    public Constructor[] getConstructors() throws SecurityException
    Parameters : 
    NA
    Returns :
    the array of Constructor objects representing the public constructors of this class
    and  array of length 0 if the class or interface has no accessible public constructor
    or if this Class object represents a primitive type or void.
    Throws :
    SecurityException - If a security manager, s, is present.
    
    // Java program to demonstrate getConstructors() method
      
    import java.lang.reflect.Constructor;
      
    public class Test
    {
        public static void main(String[] args) 
                throws SecurityException,ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Boolean");
              
            // getConstructor method on c1
            // it returns an array of constructors of Boolean class
            Constructor C[] = c1.getConstructors();
              
            System.out.println("Below are the constructors of Boolean class :");
              
            // iterating through all constructors
            for (Constructor constructor : C) 
            {
            System.out.println(constructor);
            }
        }
    }
    

    输出:

    Below are the constructors of Boolean class :
    public java.lang.Boolean(boolean)
    public java.lang.Boolean(java.lang.String)
    
  28. Field getField(String fieldName) :该方法返回一个 Field 对象,该对象反映了该 Class 对象所代表的类或接口的指定公共成员字段。
    Syntax : 
    public Field getField(String fieldName) throws NoSuchFieldException,SecurityException
    Parameters : 
    fieldName -  the field name
    Returns :
    the Field object of this class specified by name
    Throws :
    NoSuchFieldException - if a field with the specified name is not found.
    NullPointerException - if fieldName is null
    SecurityException - If a security manager, s, is present.
    
    // Java program to demonstrate getField() method
      
    import java.lang.reflect.Field;
      
    public class Test
    {
        public static void main(String[] args) 
                throws SecurityException,ClassNotFoundException,
                       NoSuchFieldException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Integer");
              
            // getField method on c1
            // it checks a public field in Integer class with specified parameter
            Field f = c1.getField("MIN_VALUE");
              
            System.out.println("public field in Integer class with MIN_VALUE name :");
            System.out.println(f);
        }
    }
    

    输出:

    public field in Integer class with MIN_VALUE name :
    public static final int java.lang.Integer.MIN_VALUE
    
  29. Method getMethod(String methodName,Class... parameterTypes) :该方法返回一个Method对象,反映了该Class对象所代表的类或接口的指定公共成员方法。
    Syntax : 
    public Method getMethod(String methodName,Class... parameterTypes) throws 
    NoSuchFieldException,SecurityException
    Parameters : 
    methodName -  the method name
    parameterTypes - the list of parameters
    Returns :
    the method object of this class specified by name
    Throws :
    NoSuchMethodException - if a method with the specified name is not found.
    NullPointerException - if methodName is null
    SecurityException - If a security manager, s, is present.
    
    // Java program to demonstrate getMethod() method
      
    import java.lang.reflect.Method;
      
    public class Test
    {
        public static void main(String[] args) 
                throws SecurityException,ClassNotFoundException,
                       NoSuchMethodException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Integer");
            Class c2 = Class.forName("java.lang.String");
              
            // getMethod method on c1
            // it checks for a public Method in Integer class
            Method m = c1.getMethod("parseInt",c2);
              
            System.out.println("method in Integer class specified by parseInt : ");
            System.out.println(m);
        }
    }
    

    输出:

    public method in Integer class specified by parseInt : 
    public static int java.lang.Integer.parseInt(java.lang.String) 
    throws java.lang.NumberFormatException
    
  30. Constructor getConstructor(Class… parameterTypes) :该方法返回一个Constructor对象,该对象反映了该Class对象所代表的类的指定公共构造函数。parameterTypes参数是一个Class对象数组,标识构造函数的形参类型,按声明的顺序。
    Syntax : 
    public Constructor getConstructor(Class... parameterTypes) 
    throws NoSuchMethodException,SecurityException
    Parameters : 
    parameterTypes - the list of parameters
    Returns :
    the Constructor object of the public constructor that matches the specified parameterTypes
    Throws :
    NoSuchMethodException - if a Constructor with the specified parameterTypes is not found.
    SecurityException - If a security manager, s, is present.
    
    // Java program to demonstrate 
    // getConstructor() Constructor
      
    import java.lang.reflect.Constructor;
      
    public class Test
    {
        public static void main(String[] args) 
                throws SecurityException,ClassNotFoundException,
                    NoSuchMethodException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Integer");
            Class c2 = Class.forName("java.lang.String");
              
            // getConstructor method on c1
            // it checks a public Constructor in Integer class
            // with specified parameterTypes
            Constructor c = c1.getConstructor(c2);
              
            System.out.println("Constructor in Integer class & String parameterType:");
            System.out.println(c);
        }
    }
    

    输出:

    public Constructor in Integer class with String parameterType : 
    public java.lang.Integer(java.lang.String) throws java.lang.NumberFormatException
    

    注意:方法getFields()、getMethods()、getConstructors()、getField()、getMethod()、getConstructor()在反射中被广泛使用(参考这个例子)

  31. T cast(Object obj) :此方法用于将对象强制转换为该 Class 对象所表示的类或接口。
    Syntax : 
    public T cast(Object obj)
    TypeParameters : 
    T - The class type whose object is to be cast
    Parameters : 
    obj - the object to be cast
    Returns :
    the object after casting, or null if obj is null
    Throws :
    ClassCastException - if the object is not null and is not assignable to the type T. 
    
    // Java program to demonstrate cast() method
    class A
    {
       // methods and fields
    }
      
    class B extends A 
    {
        // methods and fields
    }
      
    // Driver Class
    public class Test
    {
        public static void main(String[] args) 
        {
            A a = new A();   
              
            System.out.println(a.getClass());
              
            B b = new B();
              
            // casting b to a
            // cast method
            a = A.class.cast(b);
         
            System.out.println(a.getClass());
          
        }
    }
    

    输出:

    class A
    class B
    
  32. asSubclass(Class clazz) :该方法用于将这个 Class 对象转换为表示指定类对象所代表的类的子类。它总是返回对该类对象的引用。
    Syntax : 
    public  Class asSubclass(Class class)
    TypeParameters : 
    U - The superclass type whose object is to be cast
    Parameters : 
    clazz - the superclass object to be cast
    Returns :
    this Class object, cast to represent a subclass of the specified class object.
    Throws :
    ClassCastException - if this Class object does not represent a 
    subclass of the specified class
    (here "subclass" includes the class itself).
    
    // Java program to demonstrate asSubclass() method
    class A
    {
       // methods and fields
    }
      
    class B extends A 
    {
        // methods and fields
    }
      
    // Driver Class
    public class Test
    {
        public static void main(String[] args) 
        {
            A a = new A();   
              
            // returns the Class object for super class(A)
            Class superClass = a.getClass();
              
            B b = new B();
              
            // returns the Class object for subclass(B)
            Class subClass = b.getClass();
              
            // asSubclass method
            // it represent a subclass of the specified  class object
            Class cast = subClass.asSubclass(superClass);
              
            System.out.println(cast);
          
        }
    }
    

    输出:

    class B