java 什么是类描述符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2895241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:19:31  来源:igfitidea点击:

What is Class Descriptor?

java

提问by learner

What is Class Descriptor?

什么是类描述符?

Is it a Classobject of a particular class?

它是Class特定类的对象吗?

采纳答案by polygenelubricants

Yes, a Classobject is a class descriptor for a certain "class".

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

From the API:

API

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 Classobjects.

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

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

下面是一个简单使用Class方法来反射性描述类型的示例:

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 links

接口链接

References

参考

回答by Brett Kail

You've given very little context, but "class descriptor" can be a termed used to describe the data necessary to deserialize an object:

您提供的上下文很少,但是“类描述符”可以用来描述反序列化对象所需的数据:

http://java.sun.com/javase/6/docs/platform/serialization/spec/class.html

http://java.sun.com/javase/6/docs/platform/serialization/spec/class.html

In that case, a "class descriptor" is actually java.io.ObjectStreamClass. An ObjectStreamClass describes a class, but it is different from the class itself.

在那种情况下,“类描述符”实际上是java.io.ObjectStreamClass. ObjectStreamClass 描述了一个类,但它与类本身不同。

回答by Matthew Flaschen

Yes. See the Class docs.

是的。请参阅类文档