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
What is Class Descriptor?
提问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
接口链接
Class.getName()- Explains the "funky" names for arrays and primitives
Class.getName()- 解释数组和原语的“时髦”名称
References
参考
- Java Tutorials/Reflection API
- See also: Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection
- JLS 15.8.2 Class literals
- Java 教程/反射 API
- 另请参阅:Effective Java 2nd Edition,第 53 条:首选接口而不是反射
- JLS 15.8.2 类文字
回答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.
是的。请参阅类文档。

