什么是类描述符? [英] What is Class Descriptor?

查看:96
本文介绍了什么是类描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是类描述符?它是特定类的 Class 对象吗?

What is Class Descriptor? Is it a Class object of a particular class?

推荐答案

是的, Class 对象是某个类的类描述符。

Yes, a Class object is a class descriptor for a certain "class".

来自 API

此类的实例表示正在运行的Java应用程序中的类和接口。枚举是一种类,注释是一种接口。每个数组也属于一个类,它反映为一个Class对象,由具有相同元素类型和维数的所有数组共享。原始Java类型(boolean,byte,char,short,int,long,float和double)和关键字void也表示为 Class 对象。

Instances of this class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

以下是一个简单使用 Class 方法的示例,用于反射描述类型:

Here's an example of a simple usage of Class methods to reflectively describe types:

static void describe(Class<?> clazz, String pad, String leadin) {
    if (clazz == null) return;
    String type =
        clazz.isInterface() ? "interface" :
        clazz.isArray() ? "array" :
        clazz.isPrimitive() ? "primitive" :
        clazz.isEnum() ? "enum" :
        "class";
    System.out.printf("%s%s%s %s ( %s )%n",
        pad, leadin, type, clazz.getSimpleName(), clazz.getName());
    for (Class<?> interfaze : clazz.getInterfaces()) {
        describe(interfaze, pad + "   ", "implements ");
    }
    describe(clazz.getComponentType(), pad + "   ", "elements are ");
    describe(clazz.getSuperclass(), pad + "   ", "extends ");
}
static void describe(Class<?> clazz) {
    describe(clazz, "", "");
    System.out.println();
}
public static void main(String[] args) {
    describe(boolean[][].class);
    describe(java.math.RoundingMode.class);
    describe(java.util.ArrayList.class);
    describe(void.class);
}

上面的代码片段产生以下输出:

The above snippet produces the following output:

array boolean[][] ( [[Z )
   implements interface Cloneable ( java.lang.Cloneable )
   implements interface Serializable ( java.io.Serializable )
   elements are array boolean[] ( [Z )
      implements interface Cloneable ( java.lang.Cloneable )
      implements interface Serializable ( java.io.Serializable )
      elements are primitive boolean ( boolean )
      extends class Object ( java.lang.Object )
   extends class Object ( java.lang.Object )

enum RoundingMode ( java.math.RoundingMode )
   extends class Enum ( java.lang.Enum )
      implements interface Comparable ( java.lang.Comparable )
      implements interface Serializable ( java.io.Serializable )
      extends class Object ( java.lang.Object )

class ArrayList ( java.util.ArrayList )
   implements interface List ( java.util.List )
      implements interface Collection ( java.util.Collection )
         implements interface Iterable ( java.lang.Iterable )
   implements interface RandomAccess ( java.util.RandomAccess )
   implements interface Cloneable ( java.lang.Cloneable )
   implements interface Serializable ( java.io.Serializable )
   extends class AbstractList ( java.util.AbstractList )
      implements interface List ( java.util.List )
         implements interface Collection ( java.util.Collection )
            implements interface Iterable ( java.lang.Iterable )
      extends class AbstractCollection ( java.util.AbstractCollection )
         implements interface Collection ( java.util.Collection )
            implements interface Iterable ( java.lang.Iterable )
         extends class Object ( java.lang.Object )

primitive void ( void )



API链接




  • Class.getName()


    • 解释数组和基元的时髦名称


      • Java教程/反思API


        • 另请参阅: Effective Java 2nd Edition,Item 53:首选接口到反射

        • Java Tutorials/Reflection API
          • See also: Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection

          这篇关于什么是类描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆