gpt4 book ai didi

java - 在文档中查找T.class

转载 作者:行者123 更新时间:2023-12-01 18:33:18 24 4
gpt4 key购买 nike

Cay Horstmann 在他的《Core Java》一书中描述了这种获取 Class 类型对象的方法

If T is any Java type, then T.class is the matching class object. For example:

Class cl1 = Date.class; // if you import java.util.*; 
Class cl2 = int.class;
Class cl3 = Double[].class;

Note that a Class object really describes a type, which may or may not be a class. For example, int is not a class, but int.class is nevertheless an object of type Class.

我已经浏览了 java.util,但无法解释这里写的内容。那个“类”似乎是一个字段。 Object 类的字段。尽管它与 Horstmann 先生引用 java.util 的内容相矛盾。你能指出我在哪里可以阅读 javadoc 中的相关内容吗?

最佳答案

它不在 javadoc 中,因为它不是您调用的方法 - 它是该语言语法的一部分。它是一个类文字,在 Java Language Specification section 15.8.2 中进行了描述。 :

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.

The type of C.class, where C is the name of a class, interface, or array type (§4.3), is Class<C>.

The type of p.class, where p is the name of a primitive type (§4.2), is Class<B>, where B is the type of an expression of type p after boxing conversion (§5.1.7).

The type of void.class (§8.4.5) is Class<Void>.

关于java - 在文档中查找T.class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23172196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com